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