mirror of
https://github.com/Noettore/lagomareGateKeeperBot.git
synced 2025-10-15 11:36:40 +02:00
Refactor and start implementing inline keyboards
This commit is contained in:
46
gates.py
46
gates.py
@@ -2,47 +2,37 @@ 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 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()
|
||||
self._gates: dict[str, Gate] = self._load_gates()
|
||||
|
||||
def _load_gates(self) -> dict[str, _Gate]:
|
||||
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()}
|
||||
return {gate: Gate.from_dict(data) for gate, data in gates_data.items()}
|
||||
except Exception:
|
||||
return {}
|
||||
|
||||
def get_name(self, gate: str) -> str:
|
||||
return self._gates.get(gate, {}).name
|
||||
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.keys():
|
||||
if gate not in self._gates:
|
||||
return False
|
||||
if self._gates[gate].status == Status.DISABLED:
|
||||
return False
|
||||
|
Reference in New Issue
Block a user