def connect(self):
"""
Connect to MQTT broker.
"""
# TODO: This is currently done synchronous which could have issues in timeout situations
# because it would block other subsystems.
# => Check if we can do asynchronous connection establishment.
self.client = mqtt.Client(client_id=self.name, clean_session=True, userdata={'foo': 'bar'})
if self.broker_username:
self.client.username_pw_set(self.broker_username, self.broker_password)
self.client.on_connect = lambda *args: reactor.callFromThread(self.on_connect, *args)
self.client.on_message = lambda *args: reactor.callFromThread(self.on_message, *args)
self.client.on_log = lambda *args: reactor.callFromThread(self.on_log, *args)
# Connect with retry
self.connect_loop = LoopingCall(self.connect_with_retry)
self.connect_loop.start(self.retry_interval, now=True)
评论列表
文章目录