Complete refactor... Some issue...

This commit is contained in:
2025-05-11 02:09:24 +02:00
parent c763e76ec7
commit ebd8529c41
8 changed files with 431 additions and 179 deletions

View File

@@ -1,27 +1,54 @@
import json
from avconnect import AVConnectAPI
from commons import Status, Credential
GATE_FILE = "./data/gates.json"
class _Gate:
def __init__(self, id: str, name: str, status: int | Status = Status.ENABLED):
self._id: str = id
self._name: str = name
self._status: Status = status if isinstance(status, Status) else Status(status)
@property
def id(self) -> str:
return self._id
@property
def name(self) -> str:
return self._name
@property
def status(self) -> Status:
return self._status
@status.setter
def status(self, status: int | Status):
self._status = status if isinstance(status, Status) else Status(status)
def load_gates():
try:
with open(GATE_FILE, "r") as f:
return json.load(f)
except FileNotFoundError:
return {}
class Gates:
def get_name(gate):
return load_gates().get(str(gate), {}).get("name", "")
def __init__(self, json_path: str = "./data/gates.json"):
self._json_path: str = json_path
self._gates: dict[str, _Gate] = self._load_gates()
def open_gate(gate, credentials):
gate_info = load_gates().get(str(gate), {})
if not gate_info:
return False
gate_id = gate_info["id"]
def _load_gates(self):
try:
with open(self._json_path, "r") as file:
gates_data = json.load(file)
return {gate: _Gate(data["id"], data["name"]) for gate, data in gates_data.items()}
except Exception:
return {}
try:
api = AVConnectAPI(credentials["username"], credentials["password"])
return api.exec_gate_macro(gate_id)
except Exception as e:
print(f"Failed to open gate {gate}: {e}")
return False
def get_name(self, gate: str) -> str:
return self._gates.get(gate, {}).name
def open_gate(self, gate: str, credentials: Credential) -> bool:
if gate not in self._gates.keys():
return False
if self._gates[gate].status == Status.DISABLED:
return False
try:
api = AVConnectAPI(credentials)
return api.exec_gate_macro(self._gates[gate].id)
except Exception as e:
print(f"Failed to open gate {gate}: {e}")
return False