Skip to content

Commit 51b0cd9

Browse files
authored
Merge pull request #95 from brentru/fix-time-parsing
Fix on_message parsing
2 parents e614f56 + 623ea17 commit 51b0cd9

File tree

3 files changed

+27
-27
lines changed

3 files changed

+27
-27
lines changed

Adafruit_IO/_version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "2.3"
1+
__version__ = "2.3.1"

Adafruit_IO/mqtt_client.py

+16-13
Original file line numberDiff line numberDiff line change
@@ -103,23 +103,26 @@ def _mqtt_disconnect(self, client, userdata, rc):
103103
self.on_disconnect(self)
104104

105105
def _mqtt_message(self, client, userdata, msg):
106-
logger.debug('Client on_message called.')
107106
"""Parse out the topic and call on_message callback
108107
assume topic looks like `username/topic/id`
109108
"""
109+
logger.debug('Client on_message called.')
110110
parsed_topic = msg.topic.split('/')
111-
if self.on_message is not None and parsed_topic[2] == 'weather':
112-
topic = parsed_topic[4] # parse out the forecast type
113-
payload = '' if msg.payload is None else msg.payload.decode('utf-8')
114-
elif self.on_message is not None and parsed_topic[0] == 'time':
115-
topic = parsed_topic[0]
116-
payload = msg.payload.decode('utf-8')
117-
elif self.on_message is not None and parsed_topic[1] == 'groups':
118-
topic = parsed_topic[3]
119-
payload = msg.payload.decode('utf-8')
120-
else: # default topic
121-
topic = parsed_topic[2]
122-
payload = '' if msg.payload is None else msg.payload.decode('utf-8')
111+
if self.on_message is not None:
112+
if parsed_topic[0] == 'time':
113+
topic = parsed_topic[0]
114+
payload = msg.payload.decode('utf-8')
115+
elif parsed_topic[1] == 'groups':
116+
topic = parsed_topic[3]
117+
payload = msg.payload.decode('utf-8')
118+
elif parsed_topic[2] == 'weather':
119+
topic = parsed_topic[4]
120+
payload = '' if msg.payload is None else msg.payload.decode('utf-8')
121+
else:
122+
topic = parsed_topic[2]
123+
payload = '' if msg.payload is None else msg.payload.decode('utf-8')
124+
else:
125+
raise ValueError('on_message not defined')
123126
self.on_message(self, topic, payload)
124127

125128
def _mqtt_subscribe(client, userdata, mid, granted_qos):

examples/mqtt/mqtt_time.py

+10-13
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,14 @@ def message(client, feed_id, payload):
4848
# Connect to the Adafruit IO server.
4949
client.connect()
5050

51-
# time per loop
52-
loop_time = 2
51+
# Subscribe to the time feeds
52+
print('* Subscribing to time/seconds')
53+
client.subscribe_time('seconds')
5354

54-
client.loop_background()
55-
while True:
56-
print('* Subscribing to /time/seconds')
57-
client.subscribe_time('seconds')
58-
time.sleep(loop_time)
59-
print('* Subscribing to /time/millis')
60-
client.subscribe_time('millis')
61-
time.sleep(loop_time)
62-
print('* Subscribing to iso-8601')
63-
client.subscribe_time('iso')
64-
time.sleep(loop_time)
55+
print('* Subscribing to time/millis')
56+
client.subscribe_time('millis')
57+
58+
print('* Subscribing to time/ISO-8601')
59+
client.subscribe_time('iso')
60+
61+
client.loop_blocking()

0 commit comments

Comments
 (0)