Commit 97331a1171f1da8d3f3f91ff8629ec1dd4e71144

Authored by Etienne Pallier
1 parent 55831fe5
Exists in dev

Simplification des MODE_CHOICES ET STATUS_CHOICES dans AgentSurvey et dans Agent - CLEANUP

src/core/pyros_django/agent/Agent.py
... ... @@ -607,7 +607,7 @@ class Agent:
607 607 self.unit = None
608 608 self.TEST_COMMANDS = None
609 609  
610   - #print(AgentSurvey.MODE_IDLE)
  610 + #print(AgentSurvey.MODE_CHOICES.IDLE)
611 611 #sys.exit()
612 612  
613 613 # Agent is by default in mode ATTENTIVE (most active mode)
... ... @@ -642,7 +642,7 @@ class Agent:
642 642  
643 643 self.TEST_COMMANDS = iter(self.TEST_COMMANDS_LIST)
644 644 ##self.RUN_IN_THREAD = RUN_IN_THREAD
645   - self._set_status(AgentSurvey.STATUS_LAUNCH)
  645 + self._set_status(AgentSurvey.STATUS_CHOICES.LAUNCHED)
646 646 ####self._set_idle()
647 647  
648 648 # Create 1st survey if none
... ... @@ -964,7 +964,7 @@ class Agent:
964 964 """
965 965  
966 966 ("in run()")
967   - self._set_and_log_status(AgentSurvey.STATUS_LAUNCH)
  967 + self._set_and_log_status(AgentSurvey.STATUS_CHOICES.LAUNCHED)
968 968  
969 969 #return
970 970  
... ... @@ -1041,7 +1041,7 @@ class Agent:
1041 1041  
1042 1042 def _main_loop(self, nb_iter:int=None, FOR_REAL:bool=True):
1043 1043  
1044   - self._set_and_log_status(AgentSurvey.STATUS_MAIN_LOOP)
  1044 + self._set_and_log_status(AgentSurvey.STATUS_CHOICES.IN_MAIN_LOOP)
1045 1045  
1046 1046 self._main_loop_start(nb_iter)
1047 1047 #if not self.DO_MAIN_LOOP: return
... ... @@ -1074,15 +1074,17 @@ class Agent:
1074 1074 cmd = self._process_next_command_if_exists()
1075 1075 #print(cmd)
1076 1076 except (
  1077 + # PENDING (non running) cmd exceptions
1077 1078 CmdUnimplementedException, CmdBadArgsException, CmdUnknownException,
  1079 + # RUNNING cmd exceptions
1078 1080 CmdExecErrorException, CmdExecTimeoutException, CmdExecKilledException
1079 1081 ) as e :
1080   - print(e)
  1082 + #print(e)
1081 1083 log.error(f"EXCEPTION on Agent command '{e.cmd_name}'")
1082 1084 if isinstance(e.cmd, AgentCmd) :
1083 1085 cmd = e.cmd
1084 1086 if type(e) is CmdUnimplementedException:
1085   - if cmd.name is not "get_specific_cmds":
  1087 + if cmd.name != "get_specific_cmds":
1086 1088 cmd.set_as_unimplemented("EXCEPTION: command known but unimplemented")
1087 1089 if type(e) in (CmdBadArgsException, CmdUnknownException):
1088 1090 cmd.set_as_invalid("EXCEPTION: command unknown or bad args")
... ... @@ -1164,7 +1166,7 @@ class Agent:
1164 1166 self.DO_MAIN_LOOP = False
1165 1167 return
1166 1168  
1167   - ##self._set_status(AgentSurvey.STATUS_MAIN_LOOP)
  1169 + ##self._set_status(AgentSurvey.STATUS_CHOICES.IN_MAIN_LOOP)
1168 1170 self.show_mode_and_status()
1169 1171  
1170 1172 self.main_loop_start()
... ... @@ -1205,7 +1207,7 @@ class Agent:
1205 1207 def _process_next_command_if_exists(self)->Union[AgentCmd,None]:
1206 1208 ''' Processing the next pending command if exists '''
1207 1209  
1208   - self._set_and_log_status(AgentSurvey.STATUS_GET_NEXT_COMMAND)
  1210 + self._set_and_log_status(AgentSurvey.STATUS_CHOICES.IN_GET_NEXT_CMD)
1209 1211  
1210 1212 #print()
1211 1213 #print()
... ... @@ -1283,7 +1285,7 @@ class Agent:
1283 1285 self._cleanup_before_exit()
1284 1286 if cmd.name != 'do_restart_loop':
1285 1287 self.DO_RESTART_LOOP = False
1286   - self._set_status(AgentSurvey.STATUS_EXIT)
  1288 + self._set_status(AgentSurvey.STATUS_CHOICES.EXITING)
1287 1289 # log last agent mode & status
1288 1290 self._log_agent_state()
1289 1291 #cmd.set_result('EXITING')
... ... @@ -1391,7 +1393,7 @@ class Agent:
1391 1393 print()
1392 1394 log.info("*"*10+ " ROUTINE PROCESSING BEFORE (START) "+ "*"*10+ '\n')
1393 1395  
1394   - self._set_and_log_status(AgentSurvey.STATUS_ROUTINE_BEFORE_PROCESS)
  1396 + self._set_and_log_status(AgentSurvey.STATUS_CHOICES.IN_ROUTINE_BEF)
1395 1397 self.routine_process_before_body()
1396 1398  
1397 1399 print()
... ... @@ -1413,7 +1415,7 @@ class Agent:
1413 1415 print()
1414 1416 log.info("*"*10+ " ROUTINE PROCESSING AFTER (START) "+ "*"*10+ '\n')
1415 1417  
1416   - self._set_and_log_status(AgentSurvey.STATUS_ROUTINE_AFTER_PROCESS)
  1418 + self._set_and_log_status(AgentSurvey.STATUS_CHOICES.IN_ROUTINE_AFT)
1417 1419 self.routine_process_after_body()
1418 1420  
1419 1421 print()
... ... @@ -1543,9 +1545,9 @@ class Agent:
1543 1545 return self.mode
1544 1546  
1545 1547 # Test mode
1546   - def IS_MODE_IDLE(self): return self.get_mode() == AgentSurvey.MODE_IDLE
1547   - def IS_MODE_ROUTINE(self): return self.get_mode() == AgentSurvey.MODE_ROUTINE
1548   - def IS_MODE_ATTENTIVE(self): return self.get_mode() == AgentSurvey.MODE_ATTENTIVE
  1548 + def IS_MODE_IDLE(self): return self.get_mode() == AgentSurvey.MODE_CHOICES.IDLE
  1549 + def IS_MODE_ROUTINE(self): return self.get_mode() == AgentSurvey.MODE_CHOICES.ROUTINE
  1550 + def IS_MODE_ATTENTIVE(self): return self.get_mode() == AgentSurvey.MODE_CHOICES.ATTENTIVE
1549 1551 # @deprecated
1550 1552 def IS_IDLE(self): return self.IS_MODE_IDLE()
1551 1553 # @deprecated
... ... @@ -1559,19 +1561,20 @@ class Agent:
1559 1561 self.mode = mode
1560 1562  
1561 1563 def _set_mode_idle(self):
1562   - self._set_mode(AgentSurvey.MODE_IDLE)
  1564 + self._set_mode(AgentSurvey.MODE_CHOICES.IDLE)
1563 1565 # @deprecated
1564 1566 def set_idle(self):
1565 1567 self._set_mode_idle()
1566 1568  
1567 1569 def _set_mode_routine(self):
1568   - self._set_mode(AgentSurvey.MODE_ROUTINE)
  1570 + self._set_mode(AgentSurvey.MODE_CHOICES.ROUTINE)
1569 1571 # @deprecated
1570 1572 def set_routine(self):
1571 1573 self._set_mode_routine()
1572 1574  
1573 1575 def _set_mode_attentive(self):
1574   - self._set_mode(AgentSurvey.MODE_ATTENTIVE)
  1576 + #self._set_mode(AgentSurvey.MODE_ATTENTIVE)
  1577 + self._set_mode(AgentSurvey.MODE_CHOICES.ATTENTIVE)
1575 1578 # @deprecated
1576 1579 def set_attentive(self):
1577 1580 self._set_mode_attentive()
... ... @@ -1649,7 +1652,7 @@ class Agent:
1649 1652 def init(self):
1650 1653 #log.debug("*** Initializing... ***")
1651 1654 log.info("*** INITIALIZING... ***")
1652   - self._set_and_log_status(AgentSurvey.STATUS_INIT)
  1655 + self._set_and_log_status(AgentSurvey.STATUS_CHOICES.INITIALIZING)
1653 1656 if self.TEST_MODE: self.set_delay(2)
1654 1657  
1655 1658  
... ... @@ -1830,7 +1833,7 @@ class Agent:
1830 1833 which is relevant to this agent.
1831 1834 Commands are read in chronological order
1832 1835 """
1833   - self._set_status(AgentSurvey.STATUS_GET_NEXT_COMMAND)
  1836 + self._set_status(AgentSurvey.STATUS_CHOICES.IN_GET_NEXT_CMD)
1834 1837 log.info("Looking for a new command to process (sent by another agent):")
1835 1838  
1836 1839 # 1) Get all pending commands for me (return if None)
... ...
src/core/pyros_django/common/models.py
... ... @@ -328,60 +328,22 @@ class AgentSurvey(models.Model):
328 328 - MODE_ATTENTIVE : idem ROUTINE + executes Agent level SPECIFIC commands (commands specific to this agent, that only this agent understands and can execute)
329 329 Default mode is MODE_ATTENTIVE (most active mode)
330 330 '''
331   - #MODE_ACTIVE = "ACTIVE"
332   - MODE_IDLE = "IDLE"
333   - MODE_ROUTINE = "ROUTINE"
334   - MODE_ATTENTIVE = "ATTENTIVE"
335   - '''
336   - (MODE_ACTIVE, 'Active mode'),
337   - (MODE_IDLE, 'Idle mode'),
338   - '''
339   - '''
340   - (MODE_IDLE, 'IDLE'),
341   - (MODE_ROUTINE, 'ROUTINE'),
342   - (MODE_ATTENTIVE, 'ATTENTIVE'),
343   - '''
344   - MODE_CHOICES = (
345   - (MODE_IDLE, MODE_IDLE),
346   - (MODE_ROUTINE, MODE_ROUTINE),
347   - (MODE_ATTENTIVE, MODE_ATTENTIVE),
  331 + MODE_CHOICES = Choices(
  332 + "IDLE",
  333 + "ROUTINE",
  334 + "ATTENTIVE",
348 335 )
349 336  
350   -
351 337 # --- STATUSES ---
352   -
353   - #STATUSES = Choices('new', 'verified', 'published')
354   - STATUS_LAUNCH = "LAUNCHED"
355   - STATUS_INIT = "INITIALIZING"
356   - STATUS_MAIN_LOOP = "IN_MAIN_LOOP"
357   - STATUS_ROUTINE_BEFORE_PROCESS = "IN_ROUTINE_BEF"
358   - STATUS_GET_NEXT_COMMAND = "IN_GET_NEXT_CMD"
359   - STATUS_ROUTINE_AFTER_PROCESS = "IN_ROUTINE_AFT"
360   - STATUS_EXIT = "EXITING"
361   - # NEW
362   - ##STATUS_RESTART = "RESTARTING"
363   - #STATUS_GENERAL_PROCESS = "IN_GENERAL_PROCESS"
364   - ###STATUS_SPECIFIC_PROCESS = "IN_SPECIFIC_PROCESS"
365   - '''
366   - STATUS_CHOICES = (
367   - (STATUS_LAUNCH, "LAUNCHED"),
368   - (STATUS_INIT, "INITIALIZING"),
369   - (STATUS_MAIN_LOOP, "IN_MAIN_LOOP"),
370   - (STATUS_ROUTINE_BEFORE_PROCESS, "IN_ROUTINE_BEF"),
371   - (STATUS_GET_NEXT_COMMAND, "IN_GET_NEXT_CMD"),
372   - (STATUS_ROUTINE_AFTER_PROCESS, "IN_ROUTINE_AFT"),
373   - (STATUS_EXIT, "EXITING"),
374   - )
375   - '''
376   - STATUS_CHOICES = (
377   - #(STATUS_LAUNCH, "LAUNCHED"),
378   - (STATUS_LAUNCH, STATUS_LAUNCH),
379   - (STATUS_INIT, STATUS_INIT),
380   - (STATUS_MAIN_LOOP, STATUS_MAIN_LOOP),
381   - (STATUS_ROUTINE_BEFORE_PROCESS, STATUS_ROUTINE_BEFORE_PROCESS),
382   - (STATUS_GET_NEXT_COMMAND, STATUS_GET_NEXT_COMMAND),
383   - (STATUS_ROUTINE_AFTER_PROCESS, STATUS_ROUTINE_AFTER_PROCESS),
384   - (STATUS_EXIT, STATUS_EXIT),
  338 + ''' Agent steps '''
  339 + STATUS_CHOICES = Choices(
  340 + 'LAUNCHED',
  341 + 'INITIALIZING',
  342 + 'IN_MAIN_LOOP',
  343 + 'IN_ROUTINE_BEF',
  344 + 'IN_GET_NEXT_CMD',
  345 + 'IN_ROUTINE_AFT',
  346 + 'EXITING',
385 347 )
386 348  
387 349 name = models.CharField(max_length=50, unique=True)
... ... @@ -392,7 +354,9 @@ class AgentSurvey(models.Model):
392 354 validity_duration = models.PositiveIntegerField(default=90)
393 355 #validity_duration = models.DurationField(default=90)
394 356 mode = models.CharField('agent mode', max_length=15,
395   - blank=True, choices=MODE_CHOICES)
  357 + blank=True, choices=MODE_CHOICES,
  358 + #default = MODE_CHOICES.ATTENTIVE
  359 + )
396 360 status = models.CharField(
397 361 max_length=15, blank=True, choices=STATUS_CHOICES)
398 362 iteration = models.IntegerField(blank=True, null=True)
... ...