From e519f61deb0de105a8dd104eaed62710479ac25d Mon Sep 17 00:00:00 2001 From: pmaeght Date: Thu, 27 Sep 2018 17:11:34 +0200 Subject: [PATCH] test valid device for database --- simulators/plc/colibri-new-fixed-2.json | 2 +- simulators/plc/plcSimulator.py | 5 +++-- src/monitoring/tasks.py | 39 ++++++++++++++++++++++----------------- 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/simulators/plc/colibri-new-fixed-2.json b/simulators/plc/colibri-new-fixed-2.json index 13ba66a..c4f282a 100644 --- a/simulators/plc/colibri-new-fixed-2.json +++ b/simulators/plc/colibri-new-fixed-2.json @@ -247,7 +247,7 @@ "device_name": "VantagePro", "device_type": "meteo", "serial_number": "ERTRY2344324", - "valid": "yes", + "valid": "no", "device_values": [ { "name": "OutsideTemp", diff --git a/simulators/plc/plcSimulator.py b/simulators/plc/plcSimulator.py index cf57789..fde15fd 100755 --- a/simulators/plc/plcSimulator.py +++ b/simulators/plc/plcSimulator.py @@ -144,6 +144,7 @@ class PLCSimulator(DeviceSim, StatusManager): return self.loop_1() #return (0) + # deprecated def set_status(self): self.status = { "name": "STATUS", "from": "Beckhoff", "version_firmware": "20170809", "site": "OSM-Mexico", @@ -214,7 +215,7 @@ class PLCSimulator(DeviceSim, StatusManager): ''' def get_status(self, count, answer): - list = {"name": "LIST", "from": "Beckhoff", "version_firmware": "20170809", "site": "OSM-Mexico", + """list = {"name": "LIST", "from": "Beckhoff", "version_firmware": "20170809", "site": "OSM-Mexico", "date": "2017-03-03T13:42:46", "command": [ {"name": "LIST", "type": "", @@ -230,7 +231,7 @@ class PLCSimulator(DeviceSim, StatusManager): ] } ] - } + }""" if count: answer += ',' answer += json.dumps(self.status) diff --git a/src/monitoring/tasks.py b/src/monitoring/tasks.py index b93f131..16f2ed2 100644 --- a/src/monitoring/tasks.py +++ b/src/monitoring/tasks.py @@ -164,29 +164,34 @@ class Monitoring(Task): #is_safe_str = status["is_safe"] #mode = status["mode"] for device in devices: - for value in device["device_values"]: - synthesis[value["name"]] = value["value"] - synthesis[value["name"] + "_unit"] = value["unit"] + if device['valid'] == "yes": + for value in device["device_values"]: + synthesis[value["name"]] = value["value"] + synthesis[value["name"] + "_unit"] = value["unit"] return synthesis # TODO ATTENTION SI DEUX DEVICES ONT LE MEME NOM + # TODO SAVE SYNTHESIS def saveContent(self, content): devices = content[0]["devices"] - + print("saveContent") for device in devices: - status = PlcDeviceStatus() - try: - database_device = PlcDevice.objects.get(name=device["device_name"]) - except Exception as e: - # plc = Plc.objects.first() - database_device = PlcDevice.objects.create(name=device["name"]) - status.device = database_device - for value in device["device_values"]: - status.setValue(value["name"], value["value"], value["unit"]) - - #status.setValue("mode", mode) - #status.setValue("is_safe", is_safe_str) - status.save() + if device['valid'] == "yes": + status = PlcDeviceStatus() + try: + database_device = PlcDevice.objects.get(name=device["device_name"]) + except Exception as e: + # plc = Plc.objects.first() + database_device = PlcDevice.objects.create(name=device["name"]) + status.device = database_device + for value in device["device_values"]: + status.setValue(value["name"], value["value"], value["unit"]) + if value["name"] == "OutsideTemp": + print(value["name"], value["value"]) + + #status.setValue("mode", mode) + #status.setValue("is_safe", is_safe_str) + status.save() ''' Parse status returned by plc -- libgit2 0.21.2