From 6d7d192c6dfb4801756eed1e194bf3931c2d7e7e Mon Sep 17 00:00:00 2001 From: Alexis Koralewski Date: Wed, 15 Feb 2023 15:22:35 +0100 Subject: [PATCH] Add agentdeviceplc --- CHANGELOG | 4 ++++ privatedev/plugin/agent_devices/AgentDevicePLC.py | 117 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/core/pyros_django/monitoring/AgentDevicePLC.py | 117 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 238 insertions(+), 0 deletions(-) create mode 100644 privatedev/plugin/agent_devices/AgentDevicePLC.py create mode 100644 src/core/pyros_django/monitoring/AgentDevicePLC.py diff --git a/CHANGELOG b/CHANGELOG index 056816e..befc447 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,7 @@ +15-02-2023 (AKo): v0.6.18.0 + - Add new version of weather config view + - Add agentdeviceplc + 06-02-2023 (AKo): v0.6.17.O - Add components file (RoofControler, PLC) diff --git a/privatedev/plugin/agent_devices/AgentDevicePLC.py b/privatedev/plugin/agent_devices/AgentDevicePLC.py new file mode 100644 index 0000000..5813393 --- /dev/null +++ b/privatedev/plugin/agent_devices/AgentDevicePLC.py @@ -0,0 +1,117 @@ +#!/usr/bin/env python3 + +import sys,os + +pwd = os.environ['PROJECT_ROOT_PATH'] +if pwd not in sys.path: + sys.path.append(pwd) +from src.core.pyros_django.agent.Agent import Agent, build_agent, log, parse_args + +from src.core.pyros_django.devices.PLC import PLCController +from src.core.pyros_django.monitoring.plc_checker import PlcChecker +from common.models import * +from config.pyros.config_pyros import ConfigPyros + + +class AgentDevicePLC(Agent): + + + # FOR TEST ONLY + # Run this agent in simulator mode + TEST_MODE = False + # Run the assertion tests at the end + TEST_WITH_FINAL_TEST = True + TEST_MAX_DURATION_SEC = None + #TEST_MAX_DURATION_SEC = 120 + + plcController = PLCController() + print ("AGENT ENV: config PLC is (ip={}, port={})".format(plcController.ip, plcController.port)) + plc_checker = PlcChecker() + + log.debug("PLC instanciated") + time_history_minutes = 4 + + # new config (obsconfig) + def __init__(self, name:str=None, simulated_computer=None): + if name is None: + name = self.__class__.__name__ + super().__init__(simulated_computer=simulated_computer) + self.device_name = self.get_config().get_device_for_agent(self.get_config().unit_name, self.name).get("name") + self.serial_number = self.get_config().get_device_for_agent(self.get_config().unit_name, self.name).get("device_config").get("sn") + self.output_data = self.get_config().get_output_data_device(self.device_name) + # @override + def _init(self): + super()._init() + + log.debug("end init()") + # --- Set the mode according the startmode value + ##agent_alias = self.__class__.__name__ + ##self.set_mode_from_config(agent_alias) + + # @override + def do_log(self): + super().do_log() + + + # @override + # previous name of function : routine_process + # Note : in Agent.py, routine_process_body seems to be the main function of routine of the agent + # We need to override routine_process_body and not routine_process + def _routine_process_iter_end_body(self): + log.debug("in routine_process_body()") + print("TODO: we recycle code") + status_plc = self.plcController.getStatus() + if self.parseNewStatus(status_plc): + self.saveSensors() + #self.saveInternalMonitoring() + + def parseNewStatus(self,status_plc): + # """ PM 20181009 parse new status for config + # Find return string "plc_status" positin within status_plc + if status_plc.find('PLC_STATUS') >= 0: + self.plc_checker.chk_config(status_plc) + return True + + return False + + def saveSensors(self): + for sensor in self.output_data: + for entity in self.output_data[sensor]: + sensor_monitoring_name = self.output_data[sensor].get(entity).get("monitoring_name") + if sensor_monitoring_name in self.plc_checker.monitoring_names.keys() and self.plc_checker.monitoring_names[sensor_monitoring_name] is not None: + value = self.plc_checker.get_sensor(sensor_monitoring_name) + try: + sensor_db = Sensors_data_last_value.objects.get(key=entity,serial_number=self.serial_number) + except Sensors_data_last_value.DoesNotExist: + sensor_db = Sensors_data_last_value.objects.create( + value=value, + key=entity, + model=self.device_name, + serial_number=self.serial_number + ) + sensor_db.value=value + sensor_db.key=entity + sensor_db.model=self.device_name + sensor_db.serial_number=self.serial_number + sensor_db.save() + + # We don't have an history for weatherwatch + log.debug("saved weather") + + + +if __name__ == "__main__": + + # with thread + RUN_IN_THREAD=True + # with process + #RUN_IN_THREAD=False + args = parse_args(sys.argv[1:]) + agent = build_agent(AgentDevicePLC,param_constr=args) + ''' + TEST_MODE, configfile = extract_parameters() + agent = AgentM("AgentM", configfile, RUN_IN_THREAD) + agent.setSimulatorMode(TEST_MODE) + ''' + print(agent) + agent.run() diff --git a/src/core/pyros_django/monitoring/AgentDevicePLC.py b/src/core/pyros_django/monitoring/AgentDevicePLC.py new file mode 100644 index 0000000..5813393 --- /dev/null +++ b/src/core/pyros_django/monitoring/AgentDevicePLC.py @@ -0,0 +1,117 @@ +#!/usr/bin/env python3 + +import sys,os + +pwd = os.environ['PROJECT_ROOT_PATH'] +if pwd not in sys.path: + sys.path.append(pwd) +from src.core.pyros_django.agent.Agent import Agent, build_agent, log, parse_args + +from src.core.pyros_django.devices.PLC import PLCController +from src.core.pyros_django.monitoring.plc_checker import PlcChecker +from common.models import * +from config.pyros.config_pyros import ConfigPyros + + +class AgentDevicePLC(Agent): + + + # FOR TEST ONLY + # Run this agent in simulator mode + TEST_MODE = False + # Run the assertion tests at the end + TEST_WITH_FINAL_TEST = True + TEST_MAX_DURATION_SEC = None + #TEST_MAX_DURATION_SEC = 120 + + plcController = PLCController() + print ("AGENT ENV: config PLC is (ip={}, port={})".format(plcController.ip, plcController.port)) + plc_checker = PlcChecker() + + log.debug("PLC instanciated") + time_history_minutes = 4 + + # new config (obsconfig) + def __init__(self, name:str=None, simulated_computer=None): + if name is None: + name = self.__class__.__name__ + super().__init__(simulated_computer=simulated_computer) + self.device_name = self.get_config().get_device_for_agent(self.get_config().unit_name, self.name).get("name") + self.serial_number = self.get_config().get_device_for_agent(self.get_config().unit_name, self.name).get("device_config").get("sn") + self.output_data = self.get_config().get_output_data_device(self.device_name) + # @override + def _init(self): + super()._init() + + log.debug("end init()") + # --- Set the mode according the startmode value + ##agent_alias = self.__class__.__name__ + ##self.set_mode_from_config(agent_alias) + + # @override + def do_log(self): + super().do_log() + + + # @override + # previous name of function : routine_process + # Note : in Agent.py, routine_process_body seems to be the main function of routine of the agent + # We need to override routine_process_body and not routine_process + def _routine_process_iter_end_body(self): + log.debug("in routine_process_body()") + print("TODO: we recycle code") + status_plc = self.plcController.getStatus() + if self.parseNewStatus(status_plc): + self.saveSensors() + #self.saveInternalMonitoring() + + def parseNewStatus(self,status_plc): + # """ PM 20181009 parse new status for config + # Find return string "plc_status" positin within status_plc + if status_plc.find('PLC_STATUS') >= 0: + self.plc_checker.chk_config(status_plc) + return True + + return False + + def saveSensors(self): + for sensor in self.output_data: + for entity in self.output_data[sensor]: + sensor_monitoring_name = self.output_data[sensor].get(entity).get("monitoring_name") + if sensor_monitoring_name in self.plc_checker.monitoring_names.keys() and self.plc_checker.monitoring_names[sensor_monitoring_name] is not None: + value = self.plc_checker.get_sensor(sensor_monitoring_name) + try: + sensor_db = Sensors_data_last_value.objects.get(key=entity,serial_number=self.serial_number) + except Sensors_data_last_value.DoesNotExist: + sensor_db = Sensors_data_last_value.objects.create( + value=value, + key=entity, + model=self.device_name, + serial_number=self.serial_number + ) + sensor_db.value=value + sensor_db.key=entity + sensor_db.model=self.device_name + sensor_db.serial_number=self.serial_number + sensor_db.save() + + # We don't have an history for weatherwatch + log.debug("saved weather") + + + +if __name__ == "__main__": + + # with thread + RUN_IN_THREAD=True + # with process + #RUN_IN_THREAD=False + args = parse_args(sys.argv[1:]) + agent = build_agent(AgentDevicePLC,param_constr=args) + ''' + TEST_MODE, configfile = extract_parameters() + agent = AgentM("AgentM", configfile, RUN_IN_THREAD) + agent.setSimulatorMode(TEST_MODE) + ''' + print(agent) + agent.run() -- libgit2 0.21.2