Skip to content

Commit

Permalink
refactor is_connected method, use it as a universal check before netw…
Browse files Browse the repository at this point in the history
…ork actions
  • Loading branch information
brentru committed Jul 8, 2019
1 parent 972f995 commit 41bc399
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions adafruit_minimqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
MQTT_PINGRESP = b'\xd0'
MQTT_SUB = b'\x82'
MQTT_UNSUB = b'\xA2'
#MQTT_UNSUB = bytearray(b'\xA2\0\0\0')
MQTT_PUB = bytearray(b'\x30\0')
MQTT_CON = bytearray(b'\x10\0\0')
# Variable CONNECT header [MQTT 3.1.2]
Expand Down Expand Up @@ -190,6 +189,8 @@ def reconnect(self, retries=30, resub_topics=False):

def is_connected(self):
"""Returns MQTT client session status."""
if self._sock is None or self._is_connected is False:
raise MMQTTException("MiniMQTT is not connected.")
return self._is_connected

# Core MQTT Methods
Expand Down Expand Up @@ -267,8 +268,7 @@ def connect(self, clean_session=True):
def disconnect(self):
"""Disconnects from the broker.
"""
if self._sock is None:
raise MMQTTException("MiniMQTT is not connected.")
self.is_connected()
self._logger.debug('Sending DISCONNECT packet to server')
self._sock.write(MQTT_DISCONNECT)
self._logger.debug('Closing socket')
Expand All @@ -282,6 +282,7 @@ def ping(self):
there is an active network connection.
Raises a MMQTTException if the server does not respond with a PINGRESP packet.
"""
self.is_connected()
self._logger.debug('Sending PINGREQ')
self._sock.write(MQTT_PING_REQ)
res = self._sock.read(1)
Expand All @@ -300,6 +301,7 @@ def publish(self, topic, msg, retain=False, qos=0):
:param bool retain: Whether the message is saved by the broker.
:param int qos: Quality of Service level for the message.
"""
self.is_connected()
if topic is None or len(topic) == 0:
raise MMQTTException("Invalid MQTT Topic, must have length > 0.")
if '+' in topic or '#' in topic:
Expand All @@ -317,8 +319,6 @@ def publish(self, topic, msg, retain=False, qos=0):
raise MMQTTException('Message size larger than %db.'%MQTT_MSG_MAX_SZ)
if qos < 0 or qos > 2:
raise MMQTTException("Invalid QoS level, must be between 0 and 2.")
if self._sock is None:
raise MMQTTException("MiniMQTT not connected.")
pkt = MQTT_PUB
pkt[0] |= qos << 1 | retain
sz = 2 + len(topic) + len(msg)
Expand All @@ -331,7 +331,7 @@ def publish(self, topic, msg, retain=False, qos=0):
sz >>= 7
i += 1
pkt[i] = sz
self._logger.debug('Sending PUBLISH\nTopic: {0}\nMsg: {1}\nQoS: {2}\n Retain: {3}'.format(topic, msg, qos, retain))
self._logger.debug('Sending PUBLISH\nTopic: {0}\nMsg: {1}\nQoS: {2}\nRetain? {3}'.format(topic, msg, qos, retain))
self._sock.write(pkt)
self._send_str(topic)
if qos == 0:
Expand Down Expand Up @@ -387,8 +387,7 @@ def subscribe(self, topic, qos=0):
.. code-block:: python
mqtt_client.subscribe([('topics/ledState', 1), ('topics/servoAngle', 0)])
"""
if self._sock is None:
raise MMQTTException("MiniMQTT not connected.")
self.is_connected()
topics = None
if isinstance(topic, tuple):
topic, qos = topic
Expand Down

0 comments on commit 41bc399

Please sign in to comment.