Commit 1ac4cec045ada1f5ab02617ed5a657d8e76f7da1

Authored by Alexis Koralewski
1 parent 828ea9fe
Exists in dev

Cleaning agentMCO

privatedev/plugin/agent/AgentMCO.py 0 → 100644
... ... @@ -0,0 +1,129 @@
  1 +#!/usr/bin/env python3
  2 +
  3 +
  4 +import subprocess
  5 +import sys
  6 +import os
  7 +##import utils.Logger as L
  8 +
  9 +pwd = os.environ['PROJECT_ROOT_PATH']
  10 +if pwd not in sys.path:
  11 + sys.path.append(pwd)
  12 +
  13 +##from .Agent import Agent
  14 +from Agent import Agent, build_agent
  15 +
  16 +##log = L.setupLogger("AgentXTaskLogger", "AgentX")
  17 +
  18 +
  19 +
  20 +class AgentMCO(Agent):
  21 +
  22 + # FOR TEST ONLY
  23 + # Run this agent in simulator mode
  24 + TEST_MODE = False
  25 + # Run the assertion tests at the end
  26 + TEST_WITH_FINAL_TEST = False
  27 + #TEST_MAX_DURATION_SEC = None
  28 + TEST_MAX_DURATION_SEC = 100
  29 + # Who should I send commands to ?
  30 + TEST_COMMANDS_DEST = "myself"
  31 +
  32 + AGENT_SPECIFIC_COMMANDS = [
  33 + ("do_process_image",10,0),
  34 + ]
  35 +
  36 + TEST_COMMANDS_LIST = [
  37 + ("self do_process_image i.fit", 200, None, "CMD_EXECUTED"),
  38 + ("self do_exit", 500, "STOPPING", "CMD_EXECUTED"),
  39 + ]
  40 + """
  41 + =================================================================
  42 + FUNCTIONS RUN INSIDE MAIN THREAD
  43 + =================================================================
  44 + """
  45 +
  46 + def __init__(self, name:str=None):
  47 + if name is None:
  48 + name = self.__class__.__name__
  49 + super().__init__()
  50 + #super().__init__(RUN_IN_THREAD)
  51 +
  52 +
  53 +
  54 + # @override
  55 + def init(self):
  56 + super().init()
  57 + self.processes = []
  58 + mco_folder = self.get_config().get_agent_information(self.get_config().unit_name,self.name)["path"]
  59 + mco_folder_abs_path = pwd + "/" + mco_folder + "/"
  60 + self.mco_folder_abs_path = mco_folder_abs_path
  61 +
  62 + if not os.path.exists(mco_folder_abs_path+"output/"):
  63 + os.makedirs(mco_folder_abs_path+"output/")
  64 + if not os.path.exists(mco_folder_abs_path+"input/"):
  65 + os.makedirs(mco_folder_abs_path+"input/")
  66 + self.output_folder = mco_folder_abs_path+"output/"
  67 + self.input_folder = mco_folder_abs_path+"input/"
  68 +
  69 + def do_process_image(self, image_name:str):
  70 + image_path = self.input_folder + image_name
  71 + output_file_name = image_name.split(".")[0] + ".json"
  72 + output_file_path = self.output_folder + output_file_name
  73 + if os.path.exists(image_path):
  74 + cmd = "python3 " + self.mco_folder_abs_path + "estimation_FWHM.py -i " + image_path + " -o " + self.output_folder
  75 + print(f"Running cmd {cmd}")
  76 + process = subprocess.Popen(cmd,shell=True)
  77 + self.processes.append(process)
  78 + cmd = "python3 " + self.mco_folder_abs_path + "image_stat.py -i " + image_path + " -o " + self.output_folder
  79 + print(f"Running cmd {cmd}")
  80 + process = subprocess.Popen(cmd,shell=True)
  81 + self.processes.append(process)
  82 + for process in self.processes:
  83 + process.wait()
  84 + else:
  85 + print(f"Can't find image {image_path}")
  86 + #@override
  87 + def main_loop_start(self):
  88 + print("LOOP START")
  89 +
  90 + #@override
  91 + def main_loop_end(self):
  92 + print("LOOP END");
  93 +
  94 + '''
  95 + # @override
  96 + def load_config(self):
  97 + super().load_config()
  98 + '''
  99 +
  100 + '''
  101 + # @override
  102 + def update_survey(self):
  103 + super().update_survey()
  104 + '''
  105 +
  106 + '''
  107 + # @override
  108 + def get_next_command(self):
  109 + return super().get_next_command()
  110 + '''
  111 +
  112 + # @override
  113 + def do_log(self):
  114 + super().do_log()
  115 +
  116 + # @override
  117 + def do_things_before_exit(self, stopper_agent_name=None):
  118 + print("AgentMCO fait quelques trucs à lui avant de stopper...")
  119 +
  120 +"""
  121 +=================================================================
  122 + MAIN FUNCTION
  123 +=================================================================
  124 +"""
  125 +if __name__ == "__main__":
  126 +
  127 + agent = build_agent(AgentMCO)
  128 +
  129 + agent.run()
... ...
src/core/pyros_django/agent/AgentMCO.py
... ... @@ -28,8 +28,6 @@ class AgentMCO(Agent):
28 28 TEST_MAX_DURATION_SEC = 100
29 29 # Who should I send commands to ?
30 30 TEST_COMMANDS_DEST = "myself"
31   - #TEST_COMMANDS_DEST = "AgentB"
32   - # Scenario to be executed
33 31  
34 32 AGENT_SPECIFIC_COMMANDS = [
35 33 ("do_process_image",10,0),
... ... @@ -45,16 +43,6 @@ class AgentMCO(Agent):
45 43 =================================================================
46 44 """
47 45  
48   - # @override
49   - '''
50   - #def __init__(self, name:str=None, config_filename=None, RUN_IN_THREAD=True):
51   - def __init__(self, config_filename=None):
52   - ##if name is None: name = self.__class__.__name__
53   - #super().__init__(name, config_filename, RUN_IN_THREAD)
54   - super().__init__(config_filename)
55   - #self._log.print(f"init done for {name}")
56   - self._log.print("init done")
57   - '''
58 46 def __init__(self, name:str=None):
59 47 if name is None:
60 48 name = self.__class__.__name__
... ... @@ -80,7 +68,6 @@ class AgentMCO(Agent):
80 68  
81 69 def do_process_image(self, image_name:str):
82 70 image_path = self.input_folder + image_name
83   - print(image_path)
84 71 output_file_name = image_name.split(".")[0] + ".json"
85 72 output_file_path = self.output_folder + output_file_name
86 73 if os.path.exists(image_path):
... ... @@ -128,7 +115,7 @@ class AgentMCO(Agent):
128 115  
129 116 # @override
130 117 def do_things_before_exit(self, stopper_agent_name=None):
131   - print("AgentBasic fait quelques trucs à lui avant de stopper...")
  118 + print("AgentMCO fait quelques trucs à lui avant de stopper...")
132 119  
133 120 """
134 121 =================================================================
... ... @@ -139,11 +126,4 @@ if __name__ == "__main__":
139 126  
140 127 agent = build_agent(AgentMCO)
141 128  
142   - '''
143   - TEST_MODE, configfile = extract_parameters()
144   - #agent = AgentX()
145   - agent = AgentA("AgentA", configfile, RUN_IN_THREAD)
146   - agent.setSimulatorMode(TEST_MODE)
147   - print(agent)
148   - '''
149 129 agent.run()
... ...