Skip to content

Commit

Permalink
Merge pull request #95 from brentru/fix-time-parsing
Browse files Browse the repository at this point in the history
Fix on_message parsing
  • Loading branch information
brentru authored Mar 8, 2019
2 parents e614f56 + 623ea17 commit 51b0cd9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Adafruit_IO/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "2.3"
__version__ = "2.3.1"
29 changes: 16 additions & 13 deletions Adafruit_IO/mqtt_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,23 +103,26 @@ def _mqtt_disconnect(self, client, userdata, rc):
self.on_disconnect(self)

def _mqtt_message(self, client, userdata, msg):
logger.debug('Client on_message called.')
"""Parse out the topic and call on_message callback
assume topic looks like `username/topic/id`
"""
logger.debug('Client on_message called.')
parsed_topic = msg.topic.split('/')
if self.on_message is not None and parsed_topic[2] == 'weather':
topic = parsed_topic[4] # parse out the forecast type
payload = '' if msg.payload is None else msg.payload.decode('utf-8')
elif self.on_message is not None and parsed_topic[0] == 'time':
topic = parsed_topic[0]
payload = msg.payload.decode('utf-8')
elif self.on_message is not None and parsed_topic[1] == 'groups':
topic = parsed_topic[3]
payload = msg.payload.decode('utf-8')
else: # default topic
topic = parsed_topic[2]
payload = '' if msg.payload is None else msg.payload.decode('utf-8')
if self.on_message is not None:
if parsed_topic[0] == 'time':
topic = parsed_topic[0]
payload = msg.payload.decode('utf-8')
elif parsed_topic[1] == 'groups':
topic = parsed_topic[3]
payload = msg.payload.decode('utf-8')
elif parsed_topic[2] == 'weather':
topic = parsed_topic[4]
payload = '' if msg.payload is None else msg.payload.decode('utf-8')
else:
topic = parsed_topic[2]
payload = '' if msg.payload is None else msg.payload.decode('utf-8')
else:
raise ValueError('on_message not defined')
self.on_message(self, topic, payload)

def _mqtt_subscribe(client, userdata, mid, granted_qos):
Expand Down
23 changes: 10 additions & 13 deletions examples/mqtt/mqtt_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,14 @@ def message(client, feed_id, payload):
# Connect to the Adafruit IO server.
client.connect()

# time per loop
loop_time = 2
# Subscribe to the time feeds
print('* Subscribing to time/seconds')
client.subscribe_time('seconds')

client.loop_background()
while True:
print('* Subscribing to /time/seconds')
client.subscribe_time('seconds')
time.sleep(loop_time)
print('* Subscribing to /time/millis')
client.subscribe_time('millis')
time.sleep(loop_time)
print('* Subscribing to iso-8601')
client.subscribe_time('iso')
time.sleep(loop_time)
print('* Subscribing to time/millis')
client.subscribe_time('millis')

print('* Subscribing to time/ISO-8601')
client.subscribe_time('iso')

client.loop_blocking()

0 comments on commit 51b0cd9

Please sign in to comment.