79
79
_the_interface = None # pylint: disable=invalid-name
80
80
_the_sock = None # pylint: disable=invalid-name
81
81
82
+
82
83
class MMQTTException (Exception ):
83
84
"""MiniMQTT Exception class."""
84
85
@@ -95,10 +96,11 @@ def set_socket(sock, iface=None):
95
96
global _the_sock # pylint: disable=invalid-name, global-statement
96
97
_the_sock = sock
97
98
if iface :
98
- global _the_interface # pylint: disable=invalid-name, global-statement
99
+ global _the_interface # pylint: disable=invalid-name, global-statement
99
100
_the_interface = iface
100
101
_the_sock .set_interface (iface )
101
102
103
+
102
104
class MQTT :
103
105
"""MQTT Client for CircuitPython
104
106
:param str broker: MQTT Broker URL or IP Address.
@@ -114,14 +116,22 @@ class MQTT:
114
116
"""
115
117
116
118
# pylint: disable=too-many-arguments,too-many-instance-attributes, not-callable, invalid-name, no-member
117
- def __init__ (self , broker , port = None , username = None ,
118
- password = None , client_id = None ,
119
- is_ssl = True , log = False , keep_alive = 60 ):
119
+ def __init__ (
120
+ self ,
121
+ broker ,
122
+ port = None ,
123
+ username = None ,
124
+ password = None ,
125
+ client_id = None ,
126
+ is_ssl = True ,
127
+ log = False ,
128
+ keep_alive = 60 ,
129
+ ):
120
130
self ._sock = None
121
131
# broker
122
- try : # set broker IP
132
+ try : # set broker IP
123
133
self .broker = _the_interface .unpretty_ip (broker )
124
- except ValueError : # set broker URL
134
+ except ValueError : # set broker URL
125
135
self .broker = broker
126
136
# port/ssl
127
137
self .port = MQTT_TCP_PORT
@@ -230,20 +240,26 @@ def connect(self, clean_session=True):
230
240
self .broker , port = self .broker .split (":" , 1 )
231
241
port = int (port )
232
242
233
- addr = _the_sock .getaddrinfo (self .broker , self .port , 0 , _the_sock .SOCK_STREAM )[0 ]
243
+ addr = _the_sock .getaddrinfo (self .broker , self .port , 0 , _the_sock .SOCK_STREAM )[
244
+ 0
245
+ ]
234
246
self ._sock = _the_sock .socket (addr [0 ], addr [1 ], addr [2 ])
235
247
self ._sock .settimeout (15 )
236
248
if self .port == 8883 :
237
249
try :
238
250
if self .logger is not None :
239
- self .logger .debug ('Attempting to establish secure MQTT connection...' )
251
+ self .logger .debug (
252
+ "Attempting to establish secure MQTT connection..."
253
+ )
240
254
self ._sock .connect ((self .broker , self .port ), _the_interface .TLS_MODE )
241
255
except RuntimeError as e :
242
256
raise MMQTTException ("Invalid broker address defined." , e )
243
257
else :
244
258
try :
245
259
if self .logger is not None :
246
- self .logger .debug ('Attempting to establish insecure MQTT connection...' )
260
+ self .logger .debug (
261
+ "Attempting to establish insecure MQTT connection..."
262
+ )
247
263
self ._sock .connect (addr [- 1 ], TCP_MODE )
248
264
except RuntimeError as e :
249
265
raise MMQTTException ("Invalid broker address defined." , e )
@@ -386,9 +402,9 @@ def publish(self, topic, msg, retain=False, qos=0):
386
402
raise MMQTTException ("Publish topic can not contain wildcards." )
387
403
# check msg/qos kwargs
388
404
if msg is None :
389
- raise MMQTTException (' Message can not be None.' )
405
+ raise MMQTTException (" Message can not be None." )
390
406
if isinstance (msg , (int , float )):
391
- msg = str (msg ).encode (' ascii' )
407
+ msg = str (msg ).encode (" ascii" )
392
408
elif isinstance (msg , str ):
393
409
msg = str (msg ).encode ("utf-8" )
394
410
else :
@@ -628,8 +644,10 @@ def loop(self):
628
644
if current_time - self ._timestamp >= self .keep_alive :
629
645
# Handle KeepAlive by expecting a PINGREQ/PINGRESP from the server
630
646
if self .logger is not None :
631
- self .logger .debug ('KeepAlive period elapsed - \
632
- requesting a PINGRESP from the server...' )
647
+ self .logger .debug (
648
+ "KeepAlive period elapsed - \
649
+ requesting a PINGRESP from the server..."
650
+ )
633
651
self .ping ()
634
652
self ._timestamp = 0
635
653
self ._sock .settimeout (0.1 )
@@ -699,10 +717,10 @@ def _check_topic(topic):
699
717
raise MMQTTException ("Topic may not be NoneType" )
700
718
# [MQTT-4.7.3-1]
701
719
if not topic :
702
- raise MMQTTException (' Topic may not be empty.' )
720
+ raise MMQTTException (" Topic may not be empty." )
703
721
# [MQTT-4.7.3-3]
704
- if len (topic .encode (' utf-8' )) > MQTT_TOPIC_LENGTH_LIMIT :
705
- raise MMQTTException (' Topic length is too large.' )
722
+ if len (topic .encode (" utf-8" )) > MQTT_TOPIC_LENGTH_LIMIT :
723
+ raise MMQTTException (" Topic length is too large." )
706
724
707
725
@staticmethod
708
726
def _check_qos (qos_level ):
0 commit comments