Commit 10142b11 authored by Peter-Bernd Otte's avatar Peter-Bernd Otte

config file behaviour changed and logger added for mqtt paho

parent 57bbc346
**/__pycache__/
......@@ -25,17 +25,19 @@ supportedSensors = {"TFBHSB55":"TippFunk Bewegungsmelder", "FBH63":"Bewegungsmel
"FHF":"Funk Fenstergriff", "FTKE":"Hebel Sensor", "FTK":"Solar Reed Sensor", "FRW":"Funk Rauch-Sensor",
"FSM60B":"Funk Wasser Sensor" }
#with open("config.json") as f:
# data = json.load(f)
if not os.path.exists(args.config_file):
logger.error("Config file "+args.config_file+" not found.")
ConfigFile = args.config_file
if not os.path.exists(ConfigFile):
ConfigFile = "default.yml"
if not os.path.exists(ConfigFile):
logger.error("Config file "+args.config_file+" and "+ConfigFile+" not found. A config file is required.")
sys.exit(2)
with open(args.config_file, 'r') as ymlfile:
logger.info("Reading config file: {}".format(ConfigFile))
with open(ConfigFile, 'r') as ymlfile:
data = yaml.full_load(ymlfile) # see https://github.com/yaml/pyyaml/wiki/PyYAML-yaml.load(input)-Deprecation
data = {k.lower(): v for k, v in data.items()} #make all dict keys lowercase
data = {str(k).lower(): v for k, v in data.items()} #make all dict keys lowercase
for k,v in data.items():
if v['type'] not in supportedSensors:
logger.error("In configuration file ('"+args.config_file+"'), type: "+v['type']+" is not supported.")
logger.error("In configuration file ('"+ConfigFile+"'), type: "+v['type']+" is not supported.")
sys.exit(1)
def on_connect(client, userdata, flags, rc):
......@@ -138,6 +140,7 @@ client=paho.Client(args.mqtt_client_name)
client.on_message=on_message
client.on_connect = on_connect
client.on_disconnect = on_disconnect
client.enable_logger(logger) #info: https://www.eclipse.org/paho/clients/python/docs/#callbacks
logger.info("Conncting to broker "+args.mqtt_broker_host)
client.connect(args.mqtt_broker_host)
client.loop_start()
......
......@@ -8,6 +8,7 @@ from relais import *
import yaml #pip3 install pyyaml
import os #for os.path.isfile
import logging, argparse
import sys
logging.basicConfig(format="%(asctime)-15s %(levelname)-8s %(message)s")
logger = logging.getLogger("Actor")
......@@ -92,12 +93,18 @@ client= paho.Client(args.mqtt_client_name)
relaisList = []
debug = True if args.verbosity>1 else False
if os.path.exists(args.config_file):
logger.debug("config file exists")
with open(args.config_file, 'r') as ymlfile:
ConfigFile = args.config_file
if not os.path.exists(ConfigFile):
ConfigFile = "default.yml"
if not os.path.exists(ConfigFile):
logger.error("Config file "+args.config_file+" and "+ConfigFile+" not found. A config file is required.")
sys.exit(2)
logger.info("Reading config file {}".format(ConfigFile) )
with open(ConfigFile, 'r') as ymlfile:
cfg = yaml.full_load(ymlfile) # see https://github.com/yaml/pyyaml/wiki/PyYAML-yaml.load(input)-Deprecation
for section in cfg:
for section in cfg:
MQTTName="homie/"+args.mqtt_client_name+"/"+section
logger.info("Adding Relais with MQTT topic: "+MQTTName)
logger.info("Configuration: "+str(cfg[section]))
......
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