2025-06-09 10:17:12 +05:00
|
|
|
|
from fastapi import APIRouter, HTTPException
|
2025-06-11 17:16:44 +05:00
|
|
|
|
from typing import List, Dict
|
2025-06-09 10:17:12 +05:00
|
|
|
|
from models.subnet import SubnetScanResult
|
|
|
|
|
|
from models.wifi import WifiScanResult
|
2025-06-11 17:16:44 +05:00
|
|
|
|
from models.request import NoteRequest
|
2025-06-09 10:17:12 +05:00
|
|
|
|
from services.subnet_service import SubnetService
|
|
|
|
|
|
from services.wifi_service import WifiService
|
|
|
|
|
|
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
|
|
|
|
|
|
@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:
|
2025-06-10 16:19:09 +05:00
|
|
|
|
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)}")
|