From 0733a13104481602b983add75de6df33305240c6 Mon Sep 17 00:00:00 2001 From: "p.loedige" Date: Mon, 4 Jan 2021 14:27:18 +0100 Subject: [PATCH] enabled single edit of output --- app.py | 1 - interface_handler.py | 24 ++++++++++++++---------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/app.py b/app.py index 58d2aef..09f851b 100644 --- a/app.py +++ b/app.py @@ -56,7 +56,6 @@ def api_devices(device_name): try: if request.method == "POST": response = interface_handler.write(device_name,request.data.get("output")) - return {'output': str(response)} return {'state': interface_handler.read(device_name)} except Exception as e: return {'error': str(e)} diff --git a/interface_handler.py b/interface_handler.py index 78b1c30..7421f69 100644 --- a/interface_handler.py +++ b/interface_handler.py @@ -104,7 +104,7 @@ class Interface_handler: return returnValues - def write(self, device_name:str, value): + def write(self, device_name:str, output): """writes an output to a given device Args: @@ -128,16 +128,20 @@ class Interface_handler: protocol = port_info["protocol"] pins = port_info["pins"] - for pin in self.xml_reader.get_port(device_name)["pins"]: - if protocol == "DO": - #type check input value - if not value in ['0','1']: - raise TypeError("value must be '0' or '1'") - value = GPIO.HIGH if value == '1' else GPIO.LOW + if protocol == "DO": + #type check input value + if not isinstance(output,dict): + raise TypeError("value must be a dictionary of GPIOs and values") + for pin in output: + if not pin in self.xml_reader.get_port(device_name)["pins"]: + raise ValueError(str("pin %s is not a part of device %s",pin,device_name)) + if not output[pin] in ['0','1']: + raise TypeError("value must be 1 or 0") + value = GPIO.HIGH if output[pin] == '1' else GPIO.LOW GPIO.output(self.Pin[pin].value,value) - return GPIO.input(self.Pin[pin].value) - else: #throw error for protocols without write functionality - raise ValueError("you cannot write to device %s" % device_name) + return output + else: #throw error for protocols without write functionality + raise ValueError("you cannot write to device %s" % device_name) # ##test