Commit eabb9310fd0a0f74676e6e586e718748492cf99c
1 parent
306d4778
Exists in
dev
scan captors list from plc status
Showing
4 changed files
with
99 additions
and
12 deletions
Show diff stats
.gitignore
src/dashboard/views.py
... | ... | @@ -174,11 +174,11 @@ def weather_config(request): |
174 | 174 | # Import PLC status sensor parser |
175 | 175 | from src.monitoring.plc_checker import PlcChecker |
176 | 176 | # Parse PLC status in colibri-new-fixed-2.json |
177 | - colibri_json = open("../simulators/plc/colibri-new-fixed-2.json") | |
178 | - colibri_struct = json.load(colibri_json) | |
179 | - plc_status = colibri_struct["statuses"][1]["entities"][0] | |
180 | - plc_checker = PlcChecker(plc_status) | |
181 | - plc_checker.scan_sensors() | |
177 | + #colibri_json = open("../simulators/plc/colibri-new-fixed-2.json") | |
178 | + #colibri_struct = json.load(colibri_json) | |
179 | + #plc_status = colibri_struct["statuses"][1]["entities"][0] | |
180 | + plc_checker = PlcChecker() | |
181 | + #plc_checker.scan_sensors() | |
182 | 182 | # Return template with sensors list |
183 | 183 | return render(request, 'dashboard/config_weather.html', {'weather_config' : plc_checker.captors_conf, 'base_template' : "base.html"}) |
184 | 184 | except Config.DoesNotExist: | ... | ... |
src/monitoring/plc_checker.py
1 | - | |
1 | +import json | |
2 | 2 | import logging |
3 | - | |
4 | 3 | logger = logging.getLogger(__name__) |
5 | 4 | |
5 | + | |
6 | + | |
7 | + | |
6 | 8 | class PlcChecker(object): |
7 | 9 | |
8 | - def __init__(self, plc_struct): | |
9 | - self.struct = plc_struct | |
10 | + def __init__(self): | |
11 | + self.plc_from = None | |
12 | + self.plc_site = None | |
13 | + self.struct = None | |
10 | 14 | self.path_list = None |
11 | - #self.plc_global = copy.copy(self.struct["devices"]) | |
12 | - #self.struct | |
15 | + # self.plc_global = copy.copy(self.struct["devices"]) | |
16 | + # | |
17 | + self.load_config() | |
18 | + self.device_name = None | |
19 | + self.device_type = None | |
20 | + self.serial_number = None | |
21 | + self.valid = None | |
13 | 22 | self.captors = [ |
14 | 23 | "OutsideTemp", |
15 | 24 | "InsideTemp", |
... | ... | @@ -23,8 +32,76 @@ class PlcChecker(object): |
23 | 32 | "DewPoint", |
24 | 33 | ] |
25 | 34 | self.captors_conf = {} |
35 | + self.captors_table = [] | |
36 | + | |
37 | + def load_config(self): | |
38 | + try: | |
39 | + colibri_json = open("./monitoring/plc_config.json") | |
40 | + self.struct = json.load(colibri_json) | |
41 | + logger.info("Loaded : plc_config.json") | |
42 | + colibri_json.close | |
43 | + except: | |
44 | + logger.info("No plc_config.json") | |
45 | + | |
46 | + def save_config(self): | |
47 | + #try: | |
48 | + _file = open("./monitoring/plc_config.json" , 'w') | |
49 | + _file.write(json.dumps(self.captors_conf)) | |
50 | + _file.close() | |
51 | + logger.info("Saved : plc_config.json") | |
52 | + #except: | |
53 | + logger.info("Not saved plc_config.json") | |
54 | + | |
55 | + def chk_config(self, status_plc): | |
56 | + struct = json.loads(status_plc) | |
57 | + if isinstance(struct, list): | |
58 | + self.struct = struct[0] | |
59 | + else: | |
60 | + self.struct = struct | |
61 | + if self.struct["entity_name"] == "PLC_STATUS": | |
62 | + if not self.same_origin(): | |
63 | + self.scan_sensors() | |
64 | + self.save_config() | |
65 | + else: | |
66 | + logger.info("No PLC_STATUS") | |
67 | + | |
68 | + def same_origin(self): | |
69 | + if self.struct["from"] == self.plc_from and self.struct["site"] == self.plc_site: | |
70 | + return True | |
71 | + else: | |
72 | + return False | |
73 | + | |
74 | + def get_key(self, struct, key): | |
75 | + try: | |
76 | + ret = struct[key] | |
77 | + except: | |
78 | + ret = None | |
79 | + return ret | |
26 | 80 | |
27 | 81 | def scan_sensors(self): |
82 | + # return captors table from struct | |
83 | + try: | |
84 | + logger.debug(self.struct["date"]) | |
85 | + except Exception as e: | |
86 | + logger.debug("No date") | |
87 | + for device in self.struct["devices"]: | |
88 | + self.device_name = self.get_key(device, "device_name") | |
89 | + self.device_type = self.get_key(device, "device_type") | |
90 | + self.serial_number = self.get_key(device, "serial_number") | |
91 | + self.valid = self.get_key(device, "valid") | |
92 | + for device_values in device["device_values"]: | |
93 | + captor = [] | |
94 | + captor.append(self.device_name) | |
95 | + captor.append(self.device_type) | |
96 | + captor.append(self.serial_number) | |
97 | + captor.append(self.valid) | |
98 | + captor.append(self.get_key(device_values, "name")) | |
99 | + captor.append(self.get_key(device_values, "type")) | |
100 | + self.captors_table.append(captor) | |
101 | + | |
102 | + | |
103 | + # deprecated find captor list in struct | |
104 | + def find_sensors(self): | |
28 | 105 | try: |
29 | 106 | logger.debug(self.struct["date"]) |
30 | 107 | except Exception as e: | ... | ... |
src/monitoring/tasks.py
... | ... | @@ -7,6 +7,7 @@ from celery.task import Task |
7 | 7 | # NEW task base class, but DOES NOT WORK (because celery3 and not 4 ?) !!! |
8 | 8 | #from celery import Task |
9 | 9 | |
10 | +from src.monitoring.plc_checker import PlcChecker | |
10 | 11 | from devices.PLC import PLCController |
11 | 12 | from utils.JDManipulator import * |
12 | 13 | import json |
... | ... | @@ -41,6 +42,7 @@ class Monitoring(Task): |
41 | 42 | timer_status = None |
42 | 43 | timer_tasks = None |
43 | 44 | status_plc = None |
45 | + plc_checker = PlcChecker() | |
44 | 46 | |
45 | 47 | def run(self): |
46 | 48 | print ("AGENT ENV: startup...") |
... | ... | @@ -255,6 +257,11 @@ class Monitoring(Task): |
255 | 257 | inside.save() |
256 | 258 | #return 0 |
257 | 259 | |
260 | + def parseNewStatus(self,status_plc ): | |
261 | + # """ PM 20181009 parse new status for config | |
262 | + if status_plc[:7] != "NOT_SET": | |
263 | + self.plc_checker.chk_config(status_plc) | |
264 | + | |
258 | 265 | ''' |
259 | 266 | Function called by the main loop to handle the plc status |
260 | 267 | ''' |
... | ... | @@ -262,7 +269,8 @@ class Monitoring(Task): |
262 | 269 | # Reset timer total duration |
263 | 270 | self.timers["timer_status"] = self.timer_status |
264 | 271 | status_plc = self.plcController.getStatus() |
265 | - #print("status ->" + status_plc) | |
272 | + # PM 20181009 parse new status for config | |
273 | + self.parseNewStatus(status_plc) | |
266 | 274 | |
267 | 275 | # Error while READING status ? |
268 | 276 | if (self.plcController.isError(status_plc)): | ... | ... |