Skip to content

Commit 1ae11a8

Browse files
author
brentru
committed
changes per @ladyada's review
1 parent ceb26b6 commit 1ae11a8

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

adafruit_minimqtt.py

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,8 @@ def reconnect(self, retries=30, resub_topics=True):
201201
feed = self._subscribed_topics.pop()
202202
self.subscribe(feed)
203203
except OSError as e:
204-
print('Failed to connect to the broker, retrying\n', e)
204+
if self._logger is not None:
205+
self._logger.debug('Lost connection, reconnecting and resubscribing...', e)
205206
retries += 1
206207
if retries >= 30:
207208
retries = 0
@@ -279,8 +280,8 @@ def connect(self, clean_session=True):
279280
op = self._wait_for_msg()
280281
if op == 32:
281282
rc = self._sock.read(3)
282-
assert rc[0] == const(0x02)
283-
if rc[2] != const(0x00):
283+
assert rc[0] == 0x02
284+
if rc[2] != 0x00:
284285
raise MMQTTException(CONNACK_ERRORS[rc[3]])
285286
self._is_connected = True
286287
result = rc[0] & 1
@@ -313,11 +314,11 @@ def ping(self):
313314
self._sock.write(MQTT_PINGREQ)
314315
if self._logger is not None:
315316
self._logger.debug('Checking PINGRESP')
316-
while 1:
317+
while True:
317318
op = self._wait_for_msg(0.5)
318-
if op == const(208):
319+
if op == 208:
319320
ping_resp = self._sock.read(2)
320-
if ping_resp[0] != const(0x00):
321+
if ping_resp[0] != 0x00:
321322
raise MMQTTException('PINGRESP not returned from broker.')
322323
return
323324

@@ -368,10 +369,10 @@ def publish(self, topic, msg, retain=False, qos=0):
368369
sz = 2 + len(topic) + len(msg)
369370
if qos > 0:
370371
sz += 2
371-
assert sz < const(2097152)
372+
assert sz < 2097152
372373
i = 1
373-
while sz > const(0x7f):
374-
pkt[i] = (sz & 0x7f) | const(0x80)
374+
while sz > 0x7f:
375+
pkt[i] = (sz & 0x7f) | 0x80
375376
sz >>= 7
376377
i += 1
377378
pkt[i] = sz
@@ -394,13 +395,13 @@ def publish(self, topic, msg, retain=False, qos=0):
394395
self._logger.debug('Sending PUBACK')
395396
self._sock.write(msg)
396397
if qos == 1:
397-
while 1:
398+
while True:
398399
op = self._wait_for_msg()
399-
if op == const(0x40):
400+
if op == 0x40:
400401
sz = self._sock.read(1)
401402
assert sz == b"\x02"
402403
rcv_pid = self._sock.read(2)
403-
rcv_pid = rcv_pid[0] << const(0x08) | rcv_pid[1]
404+
rcv_pid = rcv_pid[0] << 0x08 | rcv_pid[1]
404405
if pid == rcv_pid:
405406
if self.on_publish is not None:
406407
self.on_publish(self, self._user_data, topic, rcv_pid)
@@ -472,7 +473,7 @@ def subscribe(self, topic, qos=0):
472473
for t, q in topics:
473474
self._logger.debug('SUBSCRIBING to topic {0} with QoS {1}'.format(t, q))
474475
self._sock.write(packet)
475-
while 1:
476+
while True:
476477
op = self._wait_for_msg()
477478
if op == 0x90:
478479
rc = self._sock.read(4)
@@ -529,11 +530,11 @@ def unsubscribe(self, topic):
529530
self._sock.write(packet)
530531
if self._logger is not None:
531532
self._logger.debug('Waiting for UNSUBACK...')
532-
while 1:
533+
while True:
533534
op = self._wait_for_msg()
534-
if op == const(176):
535+
if op == 176:
535536
return_code = self._sock.read(3)
536-
assert return_code[0] == const(0x02)
537+
assert return_code[0] == 0x02
537538
# [MQTT-3.32]
538539
assert return_code[1] == packet_id_bytes[0] and return_code[2] == packet_id_bytes[1]
539540
for t in topics:
@@ -580,22 +581,22 @@ def _wait_for_msg(self, timeout=30):
580581
sz = self._sock.read(1)[0]
581582
assert sz == 0
582583
return None
583-
if res[0] & const(0xf0) != const(0x30):
584+
if res[0] & 0xf0 != 0x30:
584585
return res[0]
585586
sz = self._recv_len()
586587
topic_len = self._sock.read(2)
587588
topic_len = (topic_len[0] << 8) | topic_len[1]
588589
topic = self._sock.read(topic_len)
589590
topic = str(topic, 'utf-8')
590591
sz -= topic_len + 2
591-
if res[0] & const(0x06):
592+
if res[0] & 0x06:
592593
pid = self._sock.read(2)
593-
pid = pid[0] << const(0x08) | pid[1]
594-
sz -= const(0x02)
594+
pid = pid[0] << 0x08 | pid[1]
595+
sz -= 0x02
595596
msg = self._sock.read(sz)
596597
if self.on_message is not None:
597598
self.on_message(self, topic, str(msg, 'utf-8'))
598-
if res[0] & const(0x06) == const(0x02):
599+
if res[0] & 0x06 == 0x02:
599600
pkt = bytearray(b"\x40\x02\0\0")
600601
struct.pack_into("!H", pkt, 2, pid)
601602
self._sock.write(pkt)
@@ -606,7 +607,7 @@ def _wait_for_msg(self, timeout=30):
606607
def _recv_len(self):
607608
n = 0
608609
sh = 0
609-
while 1:
610+
while True:
610611
b = self._sock.read(1)[0]
611612
n |= (b & 0x7f) << sh
612613
if not b & 0x80:

0 commit comments

Comments
 (0)