Skip to content

Commit f4233d0

Browse files
authored
Merge pull request #46 from 2bndy5/remove-loop_forever()
adjust fixed_header init; (-) loop_forever()
2 parents 237373f + 2c790c0 commit f4233d0

File tree

1 file changed

+5
-24
lines changed

1 file changed

+5
-24
lines changed

adafruit_minimqtt/adafruit_minimqtt.py

+5-24
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@
6262
MQTT_PINGRESP = const(0xD0)
6363
MQTT_SUB = b"\x82"
6464
MQTT_UNSUB = b"\xA2"
65-
MQTT_PUB = bytearray(b"\x30")
6665
MQTT_DISCONNECT = b"\xe0\0"
6766

6867
# Variable CONNECT header [MQTT 3.1.2]
@@ -303,8 +302,7 @@ def connect(self, clean_session=True):
303302
raise MMQTTException("Invalid broker address defined.", e)
304303

305304
# Fixed Header
306-
fixed_header = bytearray()
307-
fixed_header.append(0x10)
305+
fixed_header = bytearray([0x10])
308306

309307
# NOTE: Variable header is
310308
# MQTT_HDR_CONNECT = bytearray(b"\x04MQTT\x04\x02\0\0")
@@ -461,13 +459,11 @@ def publish(self, topic, msg, retain=False, qos=0):
461459
0 <= qos <= 1
462460
), "Quality of Service Level 2 is unsupported by this library."
463461

464-
pub_hdr_fixed = bytearray() # fixed header
465-
pub_hdr_fixed.extend(MQTT_PUB)
466-
pub_hdr_fixed[0] |= retain | qos << 1 # [3.3.1.2], [3.3.1.3]
462+
# fixed header. [3.3.1.2], [3.3.1.3]
463+
pub_hdr_fixed = bytearray([0x30 | retain | qos << 1])
467464

468-
pub_hdr_var = bytearray() # variable header
469-
pub_hdr_var.append(len(topic) >> 8) # Topic length, MSB
470-
pub_hdr_var.append(len(topic) & 0xFF) # Topic length, LSB
465+
# variable header = 2-byte Topic length (big endian)
466+
pub_hdr_var = bytearray(struct.pack(">H", len(topic)))
471467
pub_hdr_var.extend(topic.encode("utf-8")) # Topic name
472468

473469
remaining_length = 2 + len(msg) + len(topic)
@@ -688,21 +684,6 @@ def reconnect(self, resub_topics=True):
688684
feed = subscribed_topics.pop()
689685
self.subscribe(feed)
690686

691-
def loop_forever(self):
692-
"""Starts a blocking message loop. Use this
693-
method if you want to run a program forever.
694-
Code below a call to this method will NOT execute.
695-
696-
.. note:: This method is depreciated and will be removed in the
697-
next major release. Please see
698-
`examples/minimqtt_pub_sub_blocking.py <examples.html#basic-forever-loop>`_
699-
for an example of creating a blocking loop which can handle wireless
700-
network events.
701-
"""
702-
while True:
703-
if self._sock.connected:
704-
self.loop()
705-
706687
def loop(self):
707688
"""Non-blocking message loop. Use this method to
708689
check incoming subscription messages.

0 commit comments

Comments
 (0)