Commit 056bae11e55b995936d9c1d22348424c28e66a95

Authored by Alain Klotz
2 parents aa8e5884 3ac24fdf
Exists in dev

Merge branch 'dev' of https://gitlab.irap.omp.eu/epallier/pyros into dev

pyros2
... ... @@ -213,7 +213,6 @@ def shell():
213 213 print("See documentation, chapter '9.6 - Play with the pyros objects' for more details")
214 214 print("Type 'exit()' to quit")
215 215 print()
216   - execProcess(PYTHON + " install.py")
217 216 os.chdir("src/")
218 217 # execProcess("python install.py install")
219 218 if not test_mode(): execProcessFromVenv(" manage.py shell")
... ... @@ -262,6 +261,7 @@ def check_agent(agent):
262 261 def start(agent:str, configfile:str):
263 262 print("Running start command")
264 263 if configfile: print("With config file", configfile)
  264 + else: configfile = ''
265 265 #if test_mode(): print("in test mode")
266 266 #if verbose_mode(): print("in verbose mode")
267 267 if not check_agent(agent): return
... ... @@ -303,7 +303,10 @@ def start(agent:str, configfile:str):
303 303 # self.execProcessFromVenvAsync(self.VENV_BIN + ' start_agent_'+agent+'.py')
304 304 print("Launching agent", agent_name, "...")
305 305 #if not test_mode(): execProcess(VENV_BIN + " start_agent_" + agent_name + ".py")
306   - if not test_mode(): execProcessFromVenv("start_agent_" + agent_name + ".py")
  306 + #TODO:
  307 + # - start_agent agent_name (1 script unique)
  308 + # - start_agent -c configfile
  309 + if not test_mode(): execProcessFromVenv("start_agent_" + agent_name + ".py " + configfile)
307 310  
308 311 # Go back to src/
309 312 # self.changeDirectory('..')
... ...
src/majordome/Agent.py
... ... @@ -66,14 +66,6 @@ log = L.setupLogger("MajordomeTaskLogger", "Majordome")
66 66 """
67 67  
68 68  
69   -#class AGENT_MODE:
70   -STATUS_LAUNCH = "Launched"
71   -STATUS_INIT = "Initializing"
72   -STATUS_ACTIVE = "Active"
73   -STATUS_PAUSED = "Paused"
74   -STATUS_EXIT = "Exiting"
75   -
76   -
77 69  
78 70 class Agent:
79 71 # (EP) do this so that Majordome can be run from a thread, and called with thread.start():
... ... @@ -83,10 +75,34 @@ class Agent:
83 75 FOR_REAL = True
84 76 mainloop_waittime = 3
85 77 subloop_waittime = 2
86   - STATUS = STATUS_LAUNCH
87   -
88   - def __init__(self, name: str):
  78 + STATUS = None
  79 + MODE = None
  80 + config = None
  81 +
  82 + # Statuses
  83 + STATUS_LAUNCH = "LAUNCHED"
  84 + STATUS_INIT = "INITIALIZING"
  85 + STATUS_MAIN_LOOP = "IN_MAIN_LOOP"
  86 + STATUS_PROCESS_LOOP = "IN_PROCESS_LOOP"
  87 + STATUS_EXIT = "EXITING"
  88 +
  89 + # Modes
  90 + MODE_ACTIVE = "ACTIVE"
  91 + MODE_IDLE = "IDLE"
  92 +
  93 + def __init__(self, name:str=None, config_filename:str=None):
  94 + self.set_mode(self.MODE_IDLE)
  95 + self.set_status(self.STATUS_LAUNCH)
89 96 self.name = name
  97 + #config_filename = '/PROJECTS/GFT/SOFT/PYROS_SOFT/CURRENT/config/config_unit_simulunit1.xml'
  98 + if not config_filename:
  99 + #TODO: ajouter path pyros (pas en dur) : PYROS_PATH (from current dir)
  100 + config_filename = '/PROJECTS/GFT/SOFT/PYROS_SOFT/CURRENT/config/config_unit_simulunit1.xml'
  101 + # --- Instanciate an object for configuration
  102 + self.config = ConfigPyros(config_filename)
  103 + if self.config.get_last_errno() != self.config.NO_ERROR:
  104 + raise Exception("Bad config file name, error code " + str(self.config.get_last_errno()))
  105 +
90 106  
91 107 def __str__(self):
92 108 return "I am agent " + self.name
... ... @@ -98,6 +114,12 @@ class Agent:
98 114  
99 115 self.FOR_REAL = FOR_REAL
100 116  
  117 + self.load_config()
  118 + #TODO: lire param start_mode (si ce param, alors changer le mode)
  119 +
  120 + self.init()
  121 +
  122 + '''
101 123 print()
102 124 print(self)
103 125 print("FOR REAL ?", self.FOR_REAL)
... ... @@ -115,22 +137,20 @@ class Agent:
115 137 # except Config.ObjectDoesNotExist:
116 138 print("Config read (or write) exception", str(e))
117 139 return -1
118   -
119   - self.init()
120   -
121   - self.set_status(STATUS_ACTIVE)
  140 + '''
122 141  
123 142 # Main loop
124 143 while True:
  144 + self.set_status(self.STATUS_MAIN_LOOP)
125 145  
126 146 """
127 147 A chaque tour de boucle, remplir champ "iamalive" avec timestamp + durรฉe validitรฉ (> temps iteration, n minutes par dรฉfaut)
128   - + nom agent dans table agents_survey (status "IDLE", "RUNNING", "...")
  148 + + nom agent dans table agents_survey (nom agent + mode + status + updated timestamp)
