Files
NetworkDeviceManagementSystem/backend/modules/device/service.py

139 lines
5.0 KiB
Python
Raw Normal View History

2026-05-27 13:50:27 +07:00
from modules.device.repository import (
find_all_devices,
find_device_by_id,
find_device_by_name,
find_device_by_ip,
insert_device,
update_device_db,
delete_device_db
)
from modules.device.exceptions import (
DeviceNotFoundException,
DeviceAlreadyExistsException,
DeviceIPAlreadyExistsException
)
from modules.device_type.repository import find_device_type_by_id
from modules.device_type.exceptions import DeviceTypeNotFoundException
from scheduler.scheduler import (
add_device_monitoring_job,
remove_device_monitoring_job,
reschedule_device_monitoring_job
)
def get_devices_service():
"""Lấy danh sách tất cả thiết bị"""
return find_all_devices()
def get_device_by_id_service(device_id):
"""
Lấy thông tin chi tiết một thiết bị theo ID.
Ném lỗi DeviceNotFoundException nếu không tồn tại.
"""
device = find_device_by_id(device_id)
if not device:
raise DeviceNotFoundException(device_id)
return device
def create_device_service(data):
"""
Tạo mới một thiết bị mạng.
Các bước xử :
1. Kiểm tra loại thiết bị (device_type_id) tồn tại trong hệ thống hay không.
2. Kiểm tra trùng lặp tên thiết bị (case-insensitive).
3. Kiểm tra trùng lặp địa chỉ IP.
4. Thêm thiết bị cấu hình mặc định (monitor & alert) vào database.
5. Đăng công việc giám sát vào Scheduler nền.
"""
# 1. Kiểm tra DeviceType
device_type = find_device_type_by_id(data["device_type_id"])
if not device_type:
raise DeviceTypeNotFoundException(data["device_type_id"])
# 2. Kiểm tra trùng tên
existing_name = find_device_by_name(data["name"])
if existing_name:
raise DeviceAlreadyExistsException(data["name"])
# 3. Kiểm tra trùng IP
existing_ip = find_device_by_ip(data["ip_address"])
if existing_ip:
raise DeviceIPAlreadyExistsException(data["ip_address"])
2026-05-29 11:10:51 +07:00
# 4. Insert DB (trả về thiết bị và cấu hình giám sát mặc định vừa tạo)
new_device, monitor_config = insert_device(data)
2026-05-27 13:50:27 +07:00
# 5. Kích hoạt Job giám sát trên Background Scheduler
2026-05-29 11:10:51 +07:00
# Truyền kèm cấu hình giám sát mặc định thay vì None để Scheduler lập lịch
add_device_monitoring_job(new_device["id"], monitor_config)
2026-05-27 13:50:27 +07:00
return new_device
def update_device_service(device_id, data):
"""
Cập nhật thông tin thiết bị mạng.
Các bước xử :
1. Kiểm tra sự tồn tại của thiết bị.
2. Nếu cập nhật device_type_id -> kiểm tra xem tồn tại không.
3. Nếu cập nhật name -> kiểm tra xem bị trùng với thiết bị khác không.
4. Nếu cập nhật ip_address -> kiểm tra xem bị trùng với thiết bị khác không.
5. Cập nhật dữ liệu vào DB.
6. Cập nhật lại cấu hình giám sát trong Scheduler.
"""
# 1. Kiểm tra thiết bị có tồn tại hay không
existing = find_device_by_id(device_id)
if not existing:
raise DeviceNotFoundException(device_id)
# 2. Kiểm tra loại thiết bị nếu có truyền vào
new_device_type_id = data.get("device_type_id")
if new_device_type_id and new_device_type_id != existing["device_type_id"]:
device_type = find_device_type_by_id(new_device_type_id)
if not device_type:
raise DeviceTypeNotFoundException(new_device_type_id)
# 3. Kiểm tra trùng tên khi tên bị thay đổi
new_name = data.get("name")
if new_name and new_name.lower() != existing["name"].lower():
conflict_name = find_device_by_name(new_name)
if conflict_name and conflict_name["id"] != device_id:
raise DeviceAlreadyExistsException(new_name)
# 4. Kiểm tra trùng IP khi IP bị thay đổi
new_ip = data.get("ip_address")
if new_ip and new_ip != existing["ip_address"]:
conflict_ip = find_device_by_ip(new_ip)
if conflict_ip and conflict_ip["id"] != device_id:
raise DeviceIPAlreadyExistsException(new_ip)
# 5. Cập nhật DB
updated_device = update_device_db(device_id, data)
# 6. Cập nhật lại thông tin giám sát trong Scheduler
reschedule_device_monitoring_job(device_id, None)
return updated_device
def delete_device_service(device_id):
"""
Xóa thiết bị mạng.
Các bước xử :
1. Kiểm tra sự tồn tại của thiết bị.
2. Thực hiện xóa khỏi DB (tự động xóa cấu hình lịch sử liên quan).
3. Hủy bỏ job giám sát khỏi Scheduler.
"""
# 1. Kiểm tra tồn tại
existing = find_device_by_id(device_id)
if not existing:
raise DeviceNotFoundException(device_id)
# 2. Xóa khỏi DB
delete_device_db(device_id)
# 3. Hủy công việc giám sát trong Scheduler
remove_device_monitoring_job(device_id)