api: connect to CCUJACK MQTT broker on startup
All checks were successful
Build Container / Build Container (push) Successful in 1m29s

This commit is contained in:
lilly 2026-05-19 09:34:51 +02:00
commit 0331dd6406
Signed by: lilly
SSH key fingerprint: SHA256:y9T5GFw2A20WVklhetIxG1+kcg/Ce0shnQmbu1LQ37g
3 changed files with 17 additions and 2 deletions

View file

@ -52,6 +52,12 @@ def main():
default=os.environ.get("DOORIS_CCUJACK_URL", "https://hmdooris-ccu.ccchh.net:2122"), 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", 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( argp.add_argument(
"--ccujack-user", "--ccujack-user",
required="DOORIS_CCUJACK_USER" not in os.environ, required="DOORIS_CCUJACK_USER" not in os.environ,

View file

@ -36,11 +36,14 @@ async def lifespan(app: FastAPI):
scope=app_cfg.openid_scope, scope=app_cfg.openid_scope,
) )
# TODO: regularly re-query CCUJACK to discover new locks
app.extra["ccujack"] = CCUJackClient( app.extra["ccujack"] = CCUJackClient(
base_uri=app_cfg.ccujack_url, 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"].find_locks()
await app.extra["ccujack"].connect_mqtt()
yield yield

View file

@ -4,6 +4,8 @@ import logging
import asyncio import asyncio
from pydantic import BaseModel, Field from pydantic import BaseModel, Field
from dooris_api.mqtt_client import AsyncMqttClient
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -69,7 +71,7 @@ class CCUJackClient:
base_uri: str base_uri: str
locks: LockData locks: LockData
def __init__(self, base_uri: str, auth: BasicAuth): def __init__(self, base_uri: str, auth: BasicAuth, mqtt_conn: str):
self.http = ClientSession( self.http = ClientSession(
base_url=base_uri, base_url=base_uri,
auth=auth, auth=auth,
@ -77,6 +79,10 @@ class CCUJackClient:
connector=TCPConnector(ssl=False), connector=TCPConnector(ssl=False),
) )
self.locks = None 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): async def find_locks(self):
logger.debug("Inspecting lock devices present in CCUJack") logger.debug("Inspecting lock devices present in CCUJack")