mirror of
https://github.com/Noettore/lagomareGateKeeperBot.git
synced 2025-10-14 19:16:40 +02:00
45 lines
1.5 KiB
Python
45 lines
1.5 KiB
Python
import json
|
|
from avconnect import AVConnectAPI
|
|
from commons import Status, Credential
|
|
|
|
class Gate:
|
|
def __init__(self, id: str, name: str, status: Status = Status.ENABLED):
|
|
self.id = id
|
|
self.name = name
|
|
self.status = status if isinstance(status, Status) else Status(status)
|
|
|
|
def to_dict(self):
|
|
return {"id": self.id, "name": self.name, "status": self.status.value}
|
|
|
|
@classmethod
|
|
def from_dict(cls, data: dict):
|
|
return cls(data["id"], data["name"], Status(data.get("status", Status.ENABLED)))
|
|
|
|
class Gates:
|
|
def __init__(self, json_path: str = "./data/gates.json"):
|
|
self._json_path: str = json_path
|
|
self._gates: dict[str, Gate] = self._load_gates()
|
|
|
|
def _load_gates(self) -> dict[str, Gate]:
|
|
try:
|
|
with open(self._json_path, "r") as file:
|
|
gates_data = json.load(file)
|
|
return {gate: Gate.from_dict(data) for gate, data in gates_data.items()}
|
|
except Exception:
|
|
return {}
|
|
|
|
def get_name(self, gate: str) -> str | None:
|
|
return self._gates[gate].name if gate in self._gates else None
|
|
|
|
def open_gate(self, gate: str, credentials: Credential) -> bool:
|
|
if gate not in self._gates:
|
|
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
|