Skip to content

Commit febfdb7

Browse files
author
brentru
committed
start refactoring - get rid of MMQTT dep, it shouldnt be called from the lib if someone wants two clients
1 parent 2306cbf commit febfdb7

File tree

1 file changed

+33
-43
lines changed

1 file changed

+33
-43
lines changed

adafruit_io/adafruit_io.py

Lines changed: 33 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
https://github.com/adafruit/Adafruit_CircuitPython_MiniMQTT
4040
"""
4141
import time
42-
from adafruit_minimqtt import MQTT as MQTTClient
4342
#from adafruit_io.adafruit_io_errors import AdafruitIO_RequestError, AdafruitIO_ThrottleError
4443

4544
__version__ = "0.0.0-auto.0"
@@ -49,37 +48,20 @@
4948
'User-Agent': 'AIO-CircuitPython/{0}'.format(__version__)
5049
}
5150

52-
forecast_types = ["current", "forecast_minutes_5",
53-
"forecast_minutes_30", "forecast_hours_1",
54-
"forecast_hours_2", "forecast_hours_6",
55-
"forecast_hours_24", "forecast_days_1",
56-
"forecast_days_2", "forecast_days_5",]
57-
58-
class MQTT():
51+
class MQTT_API():
5952
"""
6053
Client for interacting with the Adafruit IO MQTT API. The client establishes
6154
a secure connection to Adafruit IO by default.
62-
:param str aio_username: Adafruit.io account username.
63-
:param str aio_key: Adafruit.io active key.
64-
:param network_manager: NetworkManager object, such as WiFiManager from ESPSPI_WiFiManager.
65-
:param bool secure: Enables SSL/TLS connection.
55+
:param MiniMQTT mqtt_client: MiniMQTT Client object.
56+
:param bool secure: Enables a secure SSL/TLS connection with Adafruit IO.
6657
"""
67-
def __init__(self, aio_username, aio_key, network_manager, socket, secure=True):
58+
def __init__(self, mqtt_client, secure=True):
6859
self._user = aio_username
6960
self._key = aio_key
70-
# Network interface hardware detection
71-
manager_type = str(type(network_manager))
72-
if ('ESPSPI_WiFiManager' in manager_type or 'ESPAT_WiFiManager' in manager_type):
73-
self._network_manager = network_manager
74-
else:
75-
raise TypeError("This library requires a NetworkManager object.")
76-
# Set up a MiniMQTT Client
77-
self._client = MQTTClient(socket,
78-
'io.adafruit.com',
79-
port=8883,
80-
username=self._user,
81-
password=self._key,
82-
network_manager=self._network_manager)
61+
# MiniMQTT Object
62+
print(type(mqtt_client))
63+
print('MQTT CLIENT: ', mqtt_client)
64+
self._client = mqtt_client
8365
# User-defined MQTT callback methods need to be init'd to none
8466
self.on_connect = None
8567
self.on_disconnect = None
@@ -98,9 +80,7 @@ def __init__(self, aio_username, aio_key, network_manager, socket, secure=True):
9880

9981
@property
10082
def is_connected(self):
101-
"""Returns True if is connected to the to Adafruit IO
102-
MQTT Broker.
103-
"""
83+
"""Returns if connected to Adafruit IO MQTT Broker."""
10484
return self._connected
10585

10686
def connect(self):
@@ -142,30 +122,35 @@ def _on_disconnect_mqtt(self, client, userdata, rc):
142122
if self.on_disconnect is not None:
143123
self.on_disconnect(self)
144124

145-
def _on_message_mqtt(self, client, topic, message):
125+
def _on_message_mqtt(self, client, topic, payload):
146126
"""Runs when the on_message callback is run from code.
147-
Performs parsing based on username/feed/feed-key.
127+
Parses incoming data from special Adafruit IO feeds.
148128
:param MQTT client: A MQTT Client Instance.
149129
:param str topic: MQTT topic response from Adafruit IO.
150-
:param str message: MQTT message data response from Adafruit IO.
130+
:param str payload: MQTT payload data response from Adafruit IO.
151131
"""
152132
if self._logger:
153133
self._client._logger.debug('Client called on_message.')
154134
if self.on_message is not None:
155135
# Parse the MQTT topic string
156136
topic_name = topic.split('/')
157137
if topic_name[1] == "groups":
158-
print(message)
159-
message = eval(message)
160-
for feed in message['feeds']:
161-
topic_name = feed # TODO: change this to topic_name
162-
message = message['feeds'][topic]
163-
print(topic, message)
164-
topic_name = topic
138+
# Adafruit IO Group Feed Parsing
139+
# May have rx'd more than one feed - parse them.
140+
feeds = []
141+
messages = []
142+
payload = eval(payload)
143+
for feed in payload['feeds']:
144+
feeds.append(feed)
145+
for msg in feeds:
146+
payload = payload['feeds'][msg]
147+
messages.append(payload)
148+
topic_name = feeds
149+
message = messages
165150
else:
166151
topic_name = topic_name[2]
167-
# parse the message
168-
message = '' if message is None else message
152+
# parse the payload
153+
message = '' if payload is None else message
169154
else:
170155
raise ValueError('You must define an on_message method before calling this callback.')
171156
self.on_message(self, topic_name, message)
@@ -214,12 +199,17 @@ def subscribe(self, feed_key=None, group_key=None, shared_user=None):
214199
raise AdafruitIO_MQTTError('Must provide a feed_key or group_key.')
215200

216201
def subscribe_to_throttling(self):
217-
"""Subscribes to the throttle feed to notify you
218-
if your Adafruit IO rate limit has been exceeded.
202+
"""Subscribes to your personal Adafruit IO /throttle feed.
219203
https://io.adafruit.com/api/docs/mqtt.html#mqtt-api-rate-limiting
220204
"""
221205
self._client.subscribe('%s/throttle'%self._user)
222206

207+
def subscribe_to_errors(self):
208+
"""Subscribes to your personal Adafruit IO /errors feed.
209+
Notifies you of errors relating to publish/subscribe calls.
210+
"""
211+
self._client.subscribe('%s/errors'%self._user)
212+
223213
def subscribe_to_randomizer(self, randomizer_id):
224214
"""Subscribes to a random data stream created by the Adafruit IO Words service.
225215
:param int randomizer_id: Random word record you want data for.

0 commit comments

Comments
 (0)