Версия 0.0.3
This commit is contained in:
parent
413e9c57be
commit
8c7d0609df
189 changed files with 52063 additions and 2769 deletions
|
|
@ -3,7 +3,13 @@ import logging
|
|||
from typing import Dict, Tuple, List, Union
|
||||
|
||||
from services.network_service import NetworkService
|
||||
from models.network import NetworkInterfacesContainer, Activation, Deactivation, NetworkInterface, WiFiDevice
|
||||
from models.network import (
|
||||
NetworkInterfacesContainer,
|
||||
Activation,
|
||||
Deactivation,
|
||||
NetworkInterface,
|
||||
WiFiDevice,
|
||||
)
|
||||
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
|
@ -19,19 +25,29 @@ class ContainerPositionManager:
|
|||
|
||||
def __init__(self):
|
||||
self.positions: Dict[str, Dict[str, Union[int, bool]]] = {
|
||||
'activation': {'x': 0, 'y': 0, 'isOpen': False},
|
||||
'deactivation': {'x': 0, 'y': 0, 'isOpen': False}
|
||||
"activation": {
|
||||
"x": 0,
|
||||
"y": 0,
|
||||
"isOpen": False
|
||||
},
|
||||
"deactivation": {
|
||||
"x": 0,
|
||||
"y": 0,
|
||||
"isOpen": False
|
||||
},
|
||||
}
|
||||
|
||||
def get_position_and_state(self, container_name: str) -> Tuple[int, int, bool]:
|
||||
def get_position_and_state(self,
|
||||
container_name: str) -> Tuple[int, int, bool]:
|
||||
container = self.positions.get(container_name)
|
||||
if not container:
|
||||
raise ValueError(f"Неверное имя контейнера: {container_name}")
|
||||
return container['x'], container['y'], container['isOpen']
|
||||
return container["x"], container["y"], container["isOpen"]
|
||||
|
||||
def update_position(self, container_id: int, x: int, y: int, isOpen: bool) -> None:
|
||||
container_name = 'activation' if container_id == 1 else 'deactivation'
|
||||
self.positions[container_name] = {'x': x, 'y': y, 'isOpen': isOpen}
|
||||
def update_position(self, container_id: int, x: int, y: int,
|
||||
isOpen: bool) -> None:
|
||||
container_name = "activation" if container_id == 1 else "deactivation"
|
||||
self.positions[container_name] = {"x": x, "y": y, "isOpen": isOpen}
|
||||
|
||||
|
||||
container_manager = ContainerPositionManager()
|
||||
|
|
@ -45,44 +61,50 @@ async def get_network_data() -> Dict[str, Dict]:
|
|||
try:
|
||||
network_data = await network_service.get_network_interfaces_container()
|
||||
|
||||
activation_interfaces, deactivation_interfaces = _split_interfaces(network_data.interfaces)
|
||||
activation_interfaces, deactivation_interfaces = _split_interfaces(
|
||||
network_data.interfaces)
|
||||
|
||||
activation_x, activation_y, activation_is_open = container_manager.get_position_and_state('activation')
|
||||
deactivation_x, deactivation_y, deactivation_is_open = container_manager.get_position_and_state('deactivation')
|
||||
activation_x, activation_y, activation_is_open = (
|
||||
container_manager.get_position_and_state("activation"))
|
||||
deactivation_x, deactivation_y, deactivation_is_open = (
|
||||
container_manager.get_position_and_state("deactivation"))
|
||||
|
||||
activation = Activation(
|
||||
id=1,
|
||||
interfaces=activation_interfaces,
|
||||
container_x=activation_x,
|
||||
container_y=activation_y,
|
||||
isOpen=activation_is_open
|
||||
isOpen=activation_is_open,
|
||||
)
|
||||
deactivation = Deactivation(
|
||||
id=2,
|
||||
interfaces=deactivation_interfaces,
|
||||
container_x=deactivation_x,
|
||||
container_y=deactivation_y,
|
||||
isOpen=deactivation_is_open
|
||||
isOpen=deactivation_is_open,
|
||||
)
|
||||
|
||||
return {
|
||||
"activation": activation.model_dump(),
|
||||
"deactivation": deactivation.model_dump()
|
||||
"deactivation": deactivation.model_dump(),
|
||||
}
|
||||
except Exception as e:
|
||||
logger.error(f"Ошибка в get_network_data: {e}")
|
||||
raise HTTPException(status_code=500, detail="Не удалось получить сетевые данные")
|
||||
raise HTTPException(status_code=500,
|
||||
detail="Не удалось получить сетевые данные")
|
||||
|
||||
|
||||
def _split_interfaces(interfaces: List[Union[NetworkInterface, WiFiDevice]]) -> Tuple[
|
||||
List[Union[NetworkInterface, WiFiDevice]], List[Union[NetworkInterface, WiFiDevice]]]:
|
||||
def _split_interfaces(
|
||||
interfaces: List[Union[NetworkInterface, WiFiDevice]],
|
||||
) -> Tuple[List[Union[NetworkInterface, WiFiDevice]], List[Union[
|
||||
NetworkInterface, WiFiDevice]]]:
|
||||
"""
|
||||
Разделяет интерфейсы на активные и неактивные
|
||||
"""
|
||||
activation_interfaces = []
|
||||
deactivation_interfaces = []
|
||||
for interface in interfaces:
|
||||
if interface.device_state == '100':
|
||||
if interface.device_state == "100":
|
||||
activation_interfaces.append(interface)
|
||||
else:
|
||||
deactivation_interfaces.append(interface)
|
||||
|
|
@ -90,15 +112,20 @@ def _split_interfaces(interfaces: List[Union[NetworkInterface, WiFiDevice]]) ->
|
|||
|
||||
|
||||
@router.post("/update_container_position")
|
||||
async def update_container_position(data: Dict[str, Union[int, bool]] = Body(...)):
|
||||
async def update_container_position(data: Dict[str, Union[int,
|
||||
bool]] = Body(...)):
|
||||
"""
|
||||
Обновляет позицию и состояние контейнера
|
||||
"""
|
||||
try:
|
||||
|
||||
required_keys = ["container_id", "container_x", "container_y", "isOpen"]
|
||||
required_keys = [
|
||||
"container_id", "container_x", "container_y", "isOpen"
|
||||
]
|
||||
if not all(key in data for key in required_keys):
|
||||
raise ValueError("Отсутствуют обязательные поля: container_id, container_x, container_y, isOpen")
|
||||
raise ValueError(
|
||||
"Отсутствуют обязательные поля: container_id, container_x, container_y, isOpen"
|
||||
)
|
||||
|
||||
container_id = data["container_id"]
|
||||
container_x = data["container_x"]
|
||||
|
|
@ -106,16 +133,24 @@ async def update_container_position(data: Dict[str, Union[int, bool]] = Body(...
|
|||
is_open = data["isOpen"]
|
||||
|
||||
# Проверка типов
|
||||
if not isinstance(container_id, int) or not isinstance(container_x, int) or not isinstance(container_y,
|
||||
int) or not isinstance(
|
||||
is_open, bool):
|
||||
if (not isinstance(container_id, int)
|
||||
or not isinstance(container_x, int)
|
||||
or not isinstance(container_y, int)
|
||||
or not isinstance(is_open, bool)):
|
||||
raise ValueError("Неверный тип данных в запросе")
|
||||
|
||||
container_manager.update_position(container_id, container_x, container_y, is_open)
|
||||
return {"success": True, "message": "Позиция и состояние контейнера обновлены"}
|
||||
container_manager.update_position(container_id, container_x,
|
||||
container_y, is_open)
|
||||
return {
|
||||
"success": True,
|
||||
"message": "Позиция и состояние контейнера обновлены"
|
||||
}
|
||||
except Exception as e:
|
||||
logger.error(f"Ошибка обновления позиции контейнера: {e}")
|
||||
raise HTTPException(status_code=400, detail=f"Не удалось обновить положение контейнера: {str(e)}")
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail=f"Не удалось обновить положение контейнера: {str(e)}",
|
||||
)
|
||||
|
||||
|
||||
@router.get("/get_hostname")
|
||||
|
|
@ -125,4 +160,5 @@ async def get_hostname():
|
|||
return hostname_info
|
||||
except Exception as e:
|
||||
logger.error(f"Ошибка в get_hostname: {e}")
|
||||
raise HTTPException(status_code=500, detail="Не удалось получить имя хоста")
|
||||
raise HTTPException(status_code=500,
|
||||
detail="Не удалось получить имя хоста")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue