Skip to content

add debug logging to recieved messages. #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 9, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 21 additions & 17 deletions adafruit_minimqtt/adafruit_minimqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,11 @@ def _get_connect_socket(self, host, port, *, timeout=1):
"ssl_context must be set before using adafruit_mqtt for secure MQTT."
)

if self.logger and port == MQTT_TLS_PORT:
if self.logger is not None and port == MQTT_TLS_PORT:
self.logger.info(
"Establishing a SECURE SSL connection to {0}:{1}".format(host, port)
)
elif self.logger:
elif self.logger is not None:
self.logger.info(
"Establishing an INSECURE connection to {0}:{1}".format(host, port)
)
Expand Down Expand Up @@ -348,7 +348,7 @@ def will_set(self, topic=None, payload=None, qos=0, retain=False):
:param bool retain: Specifies if the payload is to be retained when
it is published.
"""
if self.logger:
if self.logger is not None:
self.logger.debug("Setting last will properties")
self._valid_qos(qos)
if self._is_connected:
Expand Down Expand Up @@ -440,7 +440,7 @@ def connect(self, clean_session=True, host=None, port=None, keep_alive=None):
if keep_alive:
self.keep_alive = keep_alive

if self.logger:
if self.logger is not None:
self.logger.debug("Attempting to establish MQTT connection...")

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

if self.logger:
if self.logger is not None:
self.logger.debug("Sending CONNECT to broker...")
self.logger.debug(
"Fixed Header: %s\nVariable Header: %s", fixed_header, var_header
Expand All @@ -512,7 +512,7 @@ def connect(self, clean_session=True, host=None, port=None, keep_alive=None):
else:
self._send_str(self._username)
self._send_str(self._password)
if self.logger:
if self.logger is not None:
self.logger.debug("Receiving CONNACK packet from broker")
while True:
op = self._wait_for_msg()
Expand All @@ -535,7 +535,7 @@ def disconnect(self):
try:
self._sock.send(MQTT_DISCONNECT)
except RuntimeError as e:
if self.logger:
if self.logger is not None:
self.logger.warning("Unable to send DISCONNECT packet: {}".format(e))
if self.logger is not None:
self.logger.debug("Closing socket")
Expand All @@ -551,7 +551,7 @@ def ping(self):
Returns response codes of any messages received while waiting for PINGRESP.
"""
self.is_connected()
if self.logger:
if self.logger is not None:
self.logger.debug("Sending PINGREQ")
self._sock.send(MQTT_PINGREQ)
ping_timeout = self.keep_alive
Expand Down Expand Up @@ -622,7 +622,7 @@ def publish(self, topic, msg, retain=False, qos=0):
else:
pub_hdr_fixed.append(remaining_length)

if self.logger:
if self.logger is not None:
self.logger.debug(
"Sending PUBLISH\nTopic: %s\nMsg: %s\
\nQoS: %d\nRetain? %r",
Expand Down Expand Up @@ -693,7 +693,7 @@ def subscribe(self, topic, qos=0):
topic_size = len(t.encode("utf-8")).to_bytes(2, "big")
qos_byte = q.to_bytes(1, "big")
packet += topic_size + t.encode() + qos_byte
if self.logger:
if self.logger is not None:
for t, q in topics:
self.logger.debug("SUBSCRIBING to topic %s with QoS %d", t, q)
self._sock.send(packet)
Expand Down Expand Up @@ -740,11 +740,11 @@ def unsubscribe(self, topic):
for t in topics:
topic_size = len(t.encode("utf-8")).to_bytes(2, "big")
packet += topic_size + t.encode()
if self.logger:
if self.logger is not None:
for t in topics:
self.logger.debug("UNSUBSCRIBING from topic %s", t)
self._sock.send(packet)
if self.logger:
if self.logger is not None:
self.logger.debug("Waiting for UNSUBACK...")
while True:
op = self._wait_for_msg()
Expand All @@ -765,13 +765,13 @@ def reconnect(self, resub_topics=True):
:param bool resub_topics: Resubscribe to previously subscribed topics.

"""
if self.logger:
if self.logger is not None:
self.logger.debug("Attempting to reconnect with MQTT broker")
self.connect()
if self.logger:
if self.logger is not None:
self.logger.debug("Reconnected with broker")
if resub_topics:
if self.logger:
if self.logger is not None:
self.logger.debug(
"Attempting to resubscribe to previously subscribed topics."
)
Expand Down Expand Up @@ -828,7 +828,7 @@ def _wait_for_msg(self, timeout=0.1):
# If we get here, it means that there is nothing to be received
return None
if res[0] == MQTT_PINGRESP:
if self.logger:
if self.logger is not None:
self.logger.debug("Got PINGRESP")
sz = self._sock_exact_recv(1)[0]
if sz != 0x00:
Expand All @@ -853,6 +853,10 @@ def _wait_for_msg(self, timeout=0.1):
# read message contents
raw_msg = self._sock_exact_recv(sz)
msg = raw_msg if self._use_binary_mode else str(raw_msg, "utf-8")
if self.logger is not None:
self.logger.debug(
"Receiving SUBSCRIBE \nTopic: %s\nMsg: %s\n", topic, raw_msg
)
self._handle_on_message(self, topic, msg)
if res[0] & 0x06 == 0x02:
pkt = bytearray(b"\x40\x02\0\0")
Expand Down Expand Up @@ -907,7 +911,7 @@ def _sock_exact_recv(self, bufsize):
# This will timeout with socket timeout (not keepalive timeout)
rc = self._sock.recv(bufsize)
if not rc:
if self.logger:
if self.logger is not None:
self.logger.debug("_sock_exact_recv timeout")
# If no bytes waiting, raise same exception as socketpool
raise OSError(errno.ETIMEDOUT)
Expand Down