From eabb9310fd0a0f74676e6e586e718748492cf99c Mon Sep 17 00:00:00 2001 From: pmaeght Date: Tue, 9 Oct 2018 15:17:00 +0200 Subject: [PATCH] scan captors list from plc status --- .gitignore | 2 ++ src/dashboard/views.py | 10 +++++----- src/monitoring/plc_checker.py | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------ src/monitoring/tasks.py | 10 +++++++++- 4 files changed, 99 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index 8500c08..31b27b6 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,5 @@ images_folder Comet/ \.idea/ + +src/monitoring/plc_config.json diff --git a/src/dashboard/views.py b/src/dashboard/views.py index e0ed6b9..dbc3a6a 100644 --- a/src/dashboard/views.py +++ b/src/dashboard/views.py @@ -174,11 +174,11 @@ def weather_config(request): # Import PLC status sensor parser from src.monitoring.plc_checker import PlcChecker # Parse PLC status in colibri-new-fixed-2.json - colibri_json = open("../simulators/plc/colibri-new-fixed-2.json") - colibri_struct = json.load(colibri_json) - plc_status = colibri_struct["statuses"][1]["entities"][0] - plc_checker = PlcChecker(plc_status) - plc_checker.scan_sensors() + #colibri_json = open("../simulators/plc/colibri-new-fixed-2.json") + #colibri_struct = json.load(colibri_json) + #plc_status = colibri_struct["statuses"][1]["entities"][0] + plc_checker = PlcChecker() + #plc_checker.scan_sensors() # Return template with sensors list return render(request, 'dashboard/config_weather.html', {'weather_config' : plc_checker.captors_conf, 'base_template' : "base.html"}) except Config.DoesNotExist: diff --git a/src/monitoring/plc_checker.py b/src/monitoring/plc_checker.py index c09f152..599c75a 100644 --- a/src/monitoring/plc_checker.py +++ b/src/monitoring/plc_checker.py @@ -1,15 +1,24 @@ - +import json import logging - logger = logging.getLogger(__name__) + + + class PlcChecker(object): - def __init__(self, plc_struct): - self.struct = plc_struct + def __init__(self): + self.plc_from = None + self.plc_site = None + self.struct = None self.path_list = None - #self.plc_global = copy.copy(self.struct["devices"]) - #self.struct + # self.plc_global = copy.copy(self.struct["devices"]) + # + self.load_config() + self.device_name = None + self.device_type = None + self.serial_number = None + self.valid = None self.captors = [ "OutsideTemp", "InsideTemp", @@ -23,8 +32,76 @@ class PlcChecker(object): "DewPoint", ] self.captors_conf = {} + self.captors_table = [] + + def load_config(self): + try: + colibri_json = open("./monitoring/plc_config.json") + self.struct = json.load(colibri_json) + logger.info("Loaded : plc_config.json") + colibri_json.close + except: + logger.info("No plc_config.json") + + def save_config(self): + #try: + _file = open("./monitoring/plc_config.json" , 'w') + _file.write(json.dumps(self.captors_conf)) + _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 self.struct["entity_name"] == "PLC_STATUS": + if not self.same_origin(): + self.scan_sensors() + self.save_config() + else: + logger.info("No PLC_STATUS") + + def same_origin(self): + if self.struct["from"] == self.plc_from and self.struct["site"] == self.plc_site: + return True + else: + 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 + try: + logger.debug(self.struct["date"]) + except Exception as e: + logger.debug("No date") + 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") + 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) + captor.append(self.get_key(device_values, "name")) + captor.append(self.get_key(device_values, "type")) + self.captors_table.append(captor) + + + # deprecated find captor list in struct + def find_sensors(self): try: logger.debug(self.struct["date"]) except Exception as e: diff --git a/src/monitoring/tasks.py b/src/monitoring/tasks.py index 16f2ed2..118f63c 100644 --- a/src/monitoring/tasks.py +++ b/src/monitoring/tasks.py @@ -7,6 +7,7 @@ from celery.task import Task # NEW task base class, but DOES NOT WORK (because celery3 and not 4 ?) !!! #from celery import Task +from src.monitoring.plc_checker import PlcChecker from devices.PLC import PLCController from utils.JDManipulator import * import json @@ -41,6 +42,7 @@ class Monitoring(Task): timer_status = None timer_tasks = None status_plc = None + plc_checker = PlcChecker() def run(self): print ("AGENT ENV: startup...") @@ -255,6 +257,11 @@ class Monitoring(Task): inside.save() #return 0 + def parseNewStatus(self,status_plc ): + # """ PM 20181009 parse new status for config + if status_plc[:7] != "NOT_SET": + self.plc_checker.chk_config(status_plc) + ''' Function called by the main loop to handle the plc status ''' @@ -262,7 +269,8 @@ class Monitoring(Task): # Reset timer total duration self.timers["timer_status"] = self.timer_status status_plc = self.plcController.getStatus() - #print("status ->" + status_plc) + # PM 20181009 parse new status for config + self.parseNewStatus(status_plc) # Error while READING status ? if (self.plcController.isError(status_plc)): -- libgit2 0.21.2