129 149 """
130 150  
131 151 print()
132 152 print("Starting main loop iteration...")
133   -
  153 + self.show_mode_and_status()
134 154  
135 155 self.load_config()
136 156  
... ... @@ -138,7 +158,6 @@ class Agent:
138 158  
139 159 self.read_db_commands()
140 160  
141   - self.do_log()
142 161  
143 162 '''
144 163 if self.config.majordome_state == "STOP":
... ... @@ -151,34 +170,45 @@ class Agent:
151 170 '''
152 171  
153 172 # Sub-level loop (only if ACTIVE)
154   - if self.is_active() : self.core_process()
  173 + if self.is_active():
  174 + self.set_status(self.STATUS_PROCESS_LOOP)
  175 + self.core_process()
155 176  
156 177 self.waitfor(self.mainloop_waittime)
157 178  
158 179 print("Ending main loop iteration...")
159 180  
  181 + #self.do_log(LOG_DEBUG, "Ending main loop iteration")
  182 +
160 183  
161 184  
162 185 def waitfor(self, nbsec):
163 186 print(f"Now, waiting for {nbsec} seconds...")
164 187 time.sleep(nbsec)
165 188  
  189 + def set_status(self, status:str):
  190 + print(f"Switching from status {self.STATUS} to status {status}")
  191 + self.STATUS = status
  192 +
  193 + def set_mode(self, mode:str):
  194 + print(f"Switching from mode {self.MODE} to mode {mode}")
  195 + self.MODE = mode
  196 +
166 197 def is_active(self):
167   - return self.STATUS == STATUS_ACTIVE
  198 + return self.MODE == self.MODE_ACTIVE
168 199  
169 200 def set_active(self):
170   - self.STATUS = STATUS_ACTIVE
  201 + self.set_mode(self.MODE_ACTIVE)
171 202  
172   - def set_passive(self):
173   - self.STATUS = STATUS_PAUSED
  203 + def set_idle(self):
  204 + self.MODE = self.MODE_IDLE
174 205  
175   - def set_status(self, status:str):
176   - print(f"Switching from status {self.STATUS} to status {status}")
177   - self.STATUS = status
  206 + def show_mode_and_status(self):
  207 + print(f"CURRENT MODE is {self.MODE} (with status {self.STATUS})")
178 208  
179 209  
180 210 def die(self):
181   - self.set_status(STATUS_EXIT)
  211 + self.set_status(self.STATUS_EXIT)
182 212  
183 213 """
184 214 suspend/resume
... ... @@ -191,7 +221,7 @@ class Agent:
191 221 et lire table agents_command pour reprendre via resume,
192 222 et update la table agents_survey pour donner son status "idle"
193 223 """
194   - self.set_passive()
  224 + self.set_idle()
195 225 return True
196 226  
197 227 def resume(self):
... ... @@ -210,7 +240,8 @@ class Agent:
210 240  
211 241 def init(self):
212 242 print("Initializing...")
213   - self.set_status(STATUS_INIT)
  243 + self.set_status(self.STATUS_INIT)
  244 +
214 245  
215 246 def load_config(self):
216 247 """
... ... @@ -219,21 +250,14 @@ class Agent:
219 250 """
220 251 print("Loading the config file...")
221 252 #config_filename = 'c:/srv/develop/pyros/config/config_unit_simulunit1.xml'
222   - config_filename = '/PROJECTS/GFT/SOFT/PYROS_SOFT/CURRENT/config/config_unit_simulunit1.xml'
223   - # --- Instanciate an object for configuration
224   - config = ConfigPyros(config_filename)
225 253 #config.set_configfile(config_filename)
226   - if config.get_last_errno() != config.NO_ERROR:
227   - print("Error code = {}".format(config.get_last_errno()))
228   - else:
229   - # --- Load the configuration file if needed only (i.e. modified)
230   - config.load()
231   - # --- get the parameter value of <mount1/localization/home>
232   - instanciation = 'mount1'
233   - section = 'localization'
234   - key = 'home'
235   - paramvalue = config.get_paramvalue(instanciation, section, key)
236   - print("paramvalue <{}/{}/{}> = {}".format(instanciation, section, key,paramvalue))
  254 + self.config.load()
  255 + # --- get the parameter value of <mount1/localization/home>
  256 + instanciation = 'mount1'
  257 + section = 'localization'
  258 + key = 'home'
  259 + paramvalue = self.config.get_paramvalue(instanciation, section, key)
  260 + print("paramvalue <{}/{}/{}> = {}".format(instanciation, section, key,paramvalue))
237 261  
238 262 """
239 263 # self.config = Config.objects.get(pk=1)
... ...
src/majordome/AgentX.py
... ... @@ -53,8 +53,8 @@ log = L.setupLogger(&quot;MajordomeTaskLogger&quot;, &quot;Majordome&quot;)
53 53  
54 54 class AgentX(Agent):
55 55  
56   - def __init__(self, name):
57   - super().__init__(name)
  56 + def __init__(self, name, config_filename=None):
  57 + super().__init__(name, config_filename)
58 58  
59 59 # @override
60 60 def init(self):
... ...
src/majordome/start_agent_agentX.py
... ... @@ -4,6 +4,11 @@ import os
4 4 import sys
5 5 from django.conf import settings as djangosettings
6 6  
  7 +configfile = None
  8 +
  9 +if len(sys.argv) == 2:
  10 + configfile = sys.argv[1]
  11 +
7 12 # from AgentX import AgentX
8 13  
9 14  
... ... @@ -56,7 +61,7 @@ from majordome.AgentX import AgentX
56 61 # AgentX().run(FOR_REAL=False)
57 62 # AgentX().run()
58 63  
59   -agentx = AgentX(name="agentX")
  64 +agentx = AgentX(name="agentX", config_filename=configfile)
60 65  
61 66 # agentx.run(FOR_REAL=False)
62 67 agentx.run(FOR_REAL=True)
... ...