plc_checker.py
5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
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"]))