Commit 6d7d192c6dfb4801756eed1e194bf3931c2d7e7e
1 parent
f958e3f4
Exists in
dev
Add agentdeviceplc
Showing
3 changed files
with
238 additions
and
0 deletions
Show diff stats
CHANGELOG
... | ... | @@ -0,0 +1,117 @@ |
1 | +#!/usr/bin/env python3 | |
2 | + | |
3 | +import sys,os | |
4 | + | |
5 | +pwd = os.environ['PROJECT_ROOT_PATH'] | |
6 | +if pwd not in sys.path: | |
7 | + sys.path.append(pwd) | |
8 | +from src.core.pyros_django.agent.Agent import Agent, build_agent, log, parse_args | |
9 | + | |
10 | +from src.core.pyros_django.devices.PLC import PLCController | |
11 | +from src.core.pyros_django.monitoring.plc_checker import PlcChecker | |
12 | +from common.models import * | |
13 | +from config.pyros.config_pyros import ConfigPyros | |
14 | + | |
15 | + | |
16 | +class AgentDevicePLC(Agent): | |
17 | + | |
18 | + | |
19 | + # FOR TEST ONLY | |
20 | + # Run this agent in simulator mode | |
21 | + TEST_MODE = False | |
22 | + # Run the assertion tests at the end | |
23 | + TEST_WITH_FINAL_TEST = True | |
24 | + TEST_MAX_DURATION_SEC = None | |
25 | + #TEST_MAX_DURATION_SEC = 120 | |
26 | + | |
27 | + plcController = PLCController() | |
28 | + print ("AGENT ENV: config PLC is (ip={}, port={})".format(plcController.ip, plcController.port)) | |
29 | + plc_checker = PlcChecker() | |
30 | + | |
31 | + log.debug("PLC instanciated") | |
32 | + time_history_minutes = 4 | |
33 | + | |
34 | + # new config (obsconfig) | |
35 | + def __init__(self, name:str=None, simulated_computer=None): | |
36 | + if name is None: | |
37 | + name = self.__class__.__name__ | |
38 | + super().__init__(simulated_computer=simulated_computer) | |
39 | + self.device_name = self.get_config().get_device_for_agent(self.get_config().unit_name, self.name).get("name") | |
40 | + self.serial_number = self.get_config().get_device_for_agent(self.get_config().unit_name, self.name).get("device_config").get("sn") | |
41 | + self.output_data = self.get_config().get_output_data_device(self.device_name) | |
42 | + # @override | |
43 | + def _init(self): | |
44 | + super()._init() | |
45 | + | |
46 | + log.debug("end init()") | |
47 | + # --- Set the mode according the startmode value | |
48 | + ##agent_alias = self.__class__.__name__ | |
49 | + ##self.set_mode_from_config(agent_alias) | |
50 | + | |
51 | + # @override | |
52 | + def do_log(self): | |
53 | + super().do_log() | |
54 | + | |
55 | + | |
56 | + # @override | |
57 | + # previous name of function : routine_process | |
58 | + # Note : in Agent.py, routine_process_body seems to be the main function of routine of the agent | |
59 | + # We need to override routine_process_body and not routine_process | |
60 | + def _routine_process_iter_end_body(self): | |
61 | + log.debug("in routine_process_body()") | |
62 | + print("TODO: we recycle code") | |
63 | + status_plc = self.plcController.getStatus() | |
64 | + if self.parseNewStatus(status_plc): | |
65 | + self.saveSensors() | |
66 | + #self.saveInternalMonitoring() | |
67 | + | |
68 | + def parseNewStatus(self,status_plc): | |
69 | + # """ PM 20181009 parse new status for config | |
70 | + # Find return string "plc_status" positin within status_plc | |
71 | + if status_plc.find('PLC_STATUS') >= 0: | |
72 | + self.plc_checker.chk_config(status_plc) | |
73 | + return True | |
74 | + | |
75 | + return False | |
76 | + | |
77 | + def saveSensors(self): | |
78 | + for sensor in self.output_data: | |
79 | + for entity in self.output_data[sensor]: | |
80 | + sensor_monitoring_name = self.output_data[sensor].get(entity).get("monitoring_name") | |
81 | + if sensor_monitoring_name in self.plc_checker.monitoring_names.keys() and self.plc_checker.monitoring_names[sensor_monitoring_name] is not None: | |
82 | + value = self.plc_checker.get_sensor(sensor_monitoring_name) | |
83 | + try: | |
84 | + sensor_db = Sensors_data_last_value.objects.get(key=entity,serial_number=self.serial_number) | |
85 | + except Sensors_data_last_value.DoesNotExist: | |
86 | + sensor_db = Sensors_data_last_value.objects.create( | |
87 | + value=value, | |
88 | + key=entity, | |
89 | + model=self.device_name, | |
90 | + serial_number=self.serial_number | |
91 | + ) | |
92 | + sensor_db.value=value | |
93 | + sensor_db.key=entity | |
94 | + sensor_db.model=self.device_name | |
95 | + sensor_db.serial_number=self.serial_number | |
96 | + sensor_db.save() | |
97 | + | |
98 | + # We don't have an history for weatherwatch | |
99 | + log.debug("saved weather") | |
100 | + | |
101 | + | |
102 | + | |
103 | +if __name__ == "__main__": | |
104 | + | |
105 | + # with thread | |
106 | + RUN_IN_THREAD=True | |
107 | + # with process | |
108 | + #RUN_IN_THREAD=False | |
109 | + args = parse_args(sys.argv[1:]) | |
110 | + agent = build_agent(AgentDevicePLC,param_constr=args) | |
111 | + ''' | |
112 | + TEST_MODE, configfile = extract_parameters() | |
113 | + agent = AgentM("AgentM", configfile, RUN_IN_THREAD) | |
114 | + agent.setSimulatorMode(TEST_MODE) | |
115 | + ''' | |
116 | + print(agent) | |
117 | + agent.run() | ... | ... |
... | ... | @@ -0,0 +1,117 @@ |
1 | +#!/usr/bin/env python3 | |
2 | + | |
3 | +import sys,os | |
4 | + | |
5 | +pwd = os.environ['PROJECT_ROOT_PATH'] | |
6 | +if pwd not in sys.path: | |
7 | + sys.path.append(pwd) | |
8 | +from src.core.pyros_django.agent.Agent import Agent, build_agent, log, parse_args | |
9 | + | |
10 | +from src.core.pyros_django.devices.PLC import PLCController | |
11 | +from src.core.pyros_django.monitoring.plc_checker import PlcChecker | |
12 | +from common.models import * | |
13 | +from config.pyros.config_pyros import ConfigPyros | |
14 | + | |
15 | + | |
16 | +class AgentDevicePLC(Agent): | |
17 | + | |
18 | + | |
19 | + # FOR TEST ONLY | |
20 | + # Run this agent in simulator mode | |
21 | + TEST_MODE = False | |
22 | + # Run the assertion tests at the end | |
23 | + TEST_WITH_FINAL_TEST = True | |
24 | + TEST_MAX_DURATION_SEC = None | |
25 | + #TEST_MAX_DURATION_SEC = 120 | |
26 | + | |
27 | + plcController = PLCController() | |
28 | + print ("AGENT ENV: config PLC is (ip={}, port={})".format(plcController.ip, plcController.port)) | |
29 | + plc_checker = PlcChecker() | |
30 | + | |
31 | + log.debug("PLC instanciated") | |
32 | + time_history_minutes = 4 | |
33 | + | |
34 | + # new config (obsconfig) | |
35 | + def __init__(self, name:str=None, simulated_computer=None): | |
36 | + if name is None: | |
37 | + name = self.__class__.__name__ | |
38 | + super().__init__(simulated_computer=simulated_computer) | |
39 | + self.device_name = self.get_config().get_device_for_agent(self.get_config().unit_name, self.name).get("name") | |
40 | + self.serial_number = self.get_config().get_device_for_agent(self.get_config().unit_name, self.name).get("device_config").get("sn") | |
41 | + self.output_data = self.get_config().get_output_data_device(self.device_name) | |
42 | + # @override | |
43 | + def _init(self): | |
44 | + super()._init() | |
45 | + | |
46 | + log.debug("end init()") | |
47 | + # --- Set the mode according the startmode value | |
48 | + ##agent_alias = self.__class__.__name__ | |
49 | + ##self.set_mode_from_config(agent_alias) | |
50 | + | |
51 | + # @override | |
52 | + def do_log(self): | |
53 | + super().do_log() | |
54 | + | |
55 | + | |
56 | + # @override | |
57 | + # previous name of function : routine_process | |
58 | + # Note : in Agent.py, routine_process_body seems to be the main function of routine of the agent | |
59 | + # We need to override routine_process_body and not routine_process | |
60 | + def _routine_process_iter_end_body(self): | |
61 | + log.debug("in routine_process_body()") | |
62 | + print("TODO: we recycle code") | |
63 | + status_plc = self.plcController.getStatus() | |
64 | + if self.parseNewStatus(status_plc): | |
65 | + self.saveSensors() | |
66 | + #self.saveInternalMonitoring() | |
67 | + | |
68 | + def parseNewStatus(self,status_plc): | |
69 | + # """ PM 20181009 parse new status for config | |
70 | + # Find return string "plc_status" positin within status_plc | |
71 | + if status_plc.find('PLC_STATUS') >= 0: | |
72 | + self.plc_checker.chk_config(status_plc) | |
73 | + return True | |
74 | + | |
75 | + return False | |
76 | + | |
77 | + def saveSensors(self): | |
78 | + for sensor in self.output_data: | |
79 | + for entity in self.output_data[sensor]: | |
80 | + sensor_monitoring_name = self.output_data[sensor].get(entity).get("monitoring_name") | |
81 | + if sensor_monitoring_name in self.plc_checker.monitoring_names.keys() and self.plc_checker.monitoring_names[sensor_monitoring_name] is not None: | |
82 | + value = self.plc_checker.get_sensor(sensor_monitoring_name) | |
83 | + try: | |
84 | + sensor_db = Sensors_data_last_value.objects.get(key=entity,serial_number=self.serial_number) | |
85 | + except Sensors_data_last_value.DoesNotExist: | |
86 | + sensor_db = Sensors_data_last_value.objects.create( | |
87 | + value=value, | |
88 | + key=entity, | |
89 | + model=self.device_name, | |
90 | + serial_number=self.serial_number | |
91 | + ) | |
92 | + sensor_db.value=value | |
93 | + sensor_db.key=entity | |
94 | + sensor_db.model=self.device_name | |
95 | + sensor_db.serial_number=self.serial_number | |
96 | + sensor_db.save() | |
97 | + | |
98 | + # We don't have an history for weatherwatch | |
99 | + log.debug("saved weather") | |
100 | + | |
101 | + | |
102 | + | |
103 | +if __name__ == "__main__": | |
104 | + | |
105 | + # with thread | |
106 | + RUN_IN_THREAD=True | |
107 | + # with process | |
108 | + #RUN_IN_THREAD=False | |
109 | + args = parse_args(sys.argv[1:]) | |
110 | + agent = build_agent(AgentDevicePLC,param_constr=args) | |
111 | + ''' | |
112 | + TEST_MODE, configfile = extract_parameters() | |
113 | + agent = AgentM("AgentM", configfile, RUN_IN_THREAD) | |
114 | + agent.setSimulatorMode(TEST_MODE) | |
115 | + ''' | |
116 | + print(agent) | |
117 | + agent.run() | ... | ... |