21
21
import logging
22
22
23
23
import paho .mqtt .client as mqtt
24
-
24
+ import sys
25
+ from .errors import MQTTError , RequestError
25
26
26
27
# How long to wait before sending a keep alive (paho-mqtt configuration).
27
28
KEEP_ALIVE_SEC = 60 # One minute
@@ -62,11 +63,12 @@ def _mqtt_connect(self, client, userdata, flags, rc):
62
63
# Check if the result code is success (0) or some error (non-zero) and
63
64
# raise an exception if failed.
64
65
if rc == 0 :
66
+ #raise RequestError(rc)
65
67
self ._connected = True
68
+ print ('Connected to Adafruit IO!' )
66
69
else :
67
- # TODO: Make explicit exception classes for these failures:
68
- # 0: Connection successful 1: Connection refused - incorrect protocol version 2: Connection refused - invalid client identifier 3: Connection refused - server unavailable 4: Connection refused - bad username or password 5: Connection refused - not authorised 6-255: Currently unused.
69
- raise RuntimeError ('Error connecting to Adafruit IO with rc: {0}' .format (rc ))
70
+ # handle RC errors within `errors.py`'s MQTTError class
71
+ raise MQTTError (rc )
70
72
# Call the on_connect callback if available.
71
73
if self .on_connect is not None :
72
74
self .on_connect (self )
@@ -78,7 +80,8 @@ def _mqtt_disconnect(self, client, userdata, rc):
78
80
# log the RC as an error. Continue on to call any disconnect handler
79
81
# so clients can potentially recover gracefully.
80
82
if rc != 0 :
81
- logger .debug ('Unexpected disconnect with rc: {0}' .format (rc ))
83
+ raise MQTTError (rc )
84
+ print ('Disconnected from Adafruit IO!' )
82
85
# Call the on_disconnect callback if available.
83
86
if self .on_disconnect is not None :
84
87
self .on_disconnect (self )
0 commit comments