Fix to keep mqtt client in memory

This commit is contained in:
zewelor
2018-04-02 11:37:27 +02:00
parent 6131b3c22e
commit 06cfca60c1
+10 -7
View File
@@ -5,6 +5,7 @@ import paho.mqtt.client as mqtt
class MqttClient: class MqttClient:
def __init__(self, config): def __init__(self, config):
self._config = config self._config = config
self._mqttc = mqtt.Client()
def publish(self, messages): def publish(self, messages):
if messages is None: if messages is None:
@@ -26,17 +27,19 @@ class MqttClient:
def password(self): def password(self):
return self._config['password'] return self._config['password']
def callbacks_subscription(self, callbacks): @property
mqttc = mqtt.Client() def mqttc(self):
return self._mqttc
mqttc.username_pw_set(self.username, self.password) def callbacks_subscription(self, callbacks):
mqttc.connect(self.hostname) self.mqttc.username_pw_set(self.username, self.password)
self.mqttc.connect(self.hostname)
for topic, callback in callbacks: for topic, callback in callbacks:
mqttc.message_callback_add(topic, callback) self.mqttc.message_callback_add(topic, callback)
mqttc.subscribe(topic) self.mqttc.subscribe(topic)
mqttc.loop_start() self.mqttc.loop_start()
class MqttMessage: class MqttMessage: