Exception handling for MQTT
This commit is contained in:
parent
eb24a3ee5d
commit
5425af984b
20
powerplug.py
20
powerplug.py
@ -77,7 +77,10 @@ class sispmctl(object):
|
||||
def send_state(self, output):
|
||||
topic = config.MQTT_TOPIC + "/status/" + str(output)
|
||||
logger.info("Sending Powerplug status information of plug %s to mqtt %s = %s", str(output), topic, str(self.__state__[output - 1]))
|
||||
self.__mqtt_client__.publish(topic, "true" if self.__state__[output - 1] else "false")
|
||||
try:
|
||||
self.__mqtt_client__.publish(topic, "true" if self.__state__[output - 1] else "false")
|
||||
except (socket.timeout, OSError) as e:
|
||||
logger.warning("Erro while sending state information information")
|
||||
|
||||
|
||||
class mqtt_powerplug(object):
|
||||
@ -98,12 +101,15 @@ class mqtt_powerplug(object):
|
||||
self.__client__ = mqtt.Client("mqtt_powerplug") # create client object
|
||||
self.__client__.on_message = self.__receive__ # attach function to callback
|
||||
self.__client__.username_pw_set(config.MQTT_USER, config.MQTT_PASS) # login with credentials
|
||||
self.__client__.connect(config.MQTT_SERVER, 1883) # establish connection
|
||||
self.__client__.loop_start() # start the loop
|
||||
self.__topics__ = []
|
||||
for subtopic in self.SUBTOPICS:
|
||||
self.__topics__.append(config.MQTT_TOPIC + "/" + subtopic)
|
||||
self.__client__.subscribe(self.__topics__[-1]) # subscibe a topic
|
||||
try:
|
||||
self.__client__.connect(config.MQTT_SERVER, 1883) # establish connection
|
||||
self.__client__.loop_start() # start the loop
|
||||
self.__topics__ = []
|
||||
for subtopic in self.SUBTOPICS:
|
||||
self.__topics__.append(config.MQTT_TOPIC + "/" + subtopic)
|
||||
self.__client__.subscribe(self.__topics__[-1]) # subscibe a topic
|
||||
except (socket.timeout, OSError) as e:
|
||||
logger.warning("Erro while setting up mqtt instance and listener")
|
||||
self.__sc__ = sispmctl(self.__client__)
|
||||
|
||||
def __receive__(self, client, userdata, message):
|
||||
|
Loading…
x
Reference in New Issue
Block a user