From e15030dcf143a605e81c9de16089c104c9c90976 Mon Sep 17 00:00:00 2001 From: pmaeght Date: Mon, 5 Nov 2018 19:27:46 +0100 Subject: [PATCH] penelope work part 2 --- src/common/models.py | 16 ++++++++-------- src/monitoring/plc_checker.py | 22 +++++++++++++++++++--- src/monitoring/tasks.py | 14 +++++++++++++- 3 files changed, 40 insertions(+), 12 deletions(-) diff --git a/src/common/models.py b/src/common/models.py index f484697..b5bea38 100644 --- a/src/common/models.py +++ b/src/common/models.py @@ -774,7 +774,7 @@ class WeatherWatch(models.Model): # TODO def setGlobalStatus(self): self.global_status = "" - if self.rain and float(self.rain) > 10: + if self.rain and float(self.rain) > 0: self.global_status += "RAINING " if self.wind and float(self.wind) > 80: self.global_status += "WIND_TOO_STRONG " @@ -787,19 +787,19 @@ class WeatherWatch(models.Model): return 0 def setAttribute(self, key, value): - if key == "RainRate": + if key == "Rain_boolean": self.rain = value - elif key == "CloudRate": + elif key == "_CloudRate": self.cloud = value - elif key == "WindSpeed": + elif key == "Wind_speed": self.wind = value - elif key == "WindDir": + elif key == "Wind_direction": self.wind_dir = value - elif key == "SensorTemperature": + elif key == "Temperature_outside": self.temperature = value - elif key == "OutsideHumidity": + elif key == "Humidity_outside": self.humidity = value - elif key == "Pressure": + elif key == "_Pressure": self.pressure = value else: return 1 diff --git a/src/monitoring/plc_checker.py b/src/monitoring/plc_checker.py index 2e60ca2..90d2bce 100644 --- a/src/monitoring/plc_checker.py +++ b/src/monitoring/plc_checker.py @@ -31,7 +31,7 @@ class PlcChecker(object): "LIGHTS": None, "SHUTTERS": None, } - self.sensors_conf = {} + #self.sensors_conf = {} self.sensors_table = [] self.load_config() @@ -41,6 +41,7 @@ class PlcChecker(object): _struct = json.load(colibri_json) self.origin = _struct["origin"] self.sensors_table = _struct["sensors"] + self.sensors_monitor= _struct["selected_sensors"] logger.info("Loaded : plc_config.json") colibri_json.close except: @@ -51,6 +52,7 @@ class PlcChecker(object): _struct = {} _struct["origin"] = self.origin _struct["sensors"] = self.sensors_table + _struct["selected_sensors"] = self.sensors_monitor _file = open("./monitoring/plc_config.json" , 'w') _file.write(json.dumps(_struct)) _file.close() @@ -70,6 +72,7 @@ class PlcChecker(object): if not self.same_origin(): self.scan_sensors() self.save_config() + return True def monitored_sensor(self, name, _sensor_id): known = None @@ -98,6 +101,19 @@ class PlcChecker(object): ret = None return ret + def get_sensor(self, monitored): + #id:'DHT22:/MiFe_DHT1_1:/Humidity' + _value = self.sensors_monitor[monitored] + if _value != None: + sp_id = _value.split(":/") + for device in self.struct["devices"]: + if device["device_name"] == sp_id[0] and device["serial_number"] == sp_id[1]: + for device_values in device["device_values"]: + if device_values["name"] == sp_id[2]: + _value = device_values["value"] + return _value + + def scan_sensors(self): # return sensors table from struct self.sensors_table = [] @@ -105,7 +121,7 @@ class PlcChecker(object): logger.debug(self.struct["date"]) except Exception as e: logger.debug("No date") - ind1 = 0 + for device in self.struct["devices"]: self.device_name = self.get_key(device, "device_name") self.device_type = self.get_key(device, "device_type") @@ -119,7 +135,7 @@ class PlcChecker(object): sensor.append(self.valid) _name = self.get_key(device_values, "name") sensor.append(_name) - _sensor_id = "sel:/" + self.device_name + ":/" + self.serial_number + ":/" + _name + _sensor_id = self.device_name + ":/" + self.serial_number + ":/" + _name _monitoring_name, _selected = self.monitored_sensor(self.get_key(device_values, "monitoring_name"), _sensor_id) sensor.append(_monitoring_name) sensor.append(self.get_key(device_values, "type")) diff --git a/src/monitoring/tasks.py b/src/monitoring/tasks.py index 0cb3ea0..7248209 100644 --- a/src/monitoring/tasks.py +++ b/src/monitoring/tasks.py @@ -257,10 +257,20 @@ class Monitoring(Task): inside.save() #return 0 + def saveNewStatus(self): + outside = WeatherWatch() + for sensor in self.plc_checker.sensors_monitor.keys(): + value = self.plc_checker.get_sensor(sensor) + print(sensor, value) + outside.setAttribute(sensor, value) + outside.setGlobalStatus() + outside.save() + def parseNewStatus(self,status_plc ): # """ PM 20181009 parse new status for config if status_plc.find('PLC_STATUS') >= 0: self.plc_checker.chk_config(status_plc) + return True ''' Function called by the main loop to handle the plc status @@ -270,7 +280,9 @@ class Monitoring(Task): self.timers["timer_status"] = self.timer_status status_plc = self.plcController.getStatus() # PM 20181009 parse new status for config - self.parseNewStatus(status_plc) + if self.parseNewStatus(status_plc): + self.saveNewStatus() + # Error while READING status ? if (self.plcController.isError(status_plc)): -- libgit2 0.21.2