39
39
https://github.com/adafruit/Adafruit_CircuitPython_MiniMQTT
40
40
"""
41
41
import time
42
- # from adafruit_io.adafruit_io_errors import AdafruitIO_RequestError, AdafruitIO_ThrottleError
42
+ from adafruit_io .adafruit_io_errors import AdafruitIO_RequestError , AdafruitIO_ThrottleError , AdafruitIO_MQTTError
43
43
44
44
__version__ = "0.0.0-auto.0"
45
45
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Adafruit_IO.git"
48
48
'User-Agent' : 'AIO-CircuitPython/{0}' .format (__version__ )
49
49
}
50
50
51
- class MQTT_API ():
51
+ class IO_MQTT ():
52
52
"""
53
53
Client for interacting with the Adafruit IO MQTT API. The client establishes
54
54
a secure connection to Adafruit IO by default.
55
55
:param MiniMQTT mqtt_client: MiniMQTT Client object.
56
56
:param bool secure: Enables a secure SSL/TLS connection with Adafruit IO.
57
57
"""
58
58
def __init__ (self , mqtt_client , secure = True ):
59
- self ._user = aio_username
60
- self ._key = aio_key
61
59
# MiniMQTT Object
62
- print (type (mqtt_client ))
63
- print ('MQTT CLIENT: ' , mqtt_client )
64
- self ._client = mqtt_client
65
- # User-defined MQTT callback methods need to be init'd to none
60
+ mqtt_client_type = str (type (mqtt_client ))
61
+ if 'MQTT' in mqtt_client_type :
62
+ self ._client = mqtt_client
63
+ else :
64
+ raise TypeError ("This class requires a MiniMQTT client." )
65
+ self ._user = self ._client ._user
66
+ # User-defined MQTT callback methods must be init'd to None
66
67
self .on_connect = None
67
68
self .on_disconnect = None
68
69
self .on_message = None
@@ -78,25 +79,27 @@ def __init__(self, mqtt_client, secure=True):
78
79
self ._client .set_logger_level ('DEBUG' )
79
80
self ._connected = False
80
81
81
- @property
82
- def is_connected (self ):
83
- """Returns if connected to Adafruit IO MQTT Broker."""
84
- return self ._connected
85
-
86
82
def connect (self ):
87
83
"""Connects to the Adafruit IO MQTT Broker.
88
84
Must be called before any other API methods are called.
89
85
"""
90
- if self ._connected :
91
- return
92
- self ._client .connect ()
93
-
86
+ try :
87
+ self ._client .connect ()
88
+ except error as err :
89
+ AdafruitIO_MQTTError (err )
90
+ return
91
+
94
92
def disconnect (self ):
95
93
"""Disconnects from Adafruit IO.
96
94
"""
97
95
if self ._connected :
98
96
self ._client .disconnect ()
99
97
98
+ @property
99
+ def is_connected (self ):
100
+ """Returns if connected to Adafruit IO MQTT Broker."""
101
+ return self ._client .is_connected
102
+
100
103
def _on_connect_mqtt (self , client , userdata , flags , rc ):
101
104
"""Runs when the on_connect callback is run from code.
102
105
"""
@@ -188,10 +191,10 @@ def subscribe(self, feed_key=None, group_key=None, shared_user=None):
188
191
189
192
client.subscribe([('temperature'), ('humidity')])
190
193
"""
194
+ print ('sub called!' )
191
195
if shared_user is not None and feed_key is not None :
192
196
self ._client .subscribe ('{0}/feeds/{1}' .format (shared_user , feed_key ))
193
197
elif group_key is not None :
194
- print ('subscribing to group...' )
195
198
self ._client .subscribe ('{0}/groups/{1}' .format (self ._user , group_key ))
196
199
elif feed_key is not None :
197
200
self ._client .subscribe ('{0}/feeds/{1}' .format (self ._user , feed_key ))
0 commit comments