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