api: implement abstraction for connecting to CCUJACK MQTT broker
This commit is contained in:
parent
8083330032
commit
1a50d67df6
3 changed files with 104 additions and 0 deletions
|
|
@ -6,6 +6,7 @@ requires-python = ">=3.12"
|
|||
dependencies = [
|
||||
"aiohttp>=3.13.5",
|
||||
"fastapi>=0.136.1",
|
||||
"paho-mqtt>=2.1.0",
|
||||
"simple-openid-connect>=2.4.0",
|
||||
"uvicorn>=0.46.0",
|
||||
]
|
||||
|
|
|
|||
92
api/src/dooris_api/mqtt_client.py
Normal file
92
api/src/dooris_api/mqtt_client.py
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
#
|
||||
# This whole implementation is adapted from the upstream GitHub example
|
||||
# https://github.com/eclipse-paho/paho.mqtt.python/blob/master/examples/loop_asyncio.py
|
||||
#
|
||||
|
||||
from typing import Any
|
||||
import logging
|
||||
import asyncio
|
||||
import socket
|
||||
|
||||
import paho.mqtt.client as mqtt
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class AsyncLooper:
|
||||
"""
|
||||
Helper class to implement loopgin with asyncio for the underlying mqtt IO
|
||||
"""
|
||||
|
||||
def __init__(self, loop: asyncio.AbstractEventLoop, client: mqtt.Client):
|
||||
self.loop = loop
|
||||
|
||||
self.client = client
|
||||
self.client.on_socket_open = self.on_socket_open
|
||||
self.client.on_socket_close = self.on_socket_close
|
||||
self.client.on_socket_register_write = self.on_socket_register_write
|
||||
self.client.on_socket_unregister_write = self.on_socket_unregister_write
|
||||
|
||||
def on_socket_open(self, client, userdata, sock):
|
||||
def cb():
|
||||
client.loop_read()
|
||||
|
||||
self.loop.add_reader(sock, cb)
|
||||
self.task_misc = self.loop.create_task(self.misc_loop())
|
||||
|
||||
def on_socket_close(self, client, userdata, sock):
|
||||
self.loop.remove_reader(sock)
|
||||
self.task_misc.cancel()
|
||||
|
||||
def on_socket_register_write(self, client, userdata, sock):
|
||||
def cb():
|
||||
client.loop_write()
|
||||
|
||||
self.loop.add_writer(sock, cb)
|
||||
|
||||
def on_socket_unregister_write(self, client, userdata, sock):
|
||||
self.loop.remove_writer(sock)
|
||||
|
||||
async def misc_loop(self):
|
||||
while self.client.loop_misc() == mqtt.MQTT_ERR_SUCCESS:
|
||||
try:
|
||||
await asyncio.sleep(1)
|
||||
except asyncio.CancelledError:
|
||||
break
|
||||
|
||||
|
||||
class AsyncMqttClient:
|
||||
loop: asyncio.AbstractEventLoop
|
||||
|
||||
def __init__(self, connection_string: str, username: str, password: str):
|
||||
self.connection_string = connection_string
|
||||
self.client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, client_id="dooris")
|
||||
self.client.username = username
|
||||
self.client.password = password
|
||||
self.client.on_connect = self.on_connect
|
||||
self.client.on_message = self.on_message
|
||||
self.client.on_disconnect = self.on_disconnect
|
||||
|
||||
def on_connect(self, client: mqtt.Client, userdata: Any, flags: mqtt.ConnectFlags, reason_code, properties: mqtt.Properties):
|
||||
logger.debug(f"mqtt client connected with message '{reason_code}'")
|
||||
|
||||
def on_disconnect(self, client: mqtt.Client, userdata: Any, flags: mqtt.DisconnectFlags, reason_code, properties: mqtt.Properties):
|
||||
logger.debug("mqtt client disconnected")
|
||||
print("flags", type(flags), flags)
|
||||
print("reason_code", type(reason_code), reason_code)
|
||||
print("properties", type(properties), properties)
|
||||
|
||||
def on_message(self, client: mqtt.Client, userdata: Any, msg):
|
||||
logger.debug("mqtt client got message")
|
||||
print("msg", type(msg), msg)
|
||||
|
||||
async def connect(self):
|
||||
server_host, server_port = self.connection_string.rsplit(":", maxsplit=1)
|
||||
|
||||
looper = AsyncLooper(asyncio.get_running_loop(), self.client)
|
||||
|
||||
logger.info("Connecting to mqtt server at %s:%s", server_host, server_port)
|
||||
self.client.connect(server_host, int(server_port))
|
||||
self.client.socket().setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 2048)
|
||||
|
||||
11
api/uv.lock
generated
11
api/uv.lock
generated
|
|
@ -400,6 +400,7 @@ source = { editable = "." }
|
|||
dependencies = [
|
||||
{ name = "aiohttp" },
|
||||
{ name = "fastapi" },
|
||||
{ name = "paho-mqtt" },
|
||||
{ name = "simple-openid-connect" },
|
||||
{ name = "uvicorn" },
|
||||
]
|
||||
|
|
@ -413,6 +414,7 @@ dev = [
|
|||
requires-dist = [
|
||||
{ name = "aiohttp", specifier = ">=3.13.5" },
|
||||
{ name = "fastapi", specifier = ">=0.136.1" },
|
||||
{ name = "paho-mqtt", specifier = ">=2.1.0" },
|
||||
{ name = "simple-openid-connect", specifier = ">=2.4.0" },
|
||||
{ name = "uvicorn", specifier = ">=0.46.0" },
|
||||
]
|
||||
|
|
@ -734,6 +736,15 @@ wheels = [
|
|||
{ url = "https://files.pythonhosted.org/packages/b2/6c/d8a02ffb24876b5f51fbd781f479fc6525a518553a4196bd0433dae9ff8e/orderedmultidict-1.0.2-py2.py3-none-any.whl", hash = "sha256:ab5044c1dca4226ae4c28524cfc5cc4c939f0b49e978efa46a6ad6468049f79b", size = 11897, upload-time = "2025-11-18T08:00:41.44Z" },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "paho-mqtt"
|
||||
version = "2.1.0"
|
||||
source = { registry = "https://pypi.org/simple" }
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/39/15/0a6214e76d4d32e7f663b109cf71fb22561c2be0f701d67f93950cd40542/paho_mqtt-2.1.0.tar.gz", hash = "sha256:12d6e7511d4137555a3f6ea167ae846af2c7357b10bc6fa4f7c3968fc1723834", size = 148848, upload-time = "2024-04-29T19:52:55.591Z" }
|
||||
wheels = [
|
||||
{ url = "https://files.pythonhosted.org/packages/c4/cb/00451c3cf31790287768bb12c6bec834f5d292eaf3022afc88e14b8afc94/paho_mqtt-2.1.0-py3-none-any.whl", hash = "sha256:6db9ba9b34ed5bc6b6e3812718c7e06e2fd7444540df2455d2c51bd58808feee", size = 67219, upload-time = "2024-04-29T19:52:48.345Z" },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "parso"
|
||||
version = "0.8.7"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue