Commit e15030dcf143a605e81c9de16089c104c9c90976

Authored by Patrick Maeght
1 parent f6076245
Exists in dev

penelope work part 2

src/common/models.py
... ... @@ -774,7 +774,7 @@ class WeatherWatch(models.Model):
774 774 # TODO
775 775 def setGlobalStatus(self):
776 776 self.global_status = ""
777   - if self.rain and float(self.rain) > 10:
  777 + if self.rain and float(self.rain) > 0:
778 778 self.global_status += "RAINING "
779 779 if self.wind and float(self.wind) > 80:
780 780 self.global_status += "WIND_TOO_STRONG "
... ... @@ -787,19 +787,19 @@ class WeatherWatch(models.Model):
787 787 return 0
788 788  
789 789 def setAttribute(self, key, value):
790   - if key == "RainRate":
  790 + if key == "Rain_boolean":
791 791 self.rain = value
792   - elif key == "CloudRate":
  792 + elif key == "_CloudRate":
793 793 self.cloud = value
794   - elif key == "WindSpeed":
  794 + elif key == "Wind_speed":
795 795 self.wind = value
796   - elif key == "WindDir":
  796 + elif key == "Wind_direction":
797 797 self.wind_dir = value
798   - elif key == "SensorTemperature":
  798 + elif key == "Temperature_outside":
799 799 self.temperature = value
800   - elif key == "OutsideHumidity":
  800 + elif key == "Humidity_outside":
801 801 self.humidity = value
802   - elif key == "Pressure":
  802 + elif key == "_Pressure":
803 803 self.pressure = value
804 804 else:
805 805 return 1
... ...
src/monitoring/plc_checker.py
... ... @@ -31,7 +31,7 @@ class PlcChecker(object):
31 31 "LIGHTS": None,
32 32 "SHUTTERS": None,
33 33 }
34   - self.sensors_conf = {}
  34 + #self.sensors_conf = {}
35 35 self.sensors_table = []
36 36 self.load_config()
37 37  
... ... @@ -41,6 +41,7 @@ class PlcChecker(object):
41 41 _struct = json.load(colibri_json)
42 42 self.origin = _struct["origin"]
43 43 self.sensors_table = _struct["sensors"]
  44 + self.sensors_monitor= _struct["selected_sensors"]
44 45 logger.info("Loaded : plc_config.json")
45 46 colibri_json.close
46 47 except:
... ... @@ -51,6 +52,7 @@ class PlcChecker(object):
51 52 _struct = {}
52 53 _struct["origin"] = self.origin
53 54 _struct["sensors"] = self.sensors_table
  55 + _struct["selected_sensors"] = self.sensors_monitor
54 56 _file = open("./monitoring/plc_config.json" , 'w')
55 57 _file.write(json.dumps(_struct))
56 58 _file.close()
... ... @@ -70,6 +72,7 @@ class PlcChecker(object):
70 72 if not self.same_origin():
71 73 self.scan_sensors()
72 74 self.save_config()
  75 + return True
73 76  
74 77 def monitored_sensor(self, name, _sensor_id):
75 78 known = None
... ... @@ -98,6 +101,19 @@ class PlcChecker(object):
98 101 ret = None
99 102 return ret
100 103  
  104 + def get_sensor(self, monitored):
  105 + #id:'DHT22:/MiFe_DHT1_1:/Humidity'
  106 + _value = self.sensors_monitor[monitored]
  107 + if _value != None:
  108 + sp_id = _value.split(":/")
  109 + for device in self.struct["devices"]:
  110 + if device["device_name"] == sp_id[0] and device["serial_number"] == sp_id[1]:
  111 + for device_values in device["device_values"]:
  112 + if device_values["name"] == sp_id[2]:
  113 + _value = device_values["value"]
  114 + return _value
  115 +
  116 +
101 117 def scan_sensors(self):
102 118 # return sensors table from struct
103 119 self.sensors_table = []
... ... @@ -105,7 +121,7 @@ class PlcChecker(object):
105 121 logger.debug(self.struct["date"])
106 122 except Exception as e:
107 123 logger.debug("No date")
108   - ind1 = 0
  124 +
109 125 for device in self.struct["devices"]:
110 126 self.device_name = self.get_key(device, "device_name")
111 127 self.device_type = self.get_key(device, "device_type")
... ... @@ -119,7 +135,7 @@ class PlcChecker(object):
119 135 sensor.append(self.valid)
120 136 _name = self.get_key(device_values, "name")
121 137 sensor.append(_name)
122   - _sensor_id = "sel:/" + self.device_name + ":/" + self.serial_number + ":/" + _name
  138 + _sensor_id = self.device_name + ":/" + self.serial_number + ":/" + _name
123 139 _monitoring_name, _selected = self.monitored_sensor(self.get_key(device_values, "monitoring_name"), _sensor_id)
124 140 sensor.append(_monitoring_name)
125 141 sensor.append(self.get_key(device_values, "type"))
... ...
src/monitoring/tasks.py
... ... @@ -257,10 +257,20 @@ class Monitoring(Task):
257 257 inside.save()
258 258 #return 0
259 259  
  260 + def saveNewStatus(self):
  261 + outside = WeatherWatch()
  262 + for sensor in self.plc_checker.sensors_monitor.keys():
  263 + value = self.plc_checker.get_sensor(sensor)
  264 + print(sensor, value)
  265 + outside.setAttribute(sensor, value)
  266 + outside.setGlobalStatus()
  267 + outside.save()
  268 +
260 269 def parseNewStatus(self,status_plc ):
261 270 # """ PM 20181009 parse new status for config
262 271 if status_plc.find('PLC_STATUS') >= 0:
263 272 self.plc_checker.chk_config(status_plc)
  273 + return True
264 274  
265 275 '''
266 276 Function called by the main loop to handle the plc status
... ... @@ -270,7 +280,9 @@ class Monitoring(Task):
270 280 self.timers["timer_status"] = self.timer_status
271 281 status_plc = self.plcController.getStatus()
272 282 # PM 20181009 parse new status for config
273   - self.parseNewStatus(status_plc)
  283 + if self.parseNewStatus(status_plc):
  284 + self.saveNewStatus()
  285 +
274 286  
275 287 # Error while READING status ?
276 288 if (self.plcController.isError(status_plc)):
... ...