MqttMonitor/MqttClient.py

branch
eric7
changeset 132
b4fde9ee3d83
parent 131
899c7cb866a2
child 138
871d54655ef1
equal deleted inserted replaced
131:899c7cb866a2 132:b4fde9ee3d83
154 self.__mqttClient.on_message = self.__onMessageV5 154 self.__mqttClient.on_message = self.__onMessageV5
155 155
156 self.__mqttClient.on_log = self.__onLog 156 self.__mqttClient.on_log = self.__onLog
157 self.__mqttClient.on_publish = self.__onPublish 157 self.__mqttClient.on_publish = self.__onPublish
158 158
159 def __onConnectV3(self, client, userdata, flags, rc, properties=None): 159 def __onConnectV3(self, client, userdata, flags, rc, properties=None): # noqa: U100
160 """ 160 """
161 Private method to handle the connect to the broker (MQTT v3.1 and v3.1.1). 161 Private method to handle the connect to the broker (MQTT v3.1 and v3.1.1).
162 162
163 @param client reference to the client object 163 @param client reference to the client object
164 @type paho.mqtt.Client 164 @type paho.mqtt.Client
171 @param properties optional properties (defaults to None) 171 @param properties optional properties (defaults to None)
172 @type dict (optional) 172 @type dict (optional)
173 """ 173 """
174 self.onConnectV3.emit(flags, rc) 174 self.onConnectV3.emit(flags, rc)
175 175
176 def __onDisconnectedV3(self, client, userdata, rc): 176 def __onDisconnectedV3(self, client, userdata, rc): # noqa: U100
177 """ 177 """
178 Private method to handle the disconnect from the broker (MQTT v3.1 and v3.1.1). 178 Private method to handle the disconnect from the broker (MQTT v3.1 and v3.1.1).
179 179
180 @param client reference to the client object 180 @param client reference to the client object
181 @type paho.mqtt.Client 181 @type paho.mqtt.Client
184 @param rc result code 184 @param rc result code
185 @type int 185 @type int
186 """ 186 """
187 self.onDisconnectedV3.emit(rc) 187 self.onDisconnectedV3.emit(rc)
188 188
189 def __onSubscribeV3(self, client, userdata, mid, grantedQos): 189 def __onSubscribeV3(self, client, userdata, mid, grantedQos): # noqa: U100
190 """ 190 """
191 Private method to handle a subscribe event (MQTT v3.1 and v3.1.1). 191 Private method to handle a subscribe event (MQTT v3.1 and v3.1.1).
192 192
193 @param client reference to the client object 193 @param client reference to the client object
194 @type paho.mqtt.Client 194 @type paho.mqtt.Client
199 @param grantedQos list of granted QoS for each subscription request 199 @param grantedQos list of granted QoS for each subscription request
200 @type list of int 200 @type list of int
201 """ 201 """
202 self.onSubscribeV3.emit(mid, grantedQos) 202 self.onSubscribeV3.emit(mid, grantedQos)
203 203
204 def __onUnsubscribeV3(self, client, userdata, mid): 204 def __onUnsubscribeV3(self, client, userdata, mid): # noqa: U100
205 """ 205 """
206 Private method to handle an unsubscribe event (MQTT v3.1 and v3.1.1). 206 Private method to handle an unsubscribe event (MQTT v3.1 and v3.1.1).
207 207
208 @param client reference to the client object 208 @param client reference to the client object
209 @type paho.mqtt.Client 209 @type paho.mqtt.Client
212 @param mid message ID 212 @param mid message ID
213 @type int 213 @type int
214 """ 214 """
215 self.onUnsubscribeV3.emit(mid) 215 self.onUnsubscribeV3.emit(mid)
216 216
217 def __onMessageV3(self, client, userdata, message): 217 def __onMessageV3(self, client, userdata, message): # noqa: U100
218 """ 218 """
219 Private method to handle a new message received from the broker (MQTT v3.1 219 Private method to handle a new message received from the broker (MQTT v3.1
220 and v3.1.1). 220 and v3.1.1).
221 221
222 @param client reference to the client object 222 @param client reference to the client object
228 """ 228 """
229 self.onMessageV3.emit( 229 self.onMessageV3.emit(
230 message.topic, message.payload, message.qos, message.retain 230 message.topic, message.payload, message.qos, message.retain
231 ) 231 )
232 232
233 def __onConnectV5(self, client, userdata, flags, rc, properties=None): 233 def __onConnectV5(self, client, userdata, flags, rc, properties=None): # noqa: U100
234 """ 234 """
235 Private method to handle the connect to the broker (MQTT v5.0). 235 Private method to handle the connect to the broker (MQTT v5.0).
236 236
237 @param client reference to the client object 237 @param client reference to the client object
238 @type paho.mqtt.Client 238 @type paho.mqtt.Client
250 rc.value, 250 rc.value,
251 rc.packetType, 251 rc.packetType,
252 properties.json() if properties is not None else {}, 252 properties.json() if properties is not None else {},
253 ) 253 )
254 254
255 def __onDisconnectedV5(self, client, userdata, rc, properties=None): 255 def __onDisconnectedV5(self, client, userdata, rc, properties=None): # noqa: U100
256 """ 256 """
257 Private method to handle the disconnect from the broker (MQTT v5.0). 257 Private method to handle the disconnect from the broker (MQTT v5.0).
258 258
259 @param client reference to the client object 259 @param client reference to the client object
260 @type paho.mqtt.Client 260 @type paho.mqtt.Client
272 packetType = rc.packetType 272 packetType = rc.packetType
273 resultCode = rc.value 273 resultCode = rc.value
274 self.onDisconnectedV5.emit(resultCode, packetType) 274 self.onDisconnectedV5.emit(resultCode, packetType)
275 275
276 def __onSubscribeV5(self, client, userdata, mid, reasonCodes, properties=None): 276 def __onSubscribeV5(self, client, userdata, mid, reasonCodes, properties=None):
277 # noqa: U100
277 """ 278 """
278 Private method to handle a subscribe event (MQTT v5.0). 279 Private method to handle a subscribe event (MQTT v5.0).
279 280
280 @param client reference to the client object 281 @param client reference to the client object
281 @type paho.mqtt.Client 282 @type paho.mqtt.Client
293 reasonCodes, 294 reasonCodes,
294 properties.json() if properties is not None else {}, 295 properties.json() if properties is not None else {},
295 ) 296 )
296 297
297 def __onUnsubscribeV5(self, client, userdata, mid, properties, reasonCodes): 298 def __onUnsubscribeV5(self, client, userdata, mid, properties, reasonCodes):
299 # noqa: U100
298 """ 300 """
299 Private method to handle an unsubscribe event (MQTT v5.0). 301 Private method to handle an unsubscribe event (MQTT v5.0).
300 302
301 @param client reference to the client object 303 @param client reference to the client object
302 @type paho.mqtt.Client 304 @type paho.mqtt.Client
314 reasonCodes.value, 316 reasonCodes.value,
315 reasonCodes.packetType, 317 reasonCodes.packetType,
316 properties.json() if properties is not None else {}, 318 properties.json() if properties is not None else {},
317 ) 319 )
318 320
319 def __onMessageV5(self, client, userdata, message): 321 def __onMessageV5(self, client, userdata, message): # noqa: U100
320 """ 322 """
321 Private method to handle a new message received from the broker (MQTT v5.0). 323 Private method to handle a new message received from the broker (MQTT v5.0).
322 324
323 @param client reference to the client object 325 @param client reference to the client object
324 @type paho.mqtt.Client 326 @type paho.mqtt.Client
333 message.qos, 335 message.qos,
334 message.retain, 336 message.retain,
335 message.properties.json(), 337 message.properties.json(),
336 ) 338 )
337 339
338 def __onLog(self, client, userdata, level, buf): 340 def __onLog(self, client, userdata, level, buf): # noqa: U100
339 """ 341 """
340 Private method to handle a log event (MQTT v3.1, v3.1.1 and v5.0). 342 Private method to handle a log event (MQTT v3.1, v3.1.1 and v5.0).
341 343
342 @param client reference to the client object 344 @param client reference to the client object
343 @type paho.mqtt.Client 345 @type paho.mqtt.Client
348 @param buf log message 350 @param buf log message
349 @type str 351 @type str
350 """ 352 """
351 self.onLog.emit(level, buf) 353 self.onLog.emit(level, buf)
352 354
353 def __onPublish(self, client, userdata, mid): 355 def __onPublish(self, client, userdata, mid): # noqa: U100
354 """ 356 """
355 Private method to handle the publishing of a message (MQTT v3.1, v3.1.1 357 Private method to handle the publishing of a message (MQTT v3.1, v3.1.1
356 and v5.0). 358 and v5.0).
357 359
358 @param client reference to the client object 360 @param client reference to the client object
466 try: 468 try:
467 self.__mqttClient.tls_set( 469 self.__mqttClient.tls_set(
468 ca_certs=caCerts, certfile=certFile, keyfile=keyFile 470 ca_certs=caCerts, certfile=certFile, keyfile=keyFile
469 ) 471 )
470 return True, "" 472 return True, ""
471 except (ValueError, FileNotFoundError) as err: 473 except (FileNotFoundError, ValueError) as err:
472 return False, str(err) 474 return False, str(err)
473 475
474 return False, "unspecific error occurred" 476 return False, "unspecific error occurred"
475 477
476 def getProtocol(self): 478 def getProtocol(self):

eric ide

mercurial