71 @param cleanSession flag indicating to start a clean session |
71 @param cleanSession flag indicating to start a clean session |
72 @type bool |
72 @type bool |
73 @param userdata user data |
73 @param userdata user data |
74 @type any |
74 @type any |
75 @param protocol version of the MQTT protocol to use |
75 @param protocol version of the MQTT protocol to use |
76 @type int, one of mqtt.MQTTv31 or mqtt.MQTTv311 |
76 @type int, one of mqtt.MQTTv3, mqtt.MQTTv311 or mqtt.MQTTv5 |
77 @param transport transport to be used |
77 @param transport transport to be used |
78 @type str, one of "tcp" or "websockets" |
78 @type str, one of "tcp" or "websockets" |
79 @param parent reference to the parent object |
79 @param parent reference to the parent object |
80 @type QObject |
80 @type QObject |
81 """ |
81 """ |
89 MqttClient.DefaultConnectTimeout * 1000) |
89 MqttClient.DefaultConnectTimeout * 1000) |
90 self.__connectTimeoutTimer.timeout.connect(self.__connectTimeout) |
90 self.__connectTimeoutTimer.timeout.connect(self.__connectTimeout) |
91 |
91 |
92 self.onConnect.connect(self.__connectTimeoutTimer.stop) |
92 self.onConnect.connect(self.__connectTimeoutTimer.stop) |
93 |
93 |
|
94 # TODO: MQTTv5: set clean_session to None and remember cleanSession |
94 self.__mqttClient = mqtt.Client( |
95 self.__mqttClient = mqtt.Client( |
95 client_id=clientId, clean_session=cleanSession, userdata=None, |
96 client_id=clientId, clean_session=cleanSession, userdata=None, |
96 protocol=mqtt.MQTTv311, transport="tcp") |
97 protocol=mqtt.MQTTv311, transport="tcp") |
97 |
98 |
98 self.__initCallbacks() |
99 self.__initCallbacks() |
99 |
100 |
100 def __initCallbacks(self): |
101 def __initCallbacks(self): |
101 """ |
102 """ |
102 Private method to initialize the MQTT callback methods. |
103 Private method to initialize the MQTT callback methods. |
103 """ |
104 """ |
|
105 # TODO: add properties to signal |
|
106 # TODO: MQTTv5: add support for MQTTv5 signature |
104 self.__mqttClient.on_connect = ( |
107 self.__mqttClient.on_connect = ( |
105 lambda client, userdata, flags, rc: self.onConnect.emit( |
108 lambda client, userdata, flags, rc, properties: |
106 flags, rc)) |
109 self.onConnect.emit(flags, rc)) |
|
110 # TODO: MQTTv5: add support for MQTTv5 signature |
107 self.__mqttClient.on_disconnect = ( |
111 self.__mqttClient.on_disconnect = ( |
108 lambda client, userdata, rc: self.onDisconnected.emit(rc)) |
112 lambda client, userdata, rc: self.onDisconnected.emit(rc)) |
109 self.__mqttClient.on_log = ( |
113 self.__mqttClient.on_log = ( |
110 lambda client, userdata, level, buf: self.onLog.emit(level, buf)) |
114 lambda client, userdata, level, buf: self.onLog.emit(level, buf)) |
111 self.__mqttClient.on_message = ( |
115 self.__mqttClient.on_message = ( |
112 lambda client, userdata, message: self.onMessage.emit( |
116 lambda client, userdata, message: self.onMessage.emit( |
113 message.topic, message.payload, message.qos, message.retain)) |
117 message.topic, message.payload, message.qos, message.retain)) |
114 self.__mqttClient.on_publish = ( |
118 self.__mqttClient.on_publish = ( |
115 lambda client, userdata, mid: self.onPublish.emit(mid)) |
119 lambda client, userdata, mid: self.onPublish.emit(mid)) |
|
120 # TODO: add properties to signal |
|
121 # TODO: MQTTv5: add support for MQTTv5 signature |
116 self.__mqttClient.on_subscribe = ( |
122 self.__mqttClient.on_subscribe = ( |
117 lambda client, userdata, mid, grantedQos: self.onSubscribe.emit( |
123 lambda client, userdata, mid, grantedQos, properties: |
118 mid, grantedQos)) |
124 self.onSubscribe.emit(mid, grantedQos)) |
|
125 # TODO: MQTTv5: add support for MQTTv5 signature |
119 self.__mqttClient.on_unsubscribe = ( |
126 self.__mqttClient.on_unsubscribe = ( |
120 lambda client, userdata, mid: self.onUnsubscribe.emit(mid)) |
127 lambda client, userdata, mid: self.onUnsubscribe.emit(mid)) |
121 |
128 |
122 @pyqtSlot() |
129 @pyqtSlot() |
123 def __connectTimeout(self): |
130 def __connectTimeout(self): |
405 """ |
415 """ |
406 Public method to disconnect the client from the remote broker. |
416 Public method to disconnect the client from the remote broker. |
407 """ |
417 """ |
408 self.__connectTimeoutTimer.stop() |
418 self.__connectTimeoutTimer.stop() |
409 |
419 |
|
420 # TODO: MQTTv5: add support for properties (?) |
|
421 # TODO: MQTTv5: add support for reason code |
410 self.__mqttClient.disconnect() |
422 self.__mqttClient.disconnect() |
411 |
423 |
|
424 # TODO: MQTTv5: add support for properties |
|
425 # TODO: MQTTv5: add support for subscribe options |
412 def subscribe(self, topic, qos=0): |
426 def subscribe(self, topic, qos=0): |
413 """ |
427 """ |
414 Public method to subscribe to topics with quality of service. |
428 Public method to subscribe to topics with quality of service. |
415 |
429 |
416 @param topic single topic to subscribe to or a tuple with a topic |
430 @param topic single topic to subscribe to or a tuple with a topic |
432 @return tuple containing the result code and the message ID |
447 @return tuple containing the result code and the message ID |
433 @rtype tuple of (int, int) |
448 @rtype tuple of (int, int) |
434 """ |
449 """ |
435 return self.__mqttClient.unsubscribe(topic) |
450 return self.__mqttClient.unsubscribe(topic) |
436 |
451 |
|
452 # TODO: MQTTv5: add support for properties |
437 def publish(self, topic, payload=None, qos=0, retain=False): |
453 def publish(self, topic, payload=None, qos=0, retain=False): |
438 """ |
454 """ |
439 Public method to publish to a topic. |
455 Public method to publish to a topic. |
440 |
456 |
441 @param topic topic to publish to |
457 @param topic topic to publish to |