Skip to content

Implement network callbacks for Feed Subscribe/Unsubscribe #27

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 1 commit into from
Oct 8, 2019
Merged
Show file tree
Hide file tree
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
24 changes: 22 additions & 2 deletions adafruit_io/adafruit_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def __init__(self, mqtt_client):
)
# MiniMQTT's username kwarg is optional, IO requires a username
try:
self._user = self._client._user
self._user = self._client.user
except:
raise TypeError(
"Adafruit IO requires a username, please set one in MiniMQTT"
Expand All @@ -84,9 +84,11 @@ def __init__(self, mqtt_client):
self._client.on_connect = self._on_connect_mqtt
self._client.on_disconnect = self._on_disconnect_mqtt
self._client.on_message = self._on_message_mqtt
self._client.on_subscribe = self._on_subscribe_mqtt
self._client.on_unsubscribe = self._on_unsubscribe_mqtt
self._logger = False
# Write to the MiniMQTT logger, if avaliable.
if self._client._logger is not None:
if self._client.logger is not None:
self._logger = True
self._client.set_logger_level("DEBUG")
self._connected = False
Expand Down Expand Up @@ -182,6 +184,24 @@ def _on_message_mqtt(self, client, topic, payload):
)
self.on_message(self, topic_name, message)

# pylint: disable=not-callable
def _on_subscribe_mqtt(self, client, user_data, topic, qos):
"""Runs when the client calls on_subscribe.
"""
if self._logger:
self._client._logger.debug("Client called on_subscribe")
if self.on_subscribe is not None:
self.on_subscribe(self, user_data, topic, qos)

# pylint: disable=not-callable
def _on_unsubscribe_mqtt(self, client, user_data, topic, pid):
"""Runs when the client calls on_unsubscribe.
"""
if self._logger:
self._client._logger.debug("Client called on_unsubscribe")
if self.on_unsubscribe is not None:
self.on_unsubscribe(self, user_data, topic, pid)

def loop(self):
"""Manually process messages from Adafruit IO.
Call this method to check incoming subscription messages.
Expand Down
10 changes: 10 additions & 0 deletions examples/mqtt/adafruit_io_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ def connected(client):
# Subscribe to changes on a feed named DemoFeed.
client.subscribe("DemoFeed")

def subscribe(client, userdata, topic, granted_qos):
# This method is called when the client subscribes to a new feed.
print('Subscribed to {0} with QOS level {1}'.format(topic, granted_qos))

def unsubscribe(client, userdata, topic, pid):
# This method is called when the client unsubscribes from a feed.
print('Unsubscribed from {0} with PID {1}'.format(topic, pid))

# pylint: disable=unused-argument
def disconnected(client):
# Disconnected function will be called when the client disconnects.
Expand Down Expand Up @@ -96,6 +104,8 @@ def message(client, feed_id, payload):
# Connect the callback methods defined above to Adafruit IO
io.on_connect = connected
io.on_disconnect = disconnected
io.on_subscribe = subscribe
io.on_unsubscribe = unsubscribe
io.on_message = message

# Connect to Adafruit IO
Expand Down