Implement Server-Sent-Events API #3
3 changed files with 17 additions and 2 deletions
commit
0331dd6406
|
|
@ -52,6 +52,12 @@ def main():
|
|||
default=os.environ.get("DOORIS_CCUJACK_URL", "https://hmdooris-ccu.ccchh.net:2122"),
|
||||
help="The URL under which a CCUJACK instance is hosted that actually operates the locks",
|
||||
)
|
||||
argp.add_argument(
|
||||
"--ccujack-mqtt",
|
||||
required=False,
|
||||
default=os.environ.get("DOORIS_CCUJACK_MQTT", "hmdooris-ccu.ccchh.net:1883"),
|
||||
help="The $HOSTNAME:$PORT of the CCUJack embedded MQTT server",
|
||||
)
|
||||
argp.add_argument(
|
||||
"--ccujack-user",
|
||||
required="DOORIS_CCUJACK_USER" not in os.environ,
|
||||
|
|
|
|||
|
|
@ -36,11 +36,14 @@ async def lifespan(app: FastAPI):
|
|||
scope=app_cfg.openid_scope,
|
||||
)
|
||||
|
||||
# TODO: regularly re-query CCUJACK to discover new locks
|
||||
app.extra["ccujack"] = CCUJackClient(
|
||||
base_uri=app_cfg.ccujack_url,
|
||||
auth=BasicAuth(app_cfg.ccujack_user, app_cfg.ccujack_password)
|
||||
auth=BasicAuth(app_cfg.ccujack_user, app_cfg.ccujack_password),
|
||||
mqtt_conn=app_cfg.ccujack_mqtt,
|
||||
)
|
||||
await app.extra["ccujack"].find_locks()
|
||||
await app.extra["ccujack"].connect_mqtt()
|
||||
|
||||
yield
|
||||
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@ import logging
|
|||
import asyncio
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from dooris_api.mqtt_client import AsyncMqttClient
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
|
@ -69,7 +71,7 @@ class CCUJackClient:
|
|||
base_uri: str
|
||||
locks: LockData
|
||||
|
||||
def __init__(self, base_uri: str, auth: BasicAuth):
|
||||
def __init__(self, base_uri: str, auth: BasicAuth, mqtt_conn: str):
|
||||
self.http = ClientSession(
|
||||
base_url=base_uri,
|
||||
auth=auth,
|
||||
|
|
@ -77,6 +79,10 @@ class CCUJackClient:
|
|||
connector=TCPConnector(ssl=False),
|
||||
)
|
||||
self.locks = None
|
||||
self.mqtt = AsyncMqttClient(mqtt_conn, auth.login, auth.password)
|
||||
|
||||
async def connect_mqtt(self):
|
||||
await self.mqtt.connect()
|
||||
|
||||
async def find_locks(self):
|
||||
logger.debug("Inspecting lock devices present in CCUJack")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue