Commit 523ab5f0 authored by Peter-Bernd Otte's avatar Peter-Bernd Otte

Added logging for mqtt part.

parent 75454efb
......@@ -22,6 +22,21 @@ logger.setLevel(logging.WARNING-(args.verbosity*10 if args.verbosity <=2 else 20
mlast = {}
def on_log(client, userdata, level, buff):
try:
if level in (paho.MQTT_LOG_NOTICE, paho.MQTT_LOG_INFO):
logger.debug("MQTT Client Log: ({}) {}".format(level,buff))
elif level == paho.MQTT_LOG_WARNING:
logger.warning("MQTT Client Log: ({}) {}".format(level,buff))
elif level == paho.MQTT_LOG_ERR:
logger.error("MQTT Client Log: ({}) {}".format(level,buff))
elif level == paho.MQTT_LOG_DEBUG:
logger.debug("MQTT Client Log: ({}) {}".format(level,buff))
else:
logger.warning("MQTT Client Log: Log level not supported.")
except:
traceback.print_exc()
def on_connect(client, userdata, flags, rc):
if rc==0:
logger.info("MQTT connected OK. Return code "+str(rc) )
......@@ -30,16 +45,25 @@ def on_connect(client, userdata, flags, rc):
client.subscribe(x.MQTTRelaisTopic)
for y in x.defaultTopics: client.subscribe(y)
logger.debug("MQTT: "+x.MQTTname+" defaultTopics subscribed")
for y in x.toggleTopics: client.subscribe(y)
for y in x.toggleTopics:
client.subscribe(y)
logger.debug("MQTT: "+x.MQTTname+" toggleTopic: "+y)
for y in x.dimmTopics:
client.subscribe(y)
logger.debug("MQTT: "+x.MQTTname+" dimmTopics: "+y)
logger.debug("MQTT: "+x.MQTTname+" toggleTopics subscribed")
for y in x.switchOffTopics: client.subscribe(y)
logger.debug("MQTT: "+x.MQTTname+" switchOffTopics subscribed")
for y in x.inverseSwitchOnTopics: client.subscribe(y)
logger.debug("MQTT: "+x.MQTTname+" inverseSwitchOnTopics subscribed")
for y in x.prolongateStateTopics: client.subscribe(y)
logger.debug("MQTT: "+x.MQTTname+" prolongateStateTopics subscribed")
if isinstance(x.prolongateStateTopics, list):
for y in x.prolongateStateTopics:
logger.debug("MQTT: "+x.MQTTname+" x.prolongateStateTopics: "+y)
client.subscribe(y)
logger.debug("MQTT: "+x.MQTTname+" x.prolongateStateTopics: "+y)
client.subscribe(x.MQTTname+"/set")
logger.debug("MQTT: Subscribed to all topics")
logger.debug("MQTT: "+x.MQTTname+": Topic /set subscribed")
logger.info("MQTT: Success, subscribed to all topics")
else:
logger.error("Bad connection. Return code="+str(rc))
......@@ -69,7 +93,7 @@ def on_message(client, userdata, message):
x.relaisMsg(j)
x.checkAction(message.topic,j)
if message.topic == x.MQTTname+"/set":
x.setV(j['v'],j['time'])
x.setV(j['v'],t=j['time'],reason="/set cmd called")
mlast[message.topic] = j
......@@ -100,24 +124,27 @@ if os.path.exists(args.config_file):
logger.error("MQTTRelaisTopic: "+str(cfg[section]["MQTTRelaisTopic"])+" does already exist. Will not be added.")
continue
defaultTopics = cfg[section]["defaultTopics"] if "defaultTopics" in cfg[section] else []
toggleTopics = cfg[section]["toggleTopics"] if "toggleTopics" in cfg[section] else []
switchOffTopics = cfg[section]["switchOffTopics"] if "switchOffTopics" in cfg[section] else []
inverseSwitchOnTopics = cfg[section]["inverseSwitchOnTopics"] if "inverseSwitchOnTopics" in cfg[section] else []
prolongateStateTopics = cfg[section]["prolongateStateTopics"] if "prolongateStateTopics" in cfg[section] else []
defaultTopics = cfg[section]["defaultTopics"] if "defaultTopics" in cfg[section] and isinstance(cfg[section]["defaultTopics"],(list, str)) else []
toggleTopics = cfg[section]["toggleTopics"] if "toggleTopics" in cfg[section] and isinstance(cfg[section]["toggleTopics"],(list, str)) else []
dimmTopics = cfg[section]["dimmTopics"] if "dimmTopics" in cfg[section] and isinstance(cfg[section]["dimmTopics"],(list, str)) else []
switchOffTopics = cfg[section]["switchOffTopics"] if "switchOffTopics" in cfg[section] and isinstance(cfg[section]["switchOffTopics"],(list, str)) else []
inverseSwitchOnTopics = cfg[section]["inverseSwitchOnTopics"] if "inverseSwitchOnTopics" in cfg[section] and isinstance(cfg[section]["inverseSwitchOnTopics"],(list, str)) else []
prolongateStateTopics = cfg[section]["prolongateStateTopics"] if "prolongateStateTopics" in cfg[section] and isinstance(cfg[section]["prolongateStateTopics"],(list,str)) else []
# print(type(toggleTopics))
if type(defaultTopics) is not list: defaultTopics = [defaultTopics]
if type(toggleTopics) is not list: toggleTopics = [toggleTopics]
if type(switchOffTopics) is not list: toggleTopics = [switchOffTopics]
if type(inverseSwitchOnTopics) is not list: toggleTopics = [inverseSwitchOnTopics]
if type(dimmTopics) is not list: dimmTopics = [dimmTopics]
if type(switchOffTopics) is not list: switchOffTopics = [switchOffTopics]
if type(inverseSwitchOnTopics) is not list: inverseSwitchOnTopics = [inverseSwitchOnTopics]
if type(prolongateStateTopics) is not list: prolongateStateTopics = [prolongateStateTopics]
defaultSwitchOffTime = cfg[section]["defaultSwitchOffTime"] if "defaultSwitchOffTime" in cfg[section] else None
defaultSwitchOffTime = cfg[section]["defaultSwitchOffTime"] if "defaultSwitchOffTime" in cfg[section] and isinstance(cfg[section]["defaultSwitchOffTime"],(int,float,str)) else None
debugItem = cfg[section]["debug"] if "debug" in cfg[section] else debug
relaisList.append(relais(MQTTClient=client, MQTTName=MQTTName,
MQTTRelaisTopic=cfg[section]["MQTTRelaisTopic"], toggleTopics=toggleTopics, switchOffTopics=switchOffTopics,
MQTTRelaisTopic=cfg[section]["MQTTRelaisTopic"], toggleTopics=toggleTopics, dimmTopics=dimmTopics,
switchOffTopics=switchOffTopics,
defaultTopics=defaultTopics, inverseSwitchOnTopics=inverseSwitchOnTopics,
prolongateStateTopics=prolongateStateTopics,
defaultSwitchOffTime=defaultSwitchOffTime, debug=debugItem))
......@@ -127,6 +154,8 @@ if os.path.exists(args.config_file):
client.on_message=on_message
client.on_connect = on_connect
client.on_disconnect = on_disconnect
#client.on_log = on_log
client.enable_logger(logger) #info: https://www.eclipse.org/paho/clients/python/docs/#callbacks
logger.info("connecting to broker: "+args.mqtt_broker_host+". If it fails, check whether the broker is reachable. Check the -b option.")
client.connect(args.mqtt_broker_host)
client.loop_start() #start loop to process received messages in separate thread
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment