Compare commits

...

5 commits

Author SHA1 Message Date
brentru
8aa251f501 Revert "MQTT->MQTTS"
This reverts commit 9d4196630e.
2018-06-28 16:55:23 -04:00
brentru
7865e7f552 Revert "added strict error checking for params"
This reverts commit 4230bdc386.
2018-06-28 16:54:58 -04:00
brentru
4230bdc386 added strict error checking for params 2018-06-28 16:50:03 -04:00
brentru
9d4196630e MQTT->MQTTS 2018-06-28 15:40:59 -04:00
brentru
a294aec8d0 break out errors into explicit types 2018-06-28 14:45:38 -04:00
2 changed files with 24 additions and 5 deletions

View file

@ -21,6 +21,14 @@
import json
# MQTT RC Error Types
MQTT_ERRORS = [ 'Connection successful',
'Incorrect protocol version',
'Invalid Client ID',
'Server unavailable ',
'Bad username or password',
'Not authorized' ]
class AdafruitIOError(Exception):
"""Base class for all Adafruit IO request failures."""
pass
@ -49,3 +57,11 @@ class ThrottlingError(AdafruitIOError):
super(ThrottlingError, self).__init__("Exceeded the limit of Adafruit IO " \
"requests in a short period of time. Please reduce the rate of requests " \
"and try again later.")
class MQTTError(Exception):
"""Handles connection attempt failed errors.
"""
def __init__(self, response):
error = MQTT_ERRORS[response]
super(MQTTError, self).__init__(error)
pass

View file

@ -21,7 +21,8 @@
import logging
import paho.mqtt.client as mqtt
import sys
from .errors import MQTTError, RequestError
# How long to wait before sending a keep alive (paho-mqtt configuration).
KEEP_ALIVE_SEC = 60 # One minute
@ -62,11 +63,12 @@ class MQTTClient(object):
# Check if the result code is success (0) or some error (non-zero) and
# raise an exception if failed.
if rc == 0:
#raise RequestError(rc)
self._connected = True
print('Connected to Adafruit IO!')
else:
# TODO: Make explicit exception classes for these failures:
# 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.
raise RuntimeError('Error connecting to Adafruit IO with rc: {0}'.format(rc))
# handle RC errors within `errors.py`'s MQTTError class
raise MQTTError(rc)
# Call the on_connect callback if available.
if self.on_connect is not None:
self.on_connect(self)
@ -78,7 +80,8 @@ class MQTTClient(object):
# log the RC as an error. Continue on to call any disconnect handler
# so clients can potentially recover gracefully.
if rc != 0:
logger.debug('Unexpected disconnect with rc: {0}'.format(rc))
raise MQTTError(rc)
print('Disconnected from Adafruit IO!')
# Call the on_disconnect callback if available.
if self.on_disconnect is not None:
self.on_disconnect(self)