Add try catch for broker, remove loading of configuration
This commit is contained in:
@@ -87,9 +87,8 @@ class Messenger:
|
|||||||
|
|
||||||
class MqttMessenger(Messenger):
|
class MqttMessenger(Messenger):
|
||||||
"""A messenger that uses MQTT."""
|
"""A messenger that uses MQTT."""
|
||||||
def __init__(self):
|
def __init__(self, configuration):
|
||||||
with open('config.json') as json_config:
|
self._cfg = configuration
|
||||||
self._cfg = json.load(json_config)
|
|
||||||
self._client = mqtt.Client(client_id=str(random.randint(0,500)))
|
self._client = mqtt.Client(client_id=str(random.randint(0,500)))
|
||||||
self._client.on_connect = self.on_connect
|
self._client.on_connect = self.on_connect
|
||||||
self._client.on_message = self.on_message
|
self._client.on_message = self.on_message
|
||||||
@@ -116,13 +115,18 @@ class MqttMessenger(Messenger):
|
|||||||
self._client.publish(target, message, qos=1)
|
self._client.publish(target, message, qos=1)
|
||||||
|
|
||||||
def connect(self):
|
def connect(self):
|
||||||
self._client.connect(self._cfg['mqtt']['host'], \
|
try:
|
||||||
int(self._cfg['mqtt']['port']), \
|
self._client.connect(self._cfg['mqtt']['host'], \
|
||||||
int(self._cfg['mqtt']['timeout']))
|
int(self._cfg['mqtt']['port']), \
|
||||||
|
int(self._cfg['mqtt']['timeout']))
|
||||||
|
except:
|
||||||
|
print("Could not connect to broker")
|
||||||
|
return False
|
||||||
|
|
||||||
self._client.loop_start()
|
self._client.loop_start()
|
||||||
|
return True
|
||||||
|
|
||||||
def disconnec(self):
|
def disconnect(self):
|
||||||
self._client.disconnect()
|
self._client.disconnect()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|||||||
Reference in New Issue
Block a user