Deny callbacks while connecting
This commit is contained in:
parent
c4ac87f222
commit
3d9fc164b4
@ -30,6 +30,7 @@ __DEPENDENCIES__ = []
|
||||
import logging
|
||||
import paho.mqtt.client as paho
|
||||
import socket
|
||||
import time
|
||||
|
||||
try:
|
||||
from config import APP_NAME as ROOT_LOGGER_NAME
|
||||
@ -40,6 +41,7 @@ logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
|
||||
|
||||
class mqtt_client(object):
|
||||
def __init__(self, name, host, port=1883, username=None, password=None):
|
||||
self.__block_add_callbacks__ = False
|
||||
logger.info("Initiating mqtt client instance")
|
||||
self.__callbacks__ = {}
|
||||
self.__client__ = paho.Client(name) # create client object
|
||||
@ -56,6 +58,9 @@ class mqtt_client(object):
|
||||
self.__client__.loop_start() # start the loop
|
||||
|
||||
def add_callback(self, topic, callback):
|
||||
while self.__block_add_callbacks__:
|
||||
time.sleep(.1)
|
||||
logger.debug("Adding callback for topic %s", topic)
|
||||
self.__callbacks__[topic] = callback
|
||||
self.__client__.subscribe(topic)
|
||||
|
||||
@ -65,9 +70,11 @@ class mqtt_client(object):
|
||||
def __on_connect__(self, client, userdata, flags, rc):
|
||||
logger.debug("Connect with rc=%d", rc)
|
||||
if rc == 0:
|
||||
self.__block_add_callbacks__ = True
|
||||
logger.debug("Registering topics...")
|
||||
for topic in self.__callbacks__:
|
||||
self.__client__.subscribe(topic)
|
||||
self.__block_add_callbacks__ = False
|
||||
|
||||
def __on_disconnect__(self, client, flags, rc):
|
||||
logger.warning("Disconnect with rc=%d", rc)
|
||||
|
Loading…
x
Reference in New Issue
Block a user