mirror of
https://github.com/Noettore/lagomareGateKeeperBot.git
synced 2025-10-15 03:26:40 +02:00
55 lines
1.7 KiB
Python
55 lines
1.7 KiB
Python
import json
|
|
from avconnect import AVConnectAPI
|
|
from commons import Status, Credential
|
|
|
|
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)
|
|
|
|
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(data["id"], data["name"]) for gate, data in gates_data.items()}
|
|
except Exception:
|
|
return {}
|
|
|
|
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
|