Commit f4659764370b4b960b239c4b1927d1b245299740
1 parent
88767c84
Exists in
dev
new AgentC qui herite de new Agent et qui load la config a chaque iteration et l'affiche
Showing
4 changed files
with
388 additions
and
79 deletions
Show diff stats
docker/PYROS_DOCKER_RUN
... | ... | @@ -7,12 +7,15 @@ then |
7 | 7 | ./PYROS_DOCKER_START.bat |
8 | 8 | fi |
9 | 9 | |
10 | +echo "LOG files :" | |
11 | + | |
10 | 12 | # 1) Web server |
11 | 13 | # - Display log on current terminal |
12 | 14 | #docker exec -it pyros python3 pyros.py start webserver $@ |
13 | 15 | # - Save log in file |
14 | 16 | #docker exec pyros python3 pyros.py start webserver $@ > ../../LOG_PYROS_webserver.log 2>&1 & |
15 | -docker exec pyros python3 pyros.py start -o tnc webserver > ../../LOG_PYROS_webserver.log 2>&1 & | |
17 | +docker exec pyros python3 pyros.py start -o tnc webserver >> ../../LOG_PYROS_webserver.log 2>&1 & | |
18 | +echo "- Webserver LOG file is ../../LOG_PYROS_webserver.log" | |
16 | 19 | |
17 | 20 | # exit if webserver only |
18 | 21 | #[[ $1 == "web" ]] && exit |
... | ... | @@ -24,13 +27,15 @@ if [[ $1 != "web" ]] ; then |
24 | 27 | # - Display log on current terminal |
25 | 28 | #docker exec -it pyros bash -c "cd simulators/plc/ ; ./plcSimulator.py" |
26 | 29 | # - Save log in file |
27 | -docker exec pyros bash -c "cd simulators/plc/ ; ./plcSimulator.py" > ../../LOG_PYROS_plc_simulator.log 2>&1 & | |
30 | +docker exec pyros bash -c "cd simulators/plc/ ; ./plcSimulator.py" >> ../../LOG_PYROS_plc_simulator.log 2>&1 & | |
31 | +echo "- PLC simulator LOG file is ../../LOG_PYROS_plc_simulator.log" | |
28 | 32 | |
29 | 33 | # 3) Agent Monitoring (AgentM) |
30 | 34 | # - Display log on current terminal |
31 | 35 | #docker exec -it pyros python3 pyros.py start agentM |
32 | 36 | # - Save log in file |
33 | -docker exec pyros python3 pyros.py start agentM > ../../LOG_PYROS_agent_monitoring.log 2>&1 & | |
37 | +docker exec pyros python3 pyros.py start agentM >> ../../LOG_PYROS_agent_monitoring.log 2>&1 & | |
38 | +echo "- Agent Monitoring LOG file is ../../LOG_PYROS_agent_monitoring.log" | |
34 | 39 | |
35 | 40 | fi |
36 | 41 | ... | ... |
src/core/pyros_django/agent/Agent.py
... | ... | @@ -117,8 +117,11 @@ import config.old_config as config_old |
117 | 117 | #from config import * |
118 | 118 | |
119 | 119 | from common.models import AgentSurvey, AgentCmd, AgentLogs |
120 | + | |
120 | 121 | #from config.configpyros import ConfigPyros |
121 | -from config.old_config.configpyros import ConfigPyros | |
122 | +from config.old_config.configpyros import ConfigPyros as ConfigPyrosOld | |
123 | +from src.core.pyros_django.obsconfig.configpyros import ConfigPyros | |
124 | + | |
122 | 125 | #from dashboard.views import get_sunelev |
123 | 126 | #from devices.TelescopeRemoteControlDefault import TelescopeRemoteControlDefault |
124 | 127 | #from utils.JDManipulator import * |
... | ... | @@ -131,6 +134,7 @@ from device_controller.abstract_component.device_controller import ( |
131 | 134 | ) |
132 | 135 | |
133 | 136 | |
137 | + | |
134 | 138 | """ |
135 | 139 | ================================================================= |
136 | 140 | GENERAL MODULE CONSTANT & FUNCTIONS DEFINITIONS |
... | ... | @@ -380,6 +384,9 @@ class Agent: |
380 | 384 | CONFIG_DIR_NAME = "config" |
381 | 385 | ''' |
382 | 386 | |
387 | + # My own ConfigPyros instance (Observatory Configuration) | |
388 | + _oc = None | |
389 | + | |
383 | 390 | # Parameters from config file |
384 | 391 | # for /src/ |
385 | 392 | #_path_data = '../../config' |
... | ... | @@ -420,6 +427,10 @@ class Agent: |
420 | 427 | print('ROOT_DIR', ROOT_DIR) |
421 | 428 | print('DOC_DIR', DOC_DIR) |
422 | 429 | ''' |
430 | + | |
431 | + # SET CONFIG INSTANCE | |
432 | + | |
433 | + # - OLD CONFIG | |
423 | 434 | print('config file is', config_filename) |
424 | 435 | ##print('PYROS_ENV', config.PYROS_ENV) |
425 | 436 | print('PYROS_ENV', config_old.PYROS_ENV) |
... | ... | @@ -430,6 +441,13 @@ class Agent: |
430 | 441 | if config_old.is_prod_env(): print("PROD ENV") |
431 | 442 | if config_old.is_debug(): print("IN DEBUG MODE") |
432 | 443 | |
444 | + # - NEW CONFIG | |
445 | + obs_config_file_path = os.environ["PATH_TO_OBSCONF_FILE"] | |
446 | + path_to_obs_config_folder = os.environ["PATH_TO_OBSCONF_FOLDER"] | |
447 | + unit = os.environ["unit_name"] | |
448 | + oc = ConfigPyros(obs_config_file_path) | |
449 | + self.set_config(oc, obs_config_file_path, path_to_obs_config_folder, unit) | |
450 | + | |
433 | 451 | #self.name = name |
434 | 452 | self.name = self.__class__.__name__ |
435 | 453 | ''' |
... | ... | @@ -465,7 +483,7 @@ class Agent: |
465 | 483 | #self._set_mode(self.MODE_IDLE) |
466 | 484 | config_filename = self.get_config_filename(config_filename) |
467 | 485 | self.printd(f"*** Config file used is={config_filename}") |
468 | - self.config = ConfigPyros(config_filename) | |
486 | + self.config = ConfigPyrosOld(config_filename) | |
469 | 487 | if self.config.get_last_errno() != self.config.NO_ERROR: |
470 | 488 | raise Exception(f"Bad config file name '{config_filename}', error {str(self.config.get_last_errno())}: {str(self.config.get_last_errmsg())}") |
471 | 489 | |
... | ... | @@ -504,6 +522,96 @@ class Agent: |
504 | 522 | self.printd("Agent survey is", self._agent_survey) |
505 | 523 | |
506 | 524 | |
525 | + def set_config(self, oc: ConfigPyros, obs_config_file_path: str, path_to_obs_config_folder: str, unit: str): | |
526 | + self._oc = { | |
527 | + 'config' : oc, | |
528 | + 'env' : [ | |
529 | + obs_config_file_path, | |
530 | + path_to_obs_config_folder, | |
531 | + unit | |
532 | + ] | |
533 | + } | |
534 | + ''' | |
535 | + config_env = { | |
536 | + obs_config_file_path: obs_config_file_path, | |
537 | + path_to_obs_config_folder: path_to_obs_config_folder, | |
538 | + unit: unit | |
539 | + } | |
540 | + ''' | |
541 | + | |
542 | + def get_config_instance(self): return self._oc['config'] | |
543 | + | |
544 | + def get_config_env(self): return self._oc['env'] | |
545 | + | |
546 | + def show_config(self): | |
547 | + oc = self.get_config_instance() | |
548 | + obs_config_file_path, path_to_obs_config_folder, unit = self.get_config_env() | |
549 | + | |
550 | + self.printd(obs_config_file_path) | |
551 | + self.printd(path_to_obs_config_folder) | |
552 | + self.printd(unit) | |
553 | + print("\n") | |
554 | + print("- Observatory:", oc.get_obs_name()) | |
555 | + my_unit_name = oc.get_units_name()[0] | |
556 | + my_unit = (oc.get_units()[my_unit_name]) | |
557 | + | |
558 | + #print("- Unit description:", my_unit) | |
559 | + | |
560 | + print("\n") | |
561 | + print("- Computers:", oc.get_computers()) | |
562 | + | |
563 | + print("\n") | |
564 | + print("- Active Computers:", oc.get_active_computers()) | |
565 | + | |
566 | + print("\n") | |
567 | + print("- Active Devices:", oc.get_active_devices()) | |
568 | + | |
569 | + print("\n") | |
570 | + print("- Unit:", my_unit_name) | |
571 | + print(oc.get_unit_by_name(my_unit_name)) | |
572 | + | |
573 | + print("\n") | |
574 | + print("- Unit topology:", oc.get_topology(my_unit_name)) | |
575 | + | |
576 | + print("\n") | |
577 | + print("- Unit active Agents:", oc.get_active_agents(my_unit_name)) | |
578 | + print(oc.get_agents(my_unit_name)) | |
579 | + | |
580 | + print("\n") | |
581 | + print("- Unit Agents per computer:", oc.get_agents_per_computer(my_unit_name)) | |
582 | + | |
583 | + print("\n") | |
584 | + print("- Unit Agents per device:", oc.get_agents_per_device(my_unit_name)) | |
585 | + | |
586 | + print("\n") | |
587 | + print("- Unit Channel groups:", oc.get_channel_groups(my_unit_name)) | |
588 | + | |
589 | + print("\n") | |
590 | + print("- Unit Channels:", oc.get_channels(my_unit_name)) | |
591 | + | |
592 | + print("\n") | |
593 | + print("- Unit/Channel info:", oc.get_channel_information(my_unit_name, 'OpticalChannel_up')) | |
594 | + | |
595 | + print("\n") | |
596 | + print("- Unit Components agents:", oc.get_components_agents(my_unit_name)) | |
597 | + | |
598 | + print("\n") | |
599 | + print("- Unit database:", oc.get_database_for_unit(my_unit_name)) | |
600 | + | |
601 | + print("\n") | |
602 | + print("- Devices names:", oc.get_devices_names()) | |
603 | + print("\n") | |
604 | + print("- Devices names & files:", oc.get_devices_names_and_file()) | |
605 | + print("\n") | |
606 | + print("- Devices:", oc.get_devices()) | |
607 | + | |
608 | + print("\n") | |
609 | + | |
610 | + | |
611 | + | |
612 | + | |
613 | + | |
614 | + | |
507 | 615 | def get_config_filename(self, config_filename: str): |
508 | 616 | if not config_filename: |
509 | 617 | #config_filename = self.DEFAULT_CONFIG_FILE_NAME |
... | ... | @@ -647,6 +755,8 @@ class Agent: |
647 | 755 | if not self._DO_MAIN_LOOP: return |
648 | 756 | |
649 | 757 | self._load_config() # only if changed |
758 | + self.show_config() | |
759 | + | |
650 | 760 | |
651 | 761 | # Log this agent status (update my current mode and status in DB) |
652 | 762 | self._log_agent_status() |
... | ... | @@ -952,6 +1062,12 @@ class Agent: |
952 | 1062 | return -1 |
953 | 1063 | ''' |
954 | 1064 | self.printd("Loading the config file...") |
1065 | + | |
1066 | + # - NEW CONFIG | |
1067 | + self.get_config_instance().load(self.get_config_env()[0]) | |
1068 | + | |
1069 | + | |
1070 | + # - OLD CONFIG | |
955 | 1071 | self.config.load() |
956 | 1072 | if self.config.get_last_errno() != self.config.NO_ERROR: |
957 | 1073 | raise Exception(f"error {str(self.config.get_last_errno())}: {str(self.config.get_last_errmsg())}") |
... | ... | @@ -1625,6 +1741,7 @@ def build_agent(Agent_type:Agent, RUN_IN_THREAD=True): |
1625 | 1741 | #agent = Agent_type(name, configfile, RUN_IN_THREAD) |
1626 | 1742 | agent._set_with_simulator(WITH_SIM) |
1627 | 1743 | agent._set_test_mode(TEST_MODE) |
1744 | + | |
1628 | 1745 | #agent._set_debug_mode(DEBUG_MODE) |
1629 | 1746 | return agent |
1630 | 1747 | |
... | ... | @@ -1637,72 +1754,8 @@ if __name__ == "__main__": |
1637 | 1754 | #RUN_IN_THREAD=False |
1638 | 1755 | |
1639 | 1756 | agent = build_agent(Agent, RUN_IN_THREAD=RUN_IN_THREAD) |
1640 | - obs_config_file_path = os.environ["PATH_TO_OBSCONF_FILE"] | |
1641 | - path_to_obs_config_folder = os.environ["PATH_TO_OBSCONF_FOLDER"] | |
1642 | - unit = os.environ["unit_name"] | |
1643 | - print("TOTO") | |
1644 | - agent.printd(obs_config_file_path) | |
1645 | - agent.printd(path_to_obs_config_folder) | |
1646 | - agent.printd(unit) | |
1647 | - from src.core.pyros_django.obsconfig.configpyros import ConfigPyros | |
1648 | - oc = ConfigPyros(obs_config_file_path) | |
1649 | - | |
1650 | - print("\n") | |
1651 | - print("- Observatory:", oc.get_obs_name()) | |
1652 | - | |
1653 | - my_unit_name = oc.get_units_name()[0] | |
1654 | - my_unit = (oc.get_units()[my_unit_name]) | |
1655 | - #print("- Unit description:", my_unit) | |
1656 | - | |
1657 | - print("\n") | |
1658 | - print("- Computers:", oc.get_computers()) | |
1659 | - | |
1660 | - print("\n") | |
1661 | - print("- Active Computers:", oc.get_active_computers()) | |
1662 | - | |
1663 | - print("\n") | |
1664 | - print("- Active Devices:", oc.get_active_devices()) | |
1665 | - | |
1666 | - print("\n") | |
1667 | - print("- Unit:", my_unit_name) | |
1668 | - print(oc.get_unit_by_name(my_unit_name)) | |
1669 | - | |
1670 | - print("\n") | |
1671 | - print("- Unit topology:", oc.get_topology(my_unit_name)) | |
1672 | - | |
1673 | - print("\n") | |
1674 | - print("- Unit active Agents:", oc.get_active_agents(my_unit_name)) | |
1675 | - print(oc.get_agents(my_unit_name)) | |
1676 | - | |
1677 | - print("\n") | |
1678 | - print("- Unit Agents per computer:", oc.get_agents_per_computer(my_unit_name)) | |
1679 | - | |
1680 | - print("\n") | |
1681 | - print("- Unit Agents per device:", oc.get_agents_per_device(my_unit_name)) | |
1682 | - | |
1683 | - print("\n") | |
1684 | - print("- Unit Channel groups:", oc.get_channel_groups(my_unit_name)) | |
1685 | - | |
1686 | - print("\n") | |
1687 | - print("- Unit Channels:", oc.get_channels(my_unit_name)) | |
1688 | - | |
1689 | - print("\n") | |
1690 | - print("- Unit/Channel info:", oc.get_channel_information(my_unit_name, 'OpticalChannel_up')) | |
1691 | - | |
1692 | - print("\n") | |
1693 | - print("- Unit Components agents:", oc.get_components_agents(my_unit_name)) | |
1694 | - | |
1695 | - print("\n") | |
1696 | - print("- Unit database:", oc.get_database_for_unit(my_unit_name)) | |
1697 | - | |
1698 | - print("\n") | |
1699 | - print("- Devices names:", oc.get_devices_names()) | |
1700 | - print("\n") | |
1701 | - print("- Devices names & files:", oc.get_devices_names_and_file()) | |
1702 | - print("\n") | |
1703 | - print("- Devices:", oc.get_devices()) | |
1704 | 1757 | |
1705 | - print("\n") | |
1758 | + agent.show_config() | |
1706 | 1759 | |
1707 | 1760 | exit(0) |
1708 | 1761 | #agent = build_agent(Agent, name="GenericAgent", RUN_IN_THREAD=RUN_IN_THREAD) | ... | ... |
... | ... | @@ -0,0 +1,249 @@ |
1 | +#!/usr/bin/env python3 | |
2 | + | |
3 | + | |
4 | +import sys | |
5 | +##import utils.Logger as L | |
6 | + | |
7 | +##from .Agent import Agent | |
8 | +sys.path.append("..") | |
9 | +from agent.Agent import Agent, build_agent | |
10 | + | |
11 | + | |
12 | +##log = L.setupLogger("AgentXTaskLogger", "AgentX") | |
13 | + | |
14 | + | |
15 | + | |
16 | +class AgentC(Agent): | |
17 | + | |
18 | + # FOR TEST ONLY | |
19 | + # Run this agent in simulator mode | |
20 | + TEST_MODE = False | |
21 | + # Run the assertion tests at the end | |
22 | + TEST_WITH_FINAL_TEST = True | |
23 | + #TEST_MAX_DURATION_SEC = None | |
24 | + TEST_MAX_DURATION_SEC = 100 | |
25 | + # Who should I send commands to ? | |
26 | + #TEST_COMMANDS_DEST = "myself" | |
27 | + TEST_COMMANDS_DEST = "AgentB" | |
28 | + # Scenario to be executed | |
29 | + TEST_COMMANDS_LIST = [ | |
30 | + # Ask receiver to delete all its previous commands | |
31 | + "flush_commands", | |
32 | + | |
33 | + "go_active", | |
34 | + | |
35 | + # Because of this command, the receiver agent : | |
36 | + # - will no more send any new command | |
37 | + # - will only execute "generic" commands (and not the "specific" ones) | |
38 | + "go_idle", | |
39 | + | |
40 | + # Not executed (skipped) because receiver agent is now "idle" | |
41 | + #"specific0", | |
42 | + | |
43 | + # Because of this command, the receiver agent | |
44 | + # will now be able to send new commands | |
45 | + "go_active", | |
46 | + | |
47 | + # Executed because recipient agent is now "active" | |
48 | + "specific1", | |
49 | + # should abort previous command (specific1) | |
50 | + "abort", | |
51 | + | |
52 | + # Executed completely because no abort | |
53 | + "specific2", | |
54 | + | |
55 | + # fully executed, result is 7 | |
56 | + "eval 4+3", | |
57 | + | |
58 | + "go_idle", | |
59 | + "exit", | |
60 | + ] | |
61 | + | |
62 | + | |
63 | + | |
64 | + """ | |
65 | + ================================================================= | |
66 | + FUNCTIONS RUN INSIDE MAIN THREAD | |
67 | + ================================================================= | |
68 | + """ | |
69 | + | |
70 | + # @override | |
71 | + #def __init__(self, name:str=None, config_filename=None, RUN_IN_THREAD=True): | |
72 | + def __init__(self, config_filename=None, RUN_IN_THREAD=True): | |
73 | + ##if name is None: name = self.__class__.__name__ | |
74 | + #super().__init__(name, config_filename, RUN_IN_THREAD) | |
75 | + super().__init__(config_filename, RUN_IN_THREAD) | |
76 | + #self._log.print(f"init done for {name}") | |
77 | + ###self._log.print("init done") | |
78 | + | |
79 | + # @override | |
80 | + def init(self): | |
81 | + super().init() | |
82 | + # --- Set the mode according the startmode value | |
83 | + ##agent_alias = self.__class__.__name__ | |
84 | + ##self.set_mode_from_config(agent_alias) | |
85 | + | |
86 | + ''' | |
87 | + # @override | |
88 | + def load_config(self): | |
89 | + super().load_config() | |
90 | + ''' | |
91 | + | |
92 | + ''' | |
93 | + # @override | |
94 | + def update_survey(self): | |
95 | + super().update_survey() | |
96 | + ''' | |
97 | + | |
98 | + ''' | |
99 | + # @override | |
100 | + def get_next_command(self): | |
101 | + return super().get_next_command() | |
102 | + ''' | |
103 | + | |
104 | + # @override | |
105 | + def do_log(self): | |
106 | + super().do_log() | |
107 | + | |
108 | + | |
109 | + | |
110 | + """ | |
111 | + ================================================================= | |
112 | + FUNCTIONS RUN INSIDE A SUB-THREAD (OR A PROCESS) (thread_*()) | |
113 | + ================================================================= | |
114 | + """ | |
115 | + | |
116 | + # Define your own command step(s) here | |
117 | + def cmd_step1(self, step:int): | |
118 | + cmd = self._current_specific_cmd | |
119 | + cmd.result = f"in step #{step}/{self._thread_total_steps_number}" | |
120 | + cmd.save() | |
121 | + """ | |
122 | + if self.RUN_IN_THREAD: | |
123 | + print("(save from thread)") | |
124 | + cmd.save() | |
125 | + else: | |
126 | + #@transaction.atomic | |
127 | + print("(save from process)") | |
128 | + with transaction.atomic(): | |
129 | + cmd.save() | |
130 | + #Command.objects.select_for_update() | |
131 | + """ | |
132 | + | |
133 | + def cmd_step2(self, step:int): | |
134 | + self.cmd_step1(step) | |
135 | + def cmd_step3(self, step:int): | |
136 | + self.cmd_step1(step) | |
137 | + def cmd_step4(self, step:int): | |
138 | + self.cmd_step1(step) | |
139 | + | |
140 | + """ | |
141 | + # @override | |
142 | + def thread_exec_specific_cmd_step(self, step:int, sleep_time:float=1.0): | |
143 | + self.thread_stop_if_asked() | |
144 | + cmd = self._current_specific_cmd | |
145 | + print(f">>>>> Thread (cmd {cmd.name}): step #{step}/5") | |
146 | + self.sleep(sleep_time) | |
147 | + """ | |
148 | + | |
149 | + ''' | |
150 | + # @override | |
151 | + def exec_specific_cmd_start(self, cmd:Command, from_thread=True): | |
152 | + super().exec_specific_cmd_start(cmd, from_thread) | |
153 | + ''' | |
154 | + | |
155 | + | |
156 | + # @override | |
157 | + def thread_exec_specific_cmd_main(self): | |
158 | + # This is optional | |
159 | + self.thread_set_total_steps_number(5) | |
160 | + | |
161 | + # HERE, write your own scenario | |
162 | + | |
163 | + # scenario OK | |
164 | + self.thread_exec_specific_cmd_step(1, self.cmd_step1, 1) | |
165 | + self.thread_exec_specific_cmd_step(2, self.cmd_step2, 3) | |
166 | + self.thread_exec_specific_cmd_step(3, self.cmd_step1, 5) | |
167 | + self.thread_exec_specific_cmd_step(4, self.cmd_step3, 10) | |
168 | + self.thread_exec_specific_cmd_step(5, self.cmd_step1, 4) | |
169 | + # ... as many as you need | |
170 | + | |
171 | + """ autre scenario | |
172 | + self.thread_exec_specific_cmd_step(1, self.cmd_step1, 1) | |
173 | + self.thread_exec_specific_cmd_step(2, self.cmd_step2, 2) | |
174 | + self.thread_exec_specific_cmd_step(3, self.cmd_step1, 2) | |
175 | + self.thread_exec_specific_cmd_step(4, self.cmd_step3, 2) | |
176 | + self.thread_exec_specific_cmd_step(5, self.cmd_step1, 3) | |
177 | + """ | |
178 | + | |
179 | + ''' | |
180 | + # @override | |
181 | + def exec_specific_cmd_end(self, cmd:Command, from_thread=True): | |
182 | + super().exec_specific_cmd_end(cmd, from_thread) | |
183 | + ''' | |
184 | + | |
185 | + # @override | |
186 | + def simulator_test_results_main(self, commands): | |
187 | + nb_asserted = 0 | |
188 | + for cmd in commands: | |
189 | + if cmd.name == "flush_commands": | |
190 | + assert cmd.is_executed() | |
191 | + nb_asserted+=1 | |
192 | + # 2 times | |
193 | + if cmd.name == "go_active": | |
194 | + assert cmd.is_executed() | |
195 | + nb_asserted+=1 | |
196 | + # 2 times | |
197 | + if cmd.name == "go_idle": | |
198 | + assert cmd.is_executed() | |
199 | + nb_asserted+=1 | |
200 | + """ | |
201 | + if cmd.name == "specific0": | |
202 | + assert cmd.is_skipped() | |
203 | + assert cmd.result == "in step #5/5" | |
204 | + nb_asserted+=1 | |
205 | + """ | |
206 | + if cmd.name == "specific1": | |
207 | + assert cmd.is_killed() | |
208 | + nb_asserted+=1 | |
209 | + if cmd.name == "specific2": | |
210 | + assert cmd.is_executed() | |
211 | + assert cmd.result == "in step #5/5" | |
212 | + nb_asserted+=1 | |
213 | + if cmd.name == "eval 4+3": | |
214 | + assert cmd.is_executed() | |
215 | + assert cmd.get_result() == "7" | |
216 | + nb_asserted+=1 | |
217 | + if cmd.name in ("abort"): | |
218 | + assert cmd.is_executed() | |
219 | + nb_asserted+=1 | |
220 | + if cmd.name in ("exit"): | |
221 | + assert cmd.is_executed() | |
222 | + nb_asserted+=1 | |
223 | + return nb_asserted | |
224 | + | |
225 | + | |
226 | +""" | |
227 | +================================================================= | |
228 | + MAIN FUNCTION | |
229 | +================================================================= | |
230 | +""" | |
231 | +if __name__ == "__main__": | |
232 | + # with thread | |
233 | + RUN_IN_THREAD=True | |
234 | + # with process | |
235 | + #RUN_IN_THREAD=False | |
236 | + | |
237 | + agent = build_agent(AgentC, RUN_IN_THREAD=RUN_IN_THREAD) | |
238 | + #agent.show_config() | |
239 | + #exit(0) | |
240 | + | |
241 | + ''' | |
242 | + TEST_MODE, configfile = extract_parameters() | |
243 | + #agent = AgentX() | |
244 | + agent = AgentA("AgentA", configfile, RUN_IN_THREAD) | |
245 | + agent.setSimulatorMode(TEST_MODE) | |
246 | + print(agent) | |
247 | + ''' | |
248 | + | |
249 | + agent.run() | ... | ... |
src/core/pyros_django/obsconfig/configpyros.py
... | ... | @@ -4,6 +4,7 @@ import yaml,sys,logging,os, pickle, time |
4 | 4 | from datetime import datetime |
5 | 5 | from pykwalify.errors import PyKwalifyException,SchemaError |
6 | 6 | from pathlib import Path |
7 | + | |
7 | 8 | class ConfigPyros: |
8 | 9 | # (AKo) : Config file path is checked on the settings file, if the file isn't valid (i.e not found) the error will be launched by the settings file when starting the application |
9 | 10 | devices_links = {} |
... | ... | @@ -18,7 +19,8 @@ class ConfigPyros: |
18 | 19 | obs_config_file_content = None |
19 | 20 | #obs_config_path = os.environ.get("PATH_TO_OBSCONF_FOLDER",os.path.join(os.environ["DJANGO_PATH"],"../../../privatedev/config/default/")) |
20 | 21 | errors = None |
21 | - def verify_if_pickle_needs_to_be_updated(self,observatory_config_file)->bool: | |
22 | + | |
23 | + def verify_if_pickle_needs_to_be_updated(self, observatory_config_file)->bool: | |
22 | 24 | """ |
23 | 25 | |
24 | 26 | Args: |
... | ... | @@ -35,7 +37,7 @@ class ConfigPyros: |
35 | 37 | else: |
36 | 38 | pickle_file_mtime = os.path.getmtime(self.CONFIG_PATH+self.pickle_file) |
37 | 39 | obs_config_mtime = os.path.getmtime(observatory_config_file) |
38 | - | |
40 | + | |
39 | 41 | obs_config = self.read_and_check_config_file(observatory_config_file) |
40 | 42 | if obs_config_mtime > pickle_file_mtime: |
41 | 43 | # create obs file (yaml) from pickle["obsconfig"] with date of pickle within history folder-> nom ficher + année + mois + jour + datetime (avec secondes) -> YYYY/MM/DD H:m:s |
... | ... | @@ -58,7 +60,7 @@ class ConfigPyros: |
58 | 60 | device_file_mtime = os.path.getmtime(device_file) |
59 | 61 | if device_file_mtime > pickle_file_mtime: |
60 | 62 | return True |
61 | - | |
63 | + | |
62 | 64 | for computer in self.obs_config["OBSERVATORY"]["COMPUTERS"]: |
63 | 65 | computer_file = self.CONFIG_PATH+computer["COMPUTER"]["file"] |
64 | 66 | computer_file_mtime = os.path.getmtime(computer_file) |
... | ... | @@ -66,9 +68,9 @@ class ConfigPyros: |
66 | 68 | return True |
67 | 69 | return False |
68 | 70 | |
69 | - def load(self,observatory_config_file): | |
70 | - does_pickle_needs_to_be_updated = self.verify_if_pickle_needs_to_be_updated(observatory_config_file) | |
71 | - if os.path.isfile(self.CONFIG_PATH+self.pickle_file) and does_pickle_needs_to_be_updated == False: | |
71 | + def load(self, observatory_config_file: str): | |
72 | + pickle_needs_to_be_updated = self.verify_if_pickle_needs_to_be_updated(observatory_config_file) | |
73 | + if os.path.isfile(self.CONFIG_PATH+self.pickle_file) and pickle_needs_to_be_updated == False: | |
72 | 74 | print("Reading pickle file") |
73 | 75 | try: |
74 | 76 | can_pickle_file_be_read = False |
... | ... | @@ -91,7 +93,7 @@ class ConfigPyros: |
91 | 93 | # we rewrite the pickle file, the content will be the same otherwise we would be in the else case |
92 | 94 | print("Rewritting the pickle file (an error occured while reading it, the content will be the same as it was") |
93 | 95 | pickle_dict = {} |
94 | - | |
96 | + | |
95 | 97 | self.obs_config = self.read_and_check_config_file(observatory_config_file) |
96 | 98 | obs_file = open(observatory_config_file,"r") |
97 | 99 | pickle_dict["raw_config"] = obs_file.read() |
... | ... | @@ -107,7 +109,7 @@ class ConfigPyros: |
107 | 109 | else: |
108 | 110 | print("Pickle file needs to be created or updated") |
109 | 111 | pickle_dict = {} |
110 | - | |
112 | + | |
111 | 113 | self.obs_config = self.read_and_check_config_file(observatory_config_file) |
112 | 114 | pickle_dict["obs_config"] = self.obs_config |
113 | 115 | pickle_dict["devices"] = self.get_devices() |
... | ... | @@ -138,7 +140,7 @@ class ConfigPyros: |
138 | 140 | else: |
139 | 141 | print(f"{yaml_file} can't be accessed, waiting for availability") |
140 | 142 | time.sleep(0.5) |
141 | - | |
143 | + | |
142 | 144 | c = pykwalify.core.Core(source_file=yaml_file, schema_files=[self.SCHEMA_PATH+schema_file]) |
143 | 145 | return c.validate(raise_exception=True) |
144 | 146 | except SchemaError: | ... | ... |