Blame view

src/monitoring/plc_checker.py 5.09 KB
eabb9310   Patrick Maeght   scan captors list...
1
import json
9bb6a2a9   Patrick Maeght   colibri-new-fixed
2
import logging
9bb6a2a9   Patrick Maeght   colibri-new-fixed
3
4
logger = logging.getLogger(__name__)

eabb9310   Patrick Maeght   scan captors list...
5
6
7



9bb6a2a9   Patrick Maeght   colibri-new-fixed
8
9
class PlcChecker(object):

eabb9310   Patrick Maeght   scan captors list...
10
    def __init__(self):
81513c84   Patrick Maeght   captor validation...
11
12
13
        self.origin = {}
        self.origin["plc_from"] = None
        self.origin["plc_site"] = None
eabb9310   Patrick Maeght   scan captors list...
14
        self.struct = None
9bb6a2a9   Patrick Maeght   colibri-new-fixed
15
        self.path_list = None
eabb9310   Patrick Maeght   scan captors list...
16
        # self.plc_global = copy.copy(self.struct["devices"])
eabb9310   Patrick Maeght   scan captors list...
17
18
19
20
        self.device_name = None
        self.device_type = None
        self.serial_number = None
        self.valid = None
9bb6a2a9   Patrick Maeght   colibri-new-fixed
21
22
23
24
25
26
27
28
29
30
31
        self.captors = [
            "OutsideTemp",
            "InsideTemp",
            "OutsideHumidity",
            "InsideHumidity",
            "Pressure",
            "RainRate",
            "WindSpeed",
            "WindDir",
            "WindDirCardinal",
            "DewPoint",
81513c84   Patrick Maeght   captor validation...
32
33
34
35
36
37
38
39
40
            "analog",
            "digital",
            "SensorTemperature",
            "SkyTemperature",
            "status",
            "current",
            "plc_mode",
            "LIGHTS",
            "SHUTTERS",
9bb6a2a9   Patrick Maeght   colibri-new-fixed
41
42
        ]
        self.captors_conf = {}
eabb9310   Patrick Maeght   scan captors list...
43
        self.captors_table = []
ff6e8ece   Patrick Maeght   check captors con...
44
        self.load_config()
eabb9310   Patrick Maeght   scan captors list...
45
46
47
48

    def load_config(self):
        try:
            colibri_json = open("./monitoring/plc_config.json")
81513c84   Patrick Maeght   captor validation...
49
50
51
            _struct = json.load(colibri_json)
            self.origin = _struct["origin"]
            self.captors_table = _struct["captors"]
eabb9310   Patrick Maeght   scan captors list...
52
53
54
55
56
57
58
            logger.info("Loaded : plc_config.json")
            colibri_json.close
        except:
            logger.info("No plc_config.json")

    def save_config(self):
        #try:
81513c84   Patrick Maeght   captor validation...
59
60
61
            _struct = {}
            _struct["origin"] = self.origin
            _struct["captors"] = self.captors_table
eabb9310   Patrick Maeght   scan captors list...
62
            _file = open("./monitoring/plc_config.json" , 'w')
81513c84   Patrick Maeght   captor validation...
63
            _file.write(json.dumps(_struct))
eabb9310   Patrick Maeght   scan captors list...
64
65
66
67
68
69
70
71
72
73
74
            _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
ff6e8ece   Patrick Maeght   check captors con...
75
        if not self.same_origin():
eabb9310   Patrick Maeght   scan captors list...
76
77
                self.scan_sensors()
                self.save_config()
eabb9310   Patrick Maeght   scan captors list...
78

81513c84   Patrick Maeght   captor validation...
79
80
81
82
83
84
    def known_captor(self, name):
        known = None
        if name in self.captors:
            known = name
        return known

eabb9310   Patrick Maeght   scan captors list...
85
    def same_origin(self):
ff6e8ece   Patrick Maeght   check captors con...
86
        # check new origin
81513c84   Patrick Maeght   captor validation...
87
        if self.struct["from"] == self.origin["plc_from"] and self.struct["site"] == self.origin["plc_site"]:
eabb9310   Patrick Maeght   scan captors list...
88
89
            return True
        else:
ff6e8ece   Patrick Maeght   check captors con...
90
            # update origin
81513c84   Patrick Maeght   captor validation...
91
92
            self.origin["plc_from"] = self.struct["from"]
            self.origin["plc_site"] = self.struct["site"]
eabb9310   Patrick Maeght   scan captors list...
93
94
95
96
97
98
99
100
            return False

    def get_key(self, struct, key):
        try:
            ret = struct[key]
        except:
            ret = None
        return ret
9bb6a2a9   Patrick Maeght   colibri-new-fixed
101

81cd1646   Patrick Maeght   weather_config pr...
102
    def scan_sensors(self):
eabb9310   Patrick Maeght   scan captors list...
103
        # return captors table from struct
ff6e8ece   Patrick Maeght   check captors con...
104
        self.captors_table = []
eabb9310   Patrick Maeght   scan captors list...
105
106
107
108
        try:
            logger.debug(self.struct["date"])
        except Exception as e:
            logger.debug("No date")
ff6e8ece   Patrick Maeght   check captors con...
109
        ind1 = 0
eabb9310   Patrick Maeght   scan captors list...
110
111
112
113
114
        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")
ff6e8ece   Patrick Maeght   check captors con...
115
116
            ind1 += 1
            ind2 = 0
eabb9310   Patrick Maeght   scan captors list...
117
118
119
120
121
122
            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)
81513c84   Patrick Maeght   captor validation...
123
124
125
                _name = self.get_key(device_values, "name")
                captor.append(_name)
                captor.append(self.known_captor(_name))
eabb9310   Patrick Maeght   scan captors list...
126
                captor.append(self.get_key(device_values, "type"))
ff6e8ece   Patrick Maeght   check captors con...
127
                captor.append(str(ind1) + "_" + str(ind2))
eabb9310   Patrick Maeght   scan captors list...
128
                self.captors_table.append(captor)
ff6e8ece   Patrick Maeght   check captors con...
129
                ind2 += 1
eabb9310   Patrick Maeght   scan captors list...
130
131
132
133


    # deprecated find captor list in struct
    def find_sensors(self):
8879f53d   Patrick Maeght   colibri-new-fixed...
134
135
136
137
        try:
            logger.debug(self.struct["date"])
        except Exception as e:
            logger.debug("No date")
9bb6a2a9   Patrick Maeght   colibri-new-fixed
138
139
140
        for captor_type in self.captors:
            self.captors_conf[captor_type] = {}
            logger.debug(captor_type + ":")
8879f53d   Patrick Maeght   colibri-new-fixed...
141
142
143
144
            try:
                self.cross_devices_list(captor_type)
            except Exception as e:
                logger.debug(captor_type + " error")
9bb6a2a9   Patrick Maeght   colibri-new-fixed
145
146
147
148
149
150
151
152
153
154
155
156
157

    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"]))