@@ -44,6 +44,10 @@ def __init__(
44
44
# Variable to keep track of latest published packet
45
45
self ._timestamp_last_publish = datetime .now ()
46
46
47
+ # load special settings for borker compatibility
48
+ self .max_qos = settings .mqtt_max_qos_supported
49
+ self .retain_supported = not settings .mqtt_retain_flag_not_supported
50
+
47
51
self ._client = mqtt .Client (
48
52
client_id = settings .gateway_id ,
49
53
clean_session = not settings .mqtt_persist_session ,
@@ -65,6 +69,7 @@ def __init__(
65
69
66
70
self ._client .username_pw_set (settings .mqtt_username , settings .mqtt_password )
67
71
self ._client .on_connect = self ._on_connect
72
+ self ._client .on_disconnect = self ._on_disconnect
68
73
self ._client .on_publish = self ._on_publish
69
74
70
75
if last_will_topic is not None and last_will_data is not None :
@@ -102,6 +107,10 @@ def _on_connect(self, client, userdata, flags, rc):
102
107
if self .on_connect_cb is not None :
103
108
self .on_connect_cb ()
104
109
110
+ def _on_disconnect (self , client , userdata , rc ):
111
+ if rc != 0 :
112
+ self .logger .error ("MQTT disconnect: %s (%s)" , connack_string (rc ), rc )
113
+
105
114
def _on_publish (self , client , userdata , mid ):
106
115
self ._unpublished_mid_set .remove (mid )
107
116
self ._timestamp_last_publish = datetime .now ()
@@ -196,7 +205,7 @@ def _get_socket(self):
196
205
197
206
def _set_last_will (self , topic , data ):
198
207
# Set Last wil message
199
- self ._client .will_set (topic , data , qos = 2 , retain = True )
208
+ self ._client .will_set (topic , data , qos = 1 , retain = self . retain_supported )
200
209
201
210
def run (self ):
202
211
self .running = True
@@ -238,6 +247,13 @@ def _publish_from_wrapper_thread(self, topic, payload, qos, retain):
238
247
retain: Is it a retain message
239
248
240
249
"""
250
+ # Limit qos in case broker has a limit
251
+ if qos > self .max_qos :
252
+ qos = self .max_qos
253
+
254
+ # Clear retain flag if not supported
255
+ retain = retain and self .retain_supported
256
+
241
257
mid = self ._client .publish (topic , payload , qos = qos , retain = retain ).mid
242
258
if self .publish_queue_size == 0 :
243
259
# Reset last published packet
@@ -250,14 +266,32 @@ def publish(self, topic, payload, qos=1, retain=False) -> None:
250
266
Args:
251
267
topic: Topic to publish on
252
268
payload: Payload
253
- qos: Qos to use
254
- retain: Is it a retain message
269
+ qos: Qos to use. Can be less than requested if broker does
270
+ not support it
271
+ retain: Is it a retain message. Can be discarded if broker
272
+ does not support it
255
273
256
274
"""
275
+ # No need to check qos or retain at this stage as
276
+ # done later in real publish to broker
277
+
257
278
# Send it to the queue to be published from Mqtt thread
258
279
self ._publish_queue .put ((topic , payload , qos , retain ))
259
280
260
281
def subscribe (self , topic , cb , qos = 2 ) -> None :
282
+ """ Method to subscribe to mqtt topic
283
+
284
+ Args:
285
+ topic: Topic to publish on
286
+ cb: Callback to call on message reception
287
+ qos: Qos to use. Can be less than requested if broker does
288
+ not support it
289
+
290
+ """
291
+ # Limit qos in case broker has a limit
292
+ if qos > self .max_qos :
293
+ qos = self .max_qos
294
+
261
295
self .logger .debug ("Subscribing to: {}" .format (topic ))
262
296
self ._client .subscribe (topic , qos )
263
297
self ._client .message_callback_add (topic , cb )
0 commit comments