-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
hbmqtt is not compatible with Python3.9 [1] and doesn't look like this is going to be fixed soon. [1]: njouanin/hbmqtt#223
- Loading branch information
Showing
4 changed files
with
48 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,62 @@ | ||
import typing | ||
import asyncio | ||
import queue | ||
|
||
import hbmqtt.client | ||
import hbmqtt.mqtt.constants | ||
import typing | ||
import paho.mqtt.client as mqtt | ||
|
||
from .JsonPatchDict import JsonPatchOperation | ||
|
||
|
||
class MqttStateSync: | ||
def __init__(self, | ||
mqtt_uri: str = "mqtt://localhost", | ||
mqtt_host: str = "localhost", | ||
mqtt_port: int = 1883, | ||
mqtt_topic_prefix: str = "", | ||
): | ||
self.mqtt_uri = mqtt_uri | ||
self.mqtt_host = mqtt_host | ||
self.mqtt_port = mqtt_port | ||
self.mqtt_topic_prefix = mqtt_topic_prefix | ||
self.connection = hbmqtt.client.MQTTClient() | ||
self.connection = mqtt.Client() | ||
self._loop = asyncio.get_event_loop() | ||
self._connected = asyncio.Future() | ||
|
||
async def connect(self): | ||
await self.connection.connect(uri=self.mqtt_uri) | ||
self.connection.on_connect = self._mqtt_thread_on_connect | ||
self.connection.connect(host=self.mqtt_host, port=self.mqtt_port) | ||
self.connection.loop_start() # in separate thread | ||
await self._connected | ||
|
||
def _mqtt_thread_on_connect(self, client, userdata, flags, rc): | ||
self._loop.call_soon_threadsafe(self._on_connect, client, userdata, flags, rc) | ||
|
||
def _on_connect(self, client, userdata, flags, rc): | ||
if rc == 0: | ||
self._connected.set_result(True) | ||
else: | ||
self._connected.set_exception(RuntimeError("connection failed")) | ||
|
||
def __hash__(self) -> int: | ||
return hash((self.mqtt_uri, self.mqtt_topic_prefix)) | ||
return hash((self.mqtt_host, self.mqtt_port, self.mqtt_topic_prefix)) | ||
|
||
def __eq__(self, other) -> bool: | ||
if not isinstance(other, MqttStateSync): | ||
return False | ||
return (self.mqtt_uri, self.mqtt_topic_prefix) == (other.mqtt_uri, other.mqtt_topic_prefix) | ||
return (self.mqtt_host, self.mqtt_port, self.mqtt_topic_prefix) \ | ||
== (other.mqtt_host, other.mqtt_port, other.mqtt_topic_prefix) | ||
|
||
def __repr__(self) -> str: | ||
return f"{self.__class__.__name__}(mqtt_uri={repr(self.mqtt_uri)}, " \ | ||
return f"{self.__class__.__name__}(mqtt_host={repr(self.mqtt_host)}, " \ | ||
f"mqtt_port={repr(self.mqtt_port)}, " \ | ||
f"mqtt_topic_prefix={repr(self.mqtt_topic_prefix)})" | ||
|
||
async def publish(self, op: JsonPatchOperation) -> None: | ||
if op.op == JsonPatchOperation.Operation.remove: | ||
return await self.publish_single(path=op.path, value=b"") | ||
return self.publish_single(path=op.path, value=b"") | ||
# else: # replace or add | ||
|
||
coroutines = [] | ||
for simple_op in op.decompose(): | ||
coroutines.append(self.publish_single(path=simple_op.path, value=str(simple_op.value).encode('utf-8'))) | ||
await asyncio.gather(*coroutines) | ||
self.publish_single(path=simple_op.path, value=str(simple_op.value).encode('utf-8')) | ||
|
||
async def publish_single(self, path: typing.List[str], value: bytes) -> None: | ||
def publish_single(self, path: typing.List[str], value: bytes) -> None: | ||
topic = self.mqtt_topic_prefix + '/' + '/'.join(path) | ||
await self.connection.publish(topic=topic, message=value, qos=hbmqtt.mqtt.constants.QOS_2, retain=True) | ||
self.connection.publish(topic=topic, payload=value, qos=2, retain=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters