Разделение сканирования на разные подсети. Хранение каждой из просканированных подсетей. Исправление багов с заметками и обновленные локали
This commit is contained in:
parent
c326edfb9a
commit
b1306ff66f
10 changed files with 1124 additions and 285 deletions
|
|
@ -1,8 +1,11 @@
|
|||
import json
|
||||
import logging
|
||||
from utils.i18n import get_translations, _
|
||||
from typing import Optional
|
||||
|
||||
from utils.i18n import get_translations
|
||||
from babel.support import Translations
|
||||
from fastapi import Depends
|
||||
from pathlib import Path
|
||||
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
|
@ -11,12 +14,16 @@ class SettingsService:
|
|||
"""Сервис для управления настройками и очистки данных сети."""
|
||||
|
||||
def __init__(self, translations: Translations = Depends(get_translations())):
|
||||
self.subnet_file = "storage/devices/devices.json"
|
||||
self.subnet_base_path = "storage/subnets"
|
||||
self.wifi_file = "storage/wifi/wifi.json"
|
||||
self.translations = translations
|
||||
|
||||
def clear_subnet(self) -> bool:
|
||||
"""Очищает данные узлов подсети в файле devices.json.
|
||||
def clear_subnet(self, subnet: Optional[str] = None) -> bool:
|
||||
"""Очищает данные узлов подсети в указанном файле или во всех файлах.
|
||||
|
||||
Args:
|
||||
subnet (Optional[str]): Подсеть в формате CIDR (например, "10.1.1.0/24").
|
||||
Если None, очищает все подсети.
|
||||
|
||||
Returns:
|
||||
bool: True, если очистка успешна.
|
||||
|
|
@ -25,12 +32,23 @@ class SettingsService:
|
|||
Exception: Если произошла ошибка при записи в файл.
|
||||
"""
|
||||
try:
|
||||
with open(self.subnet_file, "w") as f:
|
||||
json.dump([], f)
|
||||
logger.info("Subnet nodes data successfully cleared")
|
||||
return True
|
||||
if subnet:
|
||||
file_path = Path(self.subnet_base_path) / f"{subnet.replace('/', '_')}.json"
|
||||
if file_path.exists():
|
||||
with open(file_path, "w") as f:
|
||||
json.dump([], f)
|
||||
logger.info(f"Subnet nodes data cleared for {subnet}")
|
||||
else:
|
||||
logger.info(f"Subnet file not found for {subnet}, clearing considered successful")
|
||||
return True
|
||||
else:
|
||||
for file_path in Path(self.subnet_base_path).glob("*.json"):
|
||||
with open(file_path, "w") as f:
|
||||
json.dump([], f)
|
||||
logger.info(f"Cleared subnet data in {file_path}")
|
||||
return True
|
||||
except FileNotFoundError:
|
||||
logger.info("Devices file not found, clearing considered successful")
|
||||
logger.info("No subnet files found, clearing considered successful")
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.error(f"Error clearing subnet nodes: {str(e)}")
|
||||
|
|
@ -58,7 +76,7 @@ class SettingsService:
|
|||
raise Exception(self.translations.gettext("Ошибка очистки точек доступа Wi-Fi: {error}").format(error=str(e)))
|
||||
|
||||
def clear_all_data(self) -> bool:
|
||||
"""Очищает данные узлов подсети и точек доступа Wi-Fi в обоих файлах.
|
||||
"""Очищает данные узлов всех подсетей и точек доступа Wi-Fi.
|
||||
|
||||
Returns:
|
||||
bool: True, если очистка успешна.
|
||||
|
|
@ -68,10 +86,12 @@ class SettingsService:
|
|||
"""
|
||||
try:
|
||||
try:
|
||||
with open(self.subnet_file, "w") as f:
|
||||
json.dump([], f)
|
||||
for file_path in Path(self.subnet_base_path).glob("*.json"):
|
||||
with open(file_path, "w") as f:
|
||||
json.dump([], f)
|
||||
logger.info(f"Cleared subnet data in {file_path}")
|
||||
except FileNotFoundError:
|
||||
logger.info("Devices file not found, clearing considered successful")
|
||||
logger.info("No subnet files found, clearing considered successful")
|
||||
try:
|
||||
with open(self.wifi_file, "w") as f:
|
||||
json.dump([], f)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue