Commit 5b835b38086e8ac1b066141ea4596889a28fbd7a

Authored by Etienne Pallier
1 parent d7c72772
Exists in dev

agentM

Showing 3 changed files with 179 additions and 1 deletions   Show diff stats
README.md
... ... @@ -73,7 +73,9 @@ Author: E. Pallier
73 73  
74 74 VERSION: 0.20.31
75 75  
76   -Comment: routine_process() : envoi commande n'est plus bloquant
  76 +Comment: AgentM
  77 +
  78 + - routine_process() : envoi commande n'est plus bloquant
77 79  
78 80 - Scenario de test :
79 81 - lancer agents A et B en mode simu (option -t): ./pyros.py -t start agentA,agentB
... ...
pyros.py
... ... @@ -28,6 +28,7 @@ AGENTS = {
28 28 "agentX" : "AgentX",
29 29 "agentA" : "AgentA",
30 30 "agentB" : "AgentB",
  31 + "agentM" : "AgentM",
31 32 "webserver" : "webserver",
32 33 "monitoring" : "monitoring",
33 34 "majordome" : "majordome",
... ...
src/agent/AgentM.py 0 → 100755
... ... @@ -0,0 +1,175 @@
  1 +#!/usr/bin/env python3
  2 +
  3 +import sys
  4 +##import utils.Logger as L
  5 +#import threading #, multiprocessing, os
  6 +#import time
  7 +
  8 +#from django.db import transaction
  9 +#from common.models import Command
  10 +
  11 +sys.path.append("..")
  12 +from agent.Agent import Agent, extract_parameters
  13 +
  14 +
  15 +
  16 +##log = L.setupLogger("AgentXTaskLogger", "AgentX")
  17 +
  18 +
  19 +
  20 +class AgentM(Agent):
  21 +
  22 +
  23 + # FOR TEST ONLY
  24 + # Run this agent in simulator mode
  25 + SIMULATOR_MODE = False
  26 + # Run the assertion tests at the end
  27 + SIMULATOR_WITH_TEST = True
  28 + SIMULATOR_MAX_DURATION_SEC = None
  29 + #SIMULATOR_MAX_DURATION_SEC = 120
  30 + '''
  31 + # Who should I send commands to ?
  32 + #SIMULATOR_COMMANDS_DEST = "myself"
  33 + SIMULATOR_COMMANDS_DEST = "AgentA"
  34 + # Scenario to be executed
  35 + SIMULATOR_COMMANDS_LIST = [
  36 + "go_active",
  37 + "go_idle",
  38 + "go_active",
  39 + "go_idle",
  40 + "go_active",
  41 + "go_idle",
  42 + "exit",
  43 + ]
  44 + '''
  45 +
  46 + """
  47 + =================================================================
  48 + FUNCTIONS RUN INSIDE MAIN THREAD
  49 + =================================================================
  50 + """
  51 +
  52 + # @override
  53 + def __init__(self, name:str=None, config_filename=None, RUN_IN_THREAD=True):
  54 + if name is None: name = self.__class__.__name__
  55 + super().__init__(name, config_filename, RUN_IN_THREAD)
  56 +
  57 + # @override
  58 + def init(self):
  59 + super().init()
  60 + # --- Set the mode according the startmode value
  61 + ##agent_alias = self.__class__.__name__
  62 + ##self.set_mode_from_config(agent_alias)
  63 +
  64 + '''
  65 + # @override
  66 + def load_config(self):
  67 + super().load_config()
  68 + '''
  69 +
  70 + '''
  71 + # @override
  72 + def update_survey(self):
  73 + super().update_survey()
  74 + '''
  75 +
  76 + '''
  77 + # @override
  78 + def get_next_command(self):
  79 + return super().get_next_command()
  80 + '''
  81 +
  82 + # @override
  83 + def do_log(self):
  84 + super().do_log()
  85 +
  86 +
  87 +
  88 + """
  89 + =================================================================
  90 + FUNCTIONS RUN INSIDE A SUB-THREAD (OR A PROCESS) (thread_*())
  91 + =================================================================
  92 + """
  93 +
  94 + # Define your own command step(s) here
  95 + def cmd_step1(self, step:int):
  96 + cmd = self._current_specific_cmd
  97 + cmd.result = f"in step #{step}/{self._thread_total_steps_number}"
  98 + cmd.save()
  99 + """
  100 + if self.RUN_IN_THREAD:
  101 + print("(save from thread)")
  102 + cmd.save()
  103 + else:
  104 + #@transaction.atomic
  105 + print("(save from process)")
  106 + with transaction.atomic():
  107 + cmd.save()
  108 + #Command.objects.select_for_update()
  109 + """
  110 +
  111 + def cmd_step2(self, step:int):
  112 + self.cmd_step1(step)
  113 + def cmd_step3(self, step:int):
  114 + self.cmd_step1(step)
  115 + def cmd_step4(self, step:int):
  116 + self.cmd_step1(step)
  117 +
  118 + """
  119 + # @override
  120 + def thread_exec_specific_cmd_step(self, step:int, sleep_time:float=1.0):
  121 + self.thread_stop_if_asked()
  122 + cmd = self._current_specific_cmd
  123 + print(f">>>>> Thread (cmd {cmd.name}): step #{step}/5")
  124 + self.sleep(sleep_time)
  125 + """
  126 +
  127 + '''
  128 + # @override
  129 + def exec_specific_cmd_start(self, cmd:Command, from_thread=True):
  130 + super().exec_specific_cmd_start(cmd, from_thread)
  131 + '''
  132 +
  133 +
  134 + # @override
  135 + def thread_exec_specific_cmd_main(self):
  136 + # This is optional
  137 + self.thread_set_total_steps_number(5)
  138 +
  139 + # HERE, write your own scenario
  140 +
  141 + # scenario OK
  142 + self.thread_exec_specific_cmd_step(1, self.cmd_step1, 1)
  143 + self.thread_exec_specific_cmd_step(2, self.cmd_step2, 3)
  144 + self.thread_exec_specific_cmd_step(3, self.cmd_step1, 5)
  145 + self.thread_exec_specific_cmd_step(4, self.cmd_step3, 10)
  146 + self.thread_exec_specific_cmd_step(5, self.cmd_step1, 4)
  147 + # ... as many as you need
  148 +
  149 + """ autre scenario
  150 + self.thread_exec_specific_cmd_step(1, self.cmd_step1, 1)
  151 + self.thread_exec_specific_cmd_step(2, self.cmd_step2, 2)
  152 + self.thread_exec_specific_cmd_step(3, self.cmd_step1, 2)
  153 + self.thread_exec_specific_cmd_step(4, self.cmd_step3, 2)
  154 + self.thread_exec_specific_cmd_step(5, self.cmd_step1, 3)
  155 + """
  156 +
  157 + '''
  158 + # @override
  159 + def exec_specific_cmd_end(self, cmd:Command, from_thread=True):
  160 + super().exec_specific_cmd_end(cmd, from_thread)
  161 + '''
  162 +
  163 +
  164 +if __name__ == "__main__":
  165 +
  166 + # with thread
  167 + RUN_IN_THREAD=True
  168 + # with process
  169 + #RUN_IN_THREAD=False
  170 +
  171 + TEST_MODE, configfile = extract_parameters()
  172 + agent = AgentM("AgentM", configfile, RUN_IN_THREAD)
  173 + agent.setSimulatorMode(TEST_MODE)
  174 + print(agent)
  175 + agent.run()
... ...