Skip to content

Commit 508bf8a

Browse files
committed
Fix #11 by ensuring disconnect handler always called on disconnect (on unexpected disconnect the rc is logged as a debug message).
1 parent 4441845 commit 508bf8a

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
setuptools-*
12
#Useful gitignore for python: https://github.com/github/gitignore/blob/master/Python.gitignore
23
*.py[cod]
34

@@ -27,4 +28,4 @@ distribute-0.*
2728
nosetests.xml
2829

2930
# Translations
30-
*.mo
31+
*.mo

Adafruit_IO/mqtt_client.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,11 @@ def _mqtt_connect(self, client, userdata, flags, rc):
7474
def _mqtt_disconnect(self, client, userdata, rc):
7575
logger.debug('Client on_disconnect called.')
7676
self._connected = False
77-
# If this was an unexpected disconnect (non-zero result code) then raise
78-
# an exception.
77+
# If this was an unexpected disconnect (non-zero result code) then just
78+
# log the RC as an error. Continue on to call any disconnect handler
79+
# so clients can potentially recover gracefully.
7980
if rc != 0:
80-
raise RuntimeError('Unexpected disconnect with rc: {0}'.format(rc))
81+
logger.debug('Unexpected disconnect with rc: {0}'.format(rc))
8182
# Call the on_disconnect callback if available.
8283
if self.on_disconnect is not None:
8384
self.on_disconnect(self)
@@ -94,15 +95,15 @@ def _mqtt_message(self, client, userdata, msg):
9495

9596
def connect(self, **kwargs):
9697
"""Connect to the Adafruit.IO service. Must be called before any loop
97-
or publish operations are called. Will raise an exception if a
98+
or publish operations are called. Will raise an exception if a
9899
connection cannot be made. Optional keyword arguments will be passed
99100
to paho-mqtt client connect function.
100101
"""
101102
# Skip calling connect if already connected.
102103
if self._connected:
103104
return
104105
# Connect to the Adafruit IO MQTT service.
105-
self._client.connect(self._service_host, port=self._service_port,
106+
self._client.connect(self._service_host, port=self._service_port,
106107
keepalive=KEEP_ALIVE_SEC, **kwargs)
107108

108109
def is_connected(self):
@@ -128,7 +129,7 @@ def loop_blocking(self):
128129
your program and will not return until disconnect is explicitly called.
129130
130131
This is useful if your program doesn't need to do anything else except
131-
listen and respond to Adafruit.IO feed events. If you need to do other
132+
listen and respond to Adafruit.IO feed events. If you need to do other
132133
processing, consider using the loop_background function to run a loop
133134
in the background.
134135
"""
@@ -139,7 +140,7 @@ def loop(self, timeout_sec=1.0):
139140
inside your own main loop, where you periodically call this function to
140141
make sure messages are being processed to and from Adafruit_IO.
141142
142-
The optional timeout_sec parameter specifies at most how long to block
143+
The optional timeout_sec parameter specifies at most how long to block
143144
execution waiting for messages when this function is called. The default
144145
is one second.
145146
"""

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
setup(
1818
name = 'adafruit-io',
19-
version = '1.0.0',
19+
version = '1.0.1',
2020
author = 'Justin Cooper',
2121
author_email = '[email protected]',
2222
packages = ['Adafruit_IO'],

0 commit comments

Comments
 (0)