Skip to content

Commit 11f078b

Browse files
authored
Merge pull request #106 from dgriswo/subscription_logging
add debug logging to recieved messages.
2 parents 4f5f383 + 3cee708 commit 11f078b

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

adafruit_minimqtt/adafruit_minimqtt.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -237,11 +237,11 @@ def _get_connect_socket(self, host, port, *, timeout=1):
237237
"ssl_context must be set before using adafruit_mqtt for secure MQTT."
238238
)
239239

240-
if self.logger and port == MQTT_TLS_PORT:
240+
if self.logger is not None and port == MQTT_TLS_PORT:
241241
self.logger.info(
242242
"Establishing a SECURE SSL connection to {0}:{1}".format(host, port)
243243
)
244-
elif self.logger:
244+
elif self.logger is not None:
245245
self.logger.info(
246246
"Establishing an INSECURE connection to {0}:{1}".format(host, port)
247247
)
@@ -348,7 +348,7 @@ def will_set(self, topic=None, payload=None, qos=0, retain=False):
348348
:param bool retain: Specifies if the payload is to be retained when
349349
it is published.
350350
"""
351-
if self.logger:
351+
if self.logger is not None:
352352
self.logger.debug("Setting last will properties")
353353
self._valid_qos(qos)
354354
if self._is_connected:
@@ -440,7 +440,7 @@ def connect(self, clean_session=True, host=None, port=None, keep_alive=None):
440440
if keep_alive:
441441
self.keep_alive = keep_alive
442442

443-
if self.logger:
443+
if self.logger is not None:
444444
self.logger.debug("Attempting to establish MQTT connection...")
445445

446446
# Get a new socket
@@ -494,7 +494,7 @@ def connect(self, clean_session=True, host=None, port=None, keep_alive=None):
494494
fixed_header.append(remaining_length)
495495
fixed_header.append(0x00)
496496

497-
if self.logger:
497+
if self.logger is not None:
498498
self.logger.debug("Sending CONNECT to broker...")
499499
self.logger.debug(
500500
"Fixed Header: %s\nVariable Header: %s", fixed_header, var_header
@@ -512,7 +512,7 @@ def connect(self, clean_session=True, host=None, port=None, keep_alive=None):
512512
else:
513513
self._send_str(self._username)
514514
self._send_str(self._password)
515-
if self.logger:
515+
if self.logger is not None:
516516
self.logger.debug("Receiving CONNACK packet from broker")
517517
while True:
518518
op = self._wait_for_msg()
@@ -535,7 +535,7 @@ def disconnect(self):
535535
try:
536536
self._sock.send(MQTT_DISCONNECT)
537537
except RuntimeError as e:
538-
if self.logger:
538+
if self.logger is not None:
539539
self.logger.warning("Unable to send DISCONNECT packet: {}".format(e))
540540
if self.logger is not None:
541541
self.logger.debug("Closing socket")
@@ -551,7 +551,7 @@ def ping(self):
551551
Returns response codes of any messages received while waiting for PINGRESP.
552552
"""
553553
self.is_connected()
554-
if self.logger:
554+
if self.logger is not None:
555555
self.logger.debug("Sending PINGREQ")
556556
self._sock.send(MQTT_PINGREQ)
557557
ping_timeout = self.keep_alive
@@ -622,7 +622,7 @@ def publish(self, topic, msg, retain=False, qos=0):
622622
else:
623623
pub_hdr_fixed.append(remaining_length)
624624

625-
if self.logger:
625+
if self.logger is not None:
626626
self.logger.debug(
627627
"Sending PUBLISH\nTopic: %s\nMsg: %s\
628628
\nQoS: %d\nRetain? %r",
@@ -693,7 +693,7 @@ def subscribe(self, topic, qos=0):
693693
topic_size = len(t.encode("utf-8")).to_bytes(2, "big")
694694
qos_byte = q.to_bytes(1, "big")
695695
packet += topic_size + t.encode() + qos_byte
696-
if self.logger:
696+
if self.logger is not None:
697697
for t, q in topics:
698698
self.logger.debug("SUBSCRIBING to topic %s with QoS %d", t, q)
699699
self._sock.send(packet)
@@ -740,11 +740,11 @@ def unsubscribe(self, topic):
740740
for t in topics:
741741
topic_size = len(t.encode("utf-8")).to_bytes(2, "big")
742742
packet += topic_size + t.encode()
743-
if self.logger:
743+
if self.logger is not None:
744744
for t in topics:
745745
self.logger.debug("UNSUBSCRIBING from topic %s", t)
746746
self._sock.send(packet)
747-
if self.logger:
747+
if self.logger is not None:
748748
self.logger.debug("Waiting for UNSUBACK...")
749749
while True:
750750
op = self._wait_for_msg()
@@ -765,13 +765,13 @@ def reconnect(self, resub_topics=True):
765765
:param bool resub_topics: Resubscribe to previously subscribed topics.
766766
767767
"""
768-
if self.logger:
768+
if self.logger is not None:
769769
self.logger.debug("Attempting to reconnect with MQTT broker")
770770
self.connect()
771-
if self.logger:
771+
if self.logger is not None:
772772
self.logger.debug("Reconnected with broker")
773773
if resub_topics:
774-
if self.logger:
774+
if self.logger is not None:
775775
self.logger.debug(
776776
"Attempting to resubscribe to previously subscribed topics."
777777
)
@@ -828,7 +828,7 @@ def _wait_for_msg(self, timeout=0.1):
828828
# If we get here, it means that there is nothing to be received
829829
return None
830830
if res[0] == MQTT_PINGRESP:
831-
if self.logger:
831+
if self.logger is not None:
832832
self.logger.debug("Got PINGRESP")
833833
sz = self._sock_exact_recv(1)[0]
834834
if sz != 0x00:
@@ -853,6 +853,10 @@ def _wait_for_msg(self, timeout=0.1):
853853
# read message contents
854854
raw_msg = self._sock_exact_recv(sz)
855855
msg = raw_msg if self._use_binary_mode else str(raw_msg, "utf-8")
856+
if self.logger is not None:
857+
self.logger.debug(
858+
"Receiving SUBSCRIBE \nTopic: %s\nMsg: %s\n", topic, raw_msg
859+
)
856860
self._handle_on_message(self, topic, msg)
857861
if res[0] & 0x06 == 0x02:
858862
pkt = bytearray(b"\x40\x02\0\0")
@@ -907,7 +911,7 @@ def _sock_exact_recv(self, bufsize):
907911
# This will timeout with socket timeout (not keepalive timeout)
908912
rc = self._sock.recv(bufsize)
909913
if not rc:
910-
if self.logger:
914+
if self.logger is not None:
911915
self.logger.debug("_sock_exact_recv timeout")
912916
# If no bytes waiting, raise same exception as socketpool
913917
raise OSError(errno.ETIMEDOUT)

0 commit comments

Comments
 (0)