From 285e0da7adc6257703d4d41dba7c82ddef4889f7 Mon Sep 17 00:00:00 2001 From: brendan <2bndy5@gmail.com> Date: Mon, 20 Jul 2020 18:31:51 -0700 Subject: [PATCH 1/5] adjust fixed_header init; (-) loop_forever() --- adafruit_minimqtt/adafruit_minimqtt.py | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-) diff --git a/adafruit_minimqtt/adafruit_minimqtt.py b/adafruit_minimqtt/adafruit_minimqtt.py index 656c8c0..691f9a3 100755 --- a/adafruit_minimqtt/adafruit_minimqtt.py +++ b/adafruit_minimqtt/adafruit_minimqtt.py @@ -303,8 +303,7 @@ def connect(self, clean_session=True): raise MMQTTException("Invalid broker address defined.", e) # Fixed Header - fixed_header = bytearray() - fixed_header.append(0x10) + fixed_header = bytearray([0x10]) # NOTE: Variable header is # MQTT_HDR_CONNECT = bytearray(b"\x04MQTT\x04\x02\0\0") @@ -688,21 +687,6 @@ def reconnect(self, resub_topics=True): feed = subscribed_topics.pop() self.subscribe(feed) - def loop_forever(self): - """Starts a blocking message loop. Use this - method if you want to run a program forever. - Code below a call to this method will NOT execute. - - .. note:: This method is depreciated and will be removed in the - next major release. Please see - `examples/minimqtt_pub_sub_blocking.py `_ - for an example of creating a blocking loop which can handle wireless - network events. - """ - while True: - if self._sock.connected: - self.loop() - def loop(self): """Non-blocking message loop. Use this method to check incoming subscription messages. From 1cad196e30ef9f17f878103397cb8df5b92f26cc Mon Sep 17 00:00:00 2001 From: brendan <2bndy5@gmail.com> Date: Sat, 25 Jul 2020 21:18:36 -0700 Subject: [PATCH 2/5] pub_hdr_fixed & pub_hdr_var simplified init --- adafruit_minimqtt/adafruit_minimqtt.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/adafruit_minimqtt/adafruit_minimqtt.py b/adafruit_minimqtt/adafruit_minimqtt.py index 691f9a3..1cf4d91 100755 --- a/adafruit_minimqtt/adafruit_minimqtt.py +++ b/adafruit_minimqtt/adafruit_minimqtt.py @@ -460,13 +460,11 @@ def publish(self, topic, msg, retain=False, qos=0): 0 <= qos <= 1 ), "Quality of Service Level 2 is unsupported by this library." - pub_hdr_fixed = bytearray() # fixed header - pub_hdr_fixed.extend(MQTT_PUB) - pub_hdr_fixed[0] |= retain | qos << 1 # [3.3.1.2], [3.3.1.3] + # fixed header. [3.3.1.2], [3.3.1.3] + pub_hdr_fixed = bytearray([MQTT_PUB[0] | retain | qos << 1]) - pub_hdr_var = bytearray() # variable header - pub_hdr_var.append(len(topic) >> 8) # Topic length, MSB - pub_hdr_var.append(len(topic) & 0xFF) # Topic length, LSB + # variable header = 2-byte Topic length (big endian) + pub_hdr_var = struct.pack('>H', len(topic)) pub_hdr_var.extend(topic.encode("utf-8")) # Topic name remaining_length = 2 + len(msg) + len(topic) From 044d06c19151ff6f59fba6ee6dae3e74b502f0c2 Mon Sep 17 00:00:00 2001 From: brendan <2bndy5@gmail.com> Date: Sun, 26 Jul 2020 05:24:45 -0700 Subject: [PATCH 3/5] pleasing black --- adafruit_minimqtt/adafruit_minimqtt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_minimqtt/adafruit_minimqtt.py b/adafruit_minimqtt/adafruit_minimqtt.py index 1cf4d91..f893972 100755 --- a/adafruit_minimqtt/adafruit_minimqtt.py +++ b/adafruit_minimqtt/adafruit_minimqtt.py @@ -464,7 +464,7 @@ def publish(self, topic, msg, retain=False, qos=0): pub_hdr_fixed = bytearray([MQTT_PUB[0] | retain | qos << 1]) # variable header = 2-byte Topic length (big endian) - pub_hdr_var = struct.pack('>H', len(topic)) + pub_hdr_var = struct.pack(">H", len(topic)) pub_hdr_var.extend(topic.encode("utf-8")) # Topic name remaining_length = 2 + len(msg) + len(topic) From d938bc0cbcab3d7fe3ead9744374f0dca5c139a6 Mon Sep 17 00:00:00 2001 From: brendan <2bndy5@gmail.com> Date: Tue, 28 Jul 2020 19:28:26 -0700 Subject: [PATCH 4/5] (-) MQTT_PUB const; fixed pub_hdr_var.append() --- adafruit_minimqtt/adafruit_minimqtt.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/adafruit_minimqtt/adafruit_minimqtt.py b/adafruit_minimqtt/adafruit_minimqtt.py index f893972..79b1b47 100755 --- a/adafruit_minimqtt/adafruit_minimqtt.py +++ b/adafruit_minimqtt/adafruit_minimqtt.py @@ -62,7 +62,6 @@ MQTT_PINGRESP = const(0xD0) MQTT_SUB = b"\x82" MQTT_UNSUB = b"\xA2" -MQTT_PUB = bytearray(b"\x30") MQTT_DISCONNECT = b"\xe0\0" # Variable CONNECT header [MQTT 3.1.2] @@ -461,11 +460,11 @@ def publish(self, topic, msg, retain=False, qos=0): ), "Quality of Service Level 2 is unsupported by this library." # fixed header. [3.3.1.2], [3.3.1.3] - pub_hdr_fixed = bytearray([MQTT_PUB[0] | retain | qos << 1]) + pub_hdr_fixed = bytearray([0x30 | retain | qos << 1]) # variable header = 2-byte Topic length (big endian) - pub_hdr_var = struct.pack(">H", len(topic)) - pub_hdr_var.extend(topic.encode("utf-8")) # Topic name + pub_hdr_var = bytearray(struct.pack(">H", len(topic))) + pub_hdr_var.append(topic.encode("utf-8")) # Topic name remaining_length = 2 + len(msg) + len(topic) if qos > 0: From 2c790c0a06b8ce10ae62b9291be06dbabe8ecb09 Mon Sep 17 00:00:00 2001 From: brendan <2bndy5@gmail.com> Date: Tue, 28 Jul 2020 19:56:10 -0700 Subject: [PATCH 5/5] extend() actually works better with a bytes object --- adafruit_minimqtt/adafruit_minimqtt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_minimqtt/adafruit_minimqtt.py b/adafruit_minimqtt/adafruit_minimqtt.py index 79b1b47..669dfc1 100755 --- a/adafruit_minimqtt/adafruit_minimqtt.py +++ b/adafruit_minimqtt/adafruit_minimqtt.py @@ -464,7 +464,7 @@ def publish(self, topic, msg, retain=False, qos=0): # variable header = 2-byte Topic length (big endian) pub_hdr_var = bytearray(struct.pack(">H", len(topic))) - pub_hdr_var.append(topic.encode("utf-8")) # Topic name + pub_hdr_var.extend(topic.encode("utf-8")) # Topic name remaining_length = 2 + len(msg) + len(topic) if qos > 0: