-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathfastmqtt.py
335 lines (278 loc) · 11.9 KB
/
fastmqtt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import asyncio
import logging
import uuid
from itertools import zip_longest
from typing import Any, Callable, Dict, List, Optional, Tuple
from gmqtt import Client as MQTTClient
from gmqtt import Message, Subscription
from gmqtt.mqtt.constants import MQTTv50
from .config import MQTTConfig
from .handlers import (
MQTTConnectionHandler,
MQTTDisconnectHandler,
MQTTHandlers,
MQTTMessageHandler,
MQTTSubscriptionHandler,
)
try:
from uvicorn.config import logger as log_info
except ImportError:
log_info = logging.getLogger()
class FastMQTT:
"""
FastMQTT client sets connection parameters before connecting and manipulating the MQTT service.
The object holds session information necessary to connect the MQTT broker.
config: MQTTConfig config object
client_id: Should be a unique identifier for connection to the MQTT broker.
clean_session: Enables broker to establish a persistent session.
In a persistent session clean_session = False.
The broker stores all subscriptions for the client.
If the session is not persistent (clean_session = True).
The broker does not store anything for the client and \
purges all information from any previous persistent session.
The client_id identifies the session.
optimistic_acknowledgement: #TODO more info needed
mqtt_logger: Optional logging.Logger to use.
"""
def __init__(
self,
config: MQTTConfig,
*,
client_id: Optional[str] = None,
clean_session: bool = True,
optimistic_acknowledgement: bool = True,
mqtt_logger: Optional[logging.Logger] = None,
**kwargs: Any,
) -> None:
if not client_id:
client_id = uuid.uuid4().hex
self.client: MQTTClient = MQTTClient(client_id, **kwargs)
self.config: MQTTConfig = config
self.client._clean_session = clean_session
self.client._username = config.username
self.client._password = config.password
self.client._host = config.host
self.client._port = config.port
self.client._keepalive = config.keepalive
self.client._ssl = config.ssl
self.client.optimistic_acknowledgement = optimistic_acknowledgement
self.client._connect_properties = kwargs
self.client.on_message = self.__on_message
self.client.on_connect = self.__on_connect
self.subscriptions: Dict[str, Tuple[Subscription, List[MQTTMessageHandler]]] = {}
self._logger = mqtt_logger or log_info
self.mqtt_handlers = MQTTHandlers(self.client, self._logger)
if (
self.config.will_message_topic
and self.config.will_message_payload
and self.config.will_delay_interval
):
self.client._will_message = Message(
self.config.will_message_topic,
self.config.will_message_payload,
will_delay_interval=self.config.will_delay_interval,
)
self._logger.debug(
"WILL MESSAGE INITIALIZED: "
"topic -> %s\n payload -> %s\n will_delay_interval -> %s",
self.config.will_message_topic,
self.config.will_message_payload,
self.config.will_delay_interval,
)
@staticmethod
def match(topic: str, template: str) -> bool:
"""
Defined match topics
topic: topic name
template: template topic name that contains wildcards
"""
if str(template).startswith("$share/"):
template = template.split("/", 2)[2]
topic_parts = topic.split("/")
template_parts = template.split("/")
for topic_part, part in zip_longest(topic_parts, template_parts):
if part == "#" and not str(topic_part).startswith("$"):
return True
elif (topic_part is None or part not in {"+", topic_part}) or (
part == "+" and topic_part.startswith("$")
):
return False
continue
return len(template_parts) == len(topic_parts)
async def connection(self) -> None:
if self.client._username:
self.client.set_auth_credentials(self.client._username, self.client._password)
self._logger.debug("user is authenticated")
await self.__set_connetion_config()
version = self.config.version or MQTTv50
self._logger.info("Used broker version is %s", version)
await self.client.connect(
self.client._host,
self.client._port,
self.client._ssl,
self.client._keepalive,
version,
)
self._logger.debug("Connected to broker")
async def __set_connetion_config(self) -> None:
"""
The connected MQTT clients will always try to reconnect in case of lost connections.
The number of reconnect attempts is unlimited.
For changing this behavior, set reconnect_retries and reconnect_delay with its values.
For more info: https://github.com/wialon/gmqtt#reconnects
"""
self.client.set_config(
{
"reconnect_retries": self.config.reconnect_retries,
"reconnect_delay": self.config.reconnect_delay,
}
)
def __on_connect(self, client: MQTTClient, flags: int, rc: int, properties: Any) -> None:
"""
Generic on connecting handler, it would call user handler if defined.
Will perform subscription for given topics.
It cannot be done earlier, since subscription relies on connection.
"""
if self.mqtt_handlers.user_connect_handler is not None:
self.mqtt_handlers.user_connect_handler(client, flags, rc, properties)
for topic in self.subscriptions:
self._logger.debug("Subscribing for %s", topic)
self.client.subscribe(self.subscriptions[topic][0])
async def __on_message(
self, client: MQTTClient, topic: str, payload: bytes, qos: int, properties: Any
) -> Any:
"""
Generic on message handler, it will call user handler if defined.
This will invoke per topic handlers that are subscribed for
"""
gather = []
if self.mqtt_handlers.user_message_handler is not None:
self._logger.debug("Calling user_message_handler")
gather.append(
self.mqtt_handlers.user_message_handler(client, topic, payload, qos, properties)
)
for topic_template in self.subscriptions:
if self.match(topic, topic_template):
self._logger.debug("Calling specific handler for topic %s", topic)
for handler in self.subscriptions[topic_template][1]:
gather.append(handler(client, topic, payload, qos, properties))
return await asyncio.gather(*gather)
def publish(
self,
message_or_topic: str,
payload: Any = None,
qos: int = 0,
retain: bool = False,
**kwargs,
) -> None:
"""
Defined to publish payload MQTT server
message_or_topic: topic name
payload: message payload
qos: Quality of Assurance
retain:
"""
return self.client.publish(
message_or_topic, payload=payload, qos=qos, retain=retain, **kwargs
)
def unsubscribe(self, topic: str, **kwargs):
"""
Defined to unsubscribe topic
topic: topic name
"""
self._logger.debug("unsubscribe")
if topic in self.subscriptions:
del self.subscriptions[topic]
return self.client.unsubscribe(topic, **kwargs)
async def mqtt_startup(self) -> None:
"""Initial connection for MQTT client, for lifespan startup."""
await self.connection()
async def mqtt_shutdown(self) -> None:
"""Final disconnection for MQTT client, for lifespan shutdown."""
await self.client.disconnect()
def init_app(self, fastapi_app) -> None: # pragma: no cover
"""Add startup and shutdown event handlers for app without lifespan."""
@fastapi_app.on_event("startup")
async def startup() -> None:
await self.mqtt_startup()
@fastapi_app.on_event("shutdown")
async def shutdown() -> None:
await self.mqtt_shutdown()
def subscribe(
self,
*topics,
qos: int = 0,
no_local: bool = False,
retain_as_published: bool = False,
retain_handling_options: int = 0,
subscription_identifier: Any = None,
) -> Callable[..., Any]:
"""
Decorator method used to subscribe for specific topics.
"""
def subscribe_handler(handler: MQTTMessageHandler) -> MQTTMessageHandler:
self._logger.debug("Subscribe for topics: %s", topics)
for topic in topics:
if topic not in self.subscriptions:
subscription = Subscription(
topic,
qos,
no_local,
retain_as_published,
retain_handling_options,
subscription_identifier,
)
self.subscriptions[topic] = (subscription, [handler])
else:
# Use the most restrictive field of the same subscription
old_subscription = self.subscriptions[topic][0]
new_subscription = Subscription(
topic,
max(qos, old_subscription.qos),
no_local or old_subscription.no_local,
retain_as_published or old_subscription.retain_as_published,
max(
retain_handling_options,
old_subscription.retain_handling_options,
),
old_subscription.subscription_identifier or subscription_identifier,
)
self.subscriptions[topic] = (
new_subscription,
self.subscriptions[topic][1],
)
self.subscriptions[topic][1].append(handler)
return handler
return subscribe_handler
def on_connect(self) -> Callable[..., Any]:
"""
Decorator method used to handle the connection to MQTT.
"""
def connect_handler(handler: MQTTConnectionHandler) -> MQTTConnectionHandler:
self._logger.debug("handler accepted")
return self.mqtt_handlers.on_connect(handler)
return connect_handler
def on_message(self) -> Callable[..., Any]:
"""
The decorator method is used to subscribe to messages from all topics.
"""
def message_handler(handler: MQTTMessageHandler) -> MQTTMessageHandler:
self._logger.debug("on_message handler accepted")
return self.mqtt_handlers.on_message(handler)
return message_handler
def on_disconnect(self) -> Callable[..., Any]:
"""
The Decorator method used wrap disconnect callback.
"""
def disconnect_handler(handler: MQTTDisconnectHandler) -> MQTTDisconnectHandler:
self._logger.debug("on_disconnect handler accepted")
return self.mqtt_handlers.on_disconnect(handler)
return disconnect_handler
def on_subscribe(self) -> Callable[..., Any]:
"""
Decorator method is used to obtain subscribed topics and properties.
"""
def subscribe_handler(handler: MQTTSubscriptionHandler) -> MQTTSubscriptionHandler:
self._logger.debug("on_subscribe handler accepted")
return self.mqtt_handlers.on_subscribe(handler)
return subscribe_handler