Skip to content

Commit f93feae

Browse files
brentrubrentru
authored andcommitted
remove variable timeout, blocking read on wait_for_msg by default
1 parent 6405286 commit f93feae

File tree

1 file changed

+21
-23
lines changed

1 file changed

+21
-23
lines changed

adafruit_minimqtt.py

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@
3838
* Adafruit CircuitPython firmware for the supported boards:
3939
https://github.com/adafruit/circuitpython/releases
4040
41-
* Adafruit CircuitPython Logger
42-
https://github.com/adafruit/Adafruit_CircuitPython_Logger
43-
4441
"""
4542
import struct
4643
import time
@@ -78,15 +75,15 @@
7875
const(0x04) : 'Connection Refused - Incorrect username/password',
7976
const(0x05) : 'Connection Refused - Unauthorized'}
8077

81-
# pylint: disable=unnecessary-pass
8278
class MMQTTException(Exception):
8379
"""MiniMQTT Exception class."""
80+
# pylint: disable=unnecessary-pass
8481
#pass
8582

8683
class MQTT:
8784
"""
8885
MQTT client interface for CircuitPython devices.
89-
:param socket: Socket object for network interface.
86+
:param socket: Socket object for provided network interface
9087
:param str broker: MQTT Broker URL or IP Address.
9188
:param int port: Optional port definition, defaults to 8883.
9289
:param str username: Username for broker authentication.
@@ -97,7 +94,7 @@ class MQTT:
9794
Defaults to True (port 8883).
9895
:param bool log: Attaches a logger to the MQTT client, defaults to logging level INFO.
9996
"""
100-
# pylint: disable=too-many-arguments,too-many-instance-attributes, not-callable, invalid-name, no-member
97+
# pylint: disable=too-many-arguments,too-many-instance-attributes, not-callable, invalid-name
10198
def __init__(self, socket, broker, port=None, username=None,
10299
password=None, esp=None, client_id=None, is_ssl=True, log=False):
103100
# network interface
@@ -132,6 +129,7 @@ def __init__(self, socket, broker, port=None, username=None,
132129
self._client_id = client_id
133130
else:
134131
# assign a unique client_id
132+
# pylint: disable=no-member
135133
self._client_id = 'cpy{0}{1}'.format(microcontroller.cpu.uid[randint(0, 15)],
136134
randint(0, 9))
137135
# generated client_id's enforce spec.'s length rules
@@ -368,7 +366,7 @@ def publish(self, topic, msg, retain=False, qos=0):
368366
sz += 2
369367
assert sz < const(2097152)
370368
i = 1
371-
while sz > 0x7f:
369+
while sz > const(0x7f):
372370
pkt[i] = (sz & 0x7f) | const(0x80)
373371
sz >>= 7
374372
i += 1
@@ -523,23 +521,23 @@ def unsubscribe(self, topic):
523521
if self._logger is not None:
524522
self._logger.debug('Waiting for UNSUBACK...')
525523
while 1:
526-
self.wait_for_msg()
527-
return_code = self._sock.read(4)
528-
assert return_code[1] == const(0x02)
529-
# [MQTT-3.32]
530-
assert return_code[2] == packet_id_bytes[0] and return_code[3] == packet_id_bytes[1]
531-
for t in topics:
532-
if self.on_unsubscribe is not None:
533-
self.on_unsubscribe(self, self._user_data, t, self._pid)
534-
self._subscribed_topics.remove(t)
535-
return
536-
537-
def wait_for_msg(self, timeout=0.1):
538-
"""Reads and processes network events. Returns network response if successful.
539-
:param float timeout: The time in seconds to wait for network before returning.
540-
Setting this to 0.0 will cause the socket to block until it reads.
524+
op = self.wait_for_msg()
525+
if op == const(176):
526+
return_code = self._sock.read(3)
527+
assert return_code[0] == const(0x02)
528+
# [MQTT-3.32]
529+
assert return_code[1] == packet_id_bytes[0] and return_code[2] == packet_id_bytes[1]
530+
for t in topics:
531+
if self.on_unsubscribe is not None:
532+
self.on_unsubscribe(self, self._user_data, t, self._pid)
533+
self._subscribed_topics.remove(t)
534+
return
535+
536+
def wait_for_msg(self):
537+
"""Reads and processes network events.
538+
Returns response code if successful.
541539
"""
542-
self._sock.settimeout(timeout)
540+
self._sock.settimeout(0.0)
543541
res = self._sock.read(1)
544542
if res in [None, b""]:
545543
return None

0 commit comments

Comments
 (0)