import json import logging logger = logging.getLogger(__name__) class PlcChecker(object): def __init__(self): self.origin = {} self.origin["plc_from"] = None self.origin["plc_site"] = None self.struct = None self.path_list = None # self.plc_global = copy.copy(self.struct["devices"]) self.device_name = None self.device_type = None self.serial_number = None self.valid = None self.captors = [ "OutsideTemp", "InsideTemp", "OutsideHumidity", "InsideHumidity", "Pressure", "RainRate", "WindSpeed", "WindDir", "WindDirCardinal", "DewPoint", "analog", "digital", "SensorTemperature", "SkyTemperature", "status", "current", "plc_mode", "LIGHTS", "SHUTTERS", ] self.captors_conf = {} self.captors_table = [] self.load_config() def load_config(self): try: colibri_json = open("./monitoring/plc_config.json") _struct = json.load(colibri_json) self.origin = _struct["origin"] self.captors_table = _struct["captors"] logger.info("Loaded : plc_config.json") colibri_json.close except: logger.info("No plc_config.json") def save_config(self): #try: _struct = {} _struct["origin"] = self.origin _struct["captors"] = self.captors_table _file = open("./monitoring/plc_config.json" , 'w') _file.write(json.dumps(_struct)) _file.close() logger.info("Saved : plc_config.json") #except: logger.info("Not saved plc_config.json") def chk_config(self, status_plc): struct = json.loads(status_plc) if isinstance(struct, list): self.struct = struct[0] else: self.struct = struct if not self.same_origin(): self.scan_sensors() self.save_config() def known_captor(self, name): known = None if name in self.captors: known = name return known def same_origin(self): # check new origin if self.struct["from"] == self.origin["plc_from"] and self.struct["site"] == self.origin["plc_site"]: return True else: # update origin self.origin["plc_from"] = self.struct["from"] self.origin["plc_site"] = self.struct["site"] return False def get_key(self, struct, key): try: ret = struct[key] except: ret = None return ret def scan_sensors(self): # return captors table from struct self.captors_table = [] try: 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") self.serial_number = self.get_key(device, "serial_number") self.valid = self.get_key(device, "valid") ind1 += 1 ind2 = 0 for device_values in device["device_values"]: captor = [] captor.append(self.device_name) captor.append(self.device_type) captor.append(self.serial_number) captor.append(self.valid) _name = self.get_key(device_values, "name") captor.append(_name) captor.append(self.known_captor(_name)) captor.append(self.get_key(device_values, "type")) captor.append(str(ind1) + "_" + str(ind2)) self.captors_table.append(captor) ind2 += 1 # deprecated find captor list in struct def find_sensors(self): try: logger.debug(self.struct["date"]) except Exception as e: logger.debug("No date") for captor_type in self.captors: self.captors_conf[captor_type] = {} logger.debug(captor_type + ":") try: self.cross_devices_list(captor_type) except Exception as e: logger.debug(captor_type + " error") def cross_devices_list(self, captor_type): """""" for device in self.struct["devices"]: if device["device_name"] != "GLOBAL" and device["device_type"] == "meteo" and device["valid"] == "yes": self.cros_captor_list(device, captor_type) def cros_captor_list(self, device, captor_type): device_id = device["device_name"] + "/" + device["serial_number"] for captor in device["device_values"]: if captor["name"] == captor_type: self.captors_conf[captor_type][device_id] = captor["value"] logger.debug("-- " + device_id + " : " + str(captor["value"]))