gonets/SRV/api/v1/subnet.py

65 lines
3.2 KiB
Python
Raw Normal View History

from fastapi import APIRouter, HTTPException
from typing import List
from pydantic import BaseModel
from models.subnet import SubnetScanResult
from models.wifi import WifiScanResult
from services.subnet_service import SubnetService
from services.wifi_service import WifiService
router = APIRouter()
class NoteRequest(BaseModel):
note: str
@router.get("/scan", response_model=SubnetScanResult)
def scan_subnet(subnet: str = "192.168.1.0/24", ports: List[int] = None):
"""Сканирует локальную подсеть и возвращает данные об устройствах"""
try:
subnet_service = SubnetService()
result = subnet_service.scan_subnet(subnet, ports)
return result
except Exception as e:
raise HTTPException(status_code=500, detail=f"Ошибка сканирования: {str(e)}")
@router.get("/wifi", response_model=WifiScanResult)
def scan_wifi(timeout: int = 30):
"""Сканирует Wi-Fi точки доступа и возвращает данные о них"""
try:
wifi_service = WifiService()
result = wifi_service.scan_wifi(timeout=timeout)
return result
except Exception as e:
raise HTTPException(status_code=500, detail=f"Ошибка сканирования Wi-Fi: {str(e)}")
@router.post("/devices/{mac}/note")
def add_note(mac: str, request: NoteRequest):
"""Добавляет заметку для устройства по MAC-адресу"""
try:
subnet_service = SubnetService()
if not subnet_service.add_note(mac, request.note):
raise HTTPException(status_code=404, detail=f"Устройство с MAC {mac} не найдено")
return {"message": f"Заметка '{request.note}' добавлена для устройства {mac}"}
except Exception as e:
raise HTTPException(status_code=500, detail=f"Ошибка добавления заметки: {str(e)}")
@router.put("/devices/{mac}/note")
def update_note(mac: str, request: NoteRequest):
"""Обновляет заметку для устройства по MAC-адресу"""
try:
subnet_service = SubnetService()
if not subnet_service.update_note(mac, request.note):
raise HTTPException(status_code=404, detail=f"Устройство с MAC {mac} не найдено")
return {"message": f"Заметка '{request.note}' обновлена для устройства {mac}"}
except Exception as e:
raise HTTPException(status_code=500, detail=f"Ошибка обновления заметки: {str(e)}")
@router.delete("/devices/{mac}/note")
def delete_note(mac: str):
"""Удаляет заметку для устройства по MAC-адресу"""
try:
subnet_service = SubnetService()
if not subnet_service.delete_note(mac):
raise HTTPException(status_code=404, detail=f"Устройство с MAC {mac} не найдено")
return {"message": f"Заметка удалена для устройства {mac}"}
except Exception as e:
raise HTTPException(status_code=500, detail=f"Ошибка удаления заметки: {str(e)}")