Fix on_connect callback
This commit is contained in:
parent
20bbd9661a
commit
cdb8b5a222
@ -6,6 +6,12 @@ from typing import NoReturn
|
||||
|
||||
import paho.mqtt.client as mqtt
|
||||
|
||||
class Configuration:
|
||||
user: str
|
||||
password: str
|
||||
host: str
|
||||
port: int
|
||||
|
||||
class Client(mqtt.Client):
|
||||
def __init__(self, logger: logging.Logger=None):
|
||||
super().__init__()
|
||||
@ -15,6 +21,9 @@ class Client(mqtt.Client):
|
||||
self.logger = logging.getLogger('mqtt')
|
||||
|
||||
self._subscribe_callbacks = []
|
||||
self._on_connect_callbacks = []
|
||||
|
||||
self.on_connect = self._on_connect2
|
||||
|
||||
def setup(self, host: str, port: int, username: str, password: str):
|
||||
self._username = username
|
||||
@ -37,18 +46,22 @@ class Client(mqtt.Client):
|
||||
self._subscribe_callbacks.append((topic,callback))
|
||||
self.message_callback_add(topic, callback)
|
||||
|
||||
@property
|
||||
def on_connect(self):
|
||||
#TODO: called twice because of a boolean check before call
|
||||
def _on_connect2(self, *args):
|
||||
for (topic, callback) in self._subscribe_callbacks:
|
||||
self.subscribe(topic)
|
||||
return self._on_connect
|
||||
for func in self._on_connect_callbacks:
|
||||
func(self, *args)
|
||||
|
||||
@on_connect.setter
|
||||
def add_on_connect_callback(self, func):
|
||||
with self._callback_mutex:
|
||||
self._on_connect_callbacks.append(func)
|
||||
|
||||
@mqtt.Client.on_connect.setter
|
||||
def on_connect(self, func):
|
||||
with self._callback_mutex:
|
||||
self._on_connect = func
|
||||
|
||||
|
||||
class MQTTApp:
|
||||
""" Manage MQTT connection and on topic callback with help of the @topic decorator """
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user