Commit 2ce8e152 authored by Peter-Bernd Otte's avatar Peter-Bernd Otte

Added more command line options.

parent 8c4431ef
......@@ -11,25 +11,26 @@ import re
import logging
import argparse
MQTTClientName = "fsr-ww"
format = "%(asctime)-9s %(name)-6s %(levelname)-8s %(message)s"
format = "%(asctime)-9s %(levelname)-8s %(message)s"
logging.basicConfig(format=format, datefmt="%H:%M:%S")
logger = logging.getLogger(MQTTClientName)
logger = logging.getLogger("FSRMaster")
parser = argparse.ArgumentParser(description='Workload distributor for trivial parallelism.')
parser.add_argument("-v", "--verbosity", help="increase output verbosity", default=0, action="count")
parser.add_argument("SerialDeviceName", help="Serial port used, eg /dev/ttyUSB0")
parser.add_argument("-b", "--mqtt-broker-host", help="MQTT broker hostname", default="locahlhost")
parser.add_argument("-t", "--watchdog-timeout", help="timeout in seconds for the watchdog", default=20*60*10, type=int) # ca. 10 mins
parser.add_argument("mqtt_client_name", help="MQTT client name. Needs to be unique in the MQTT namespace, eg fsr-ww.", type=str)
parser.add_argument("serial_device_name", help="Serial port used, eg /dev/ttyUSB0", type=str)
args = parser.parse_args()
logger.setLevel(logging.WARNING-(args.verbosity*10 if args.verbosity <=2 else 20) )
broker="dom"
RelaisOffset = 0x01000000 #Eltako Relais should be programmed to react on addresses like 0x01000000 + RelaisID
print(args)
logger.info("MQTT client name: "+args.mqtt_client_name )
logger.info("Watchdog timeout (seconds): "+str(args.watchdog_timeout))
logger.info('Use the following Serial-Device: '+str(args.serial_device_name) )
ser = serial.Serial(args.serial_device_name, 57600, timeout=0)
logger.info('Use the following Serial-Device: '+str(args.SerialDeviceName) )
ser = serial.Serial(args.SerialDeviceName, 57600, timeout=0)
WatchDogCounter = 20*60*10 # ca. 10 mins
AskRelaisID = 0
relaisStatus = {} #stores: type, brightness,
......@@ -88,7 +89,7 @@ def on_message(client, userdata, message):
def on_connect(client, userdata, flags, rc):
if rc==0:
logger.info("MQTT connected OK. Return code "+str(rc) )
client.subscribe("homie/"+MQTTClientName+"/+/set")
client.subscribe("homie/"+args.mqtt_client_name+"/+/set")
logger.debug("MQTT: Subscribed to all topics")
else:
logger.error("Bad connection. Return code="+str(rc))
......@@ -97,12 +98,12 @@ def on_disconnect(client, userdata, rc):
if rc != 0:
logger.warning("Unexpected MQTT disconnection. Will auto-reconnect")
client= paho.Client(MQTTClientName)
client= paho.Client(args.mqtt_client_name)
client.on_message=on_message
client.on_connect = on_connect
client.on_disconnect = on_disconnect
logger.info("connecting to broker: "+broker)
client.connect(broker)
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
logger.debug("MQTT Loop started.")
......@@ -115,9 +116,10 @@ def processFSRType(time, tr, requestedHseq, requestedType, typeName, brightness)
if (tr.id in relaisStatus and relaisStatus[tr.id]["v"] != brightness) or (tr.id not in relaisStatus):
relaisStatus[tr.id] = {"type":typeName, "v": brightness, "time":time, "setv": tempSetv}
logger.info("Set Relais "+str(tr.id)+" relaisStatus "+str(relaisStatus[tr.id]) )
client.publish("homie/"+MQTTClientName+"/"+str(tr.id), json.dumps(relaisStatus[tr.id], sort_keys=True), qos=1, retain=True)
client.publish("homie/"+args.mqtt_client_name+"/"+str(tr.id), json.dumps(relaisStatus[tr.id], sort_keys=True), qos=1, retain=True)
charSet = bytearray()
WatchDogCounter = args.watchdog_timeout
while (WatchDogCounter > 0):
if len(queue)==0:
AskRelaisID = 1 if AskRelaisID > 127 else AskRelaisID+1
......@@ -142,7 +144,7 @@ while (WatchDogCounter > 0):
t = time.mktime(t.timetuple()) + t.microsecond / 1E6
processFSRType(t, tr, 139, 5, "FSR14", 100 if (tr.data>>24)==0x70 else 0 )
processFSRType(t, tr, 139, 7, "FUD14", (tr.data>>16)&0xff )
# client.publish("homie/"+MQTTClientName+"/messages", json.dumps({"data":list(tr.getByteArray()), "time":t}, sort_keys=True), qos=1)
# client.publish("homie/"+args.mqtt_client_name+"/messages", json.dumps({"data":list(tr.getByteArray()), "time":t}, sort_keys=True), qos=1)
charSet = charSet[14:]
# time.sleep(1)
......
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