|
| 1 | +from manager.libs.process_utils import get_class, class_from_module |
| 2 | +from typing import Optional |
| 3 | +from pydantic import BaseModel |
| 4 | + |
| 5 | + |
| 6 | +from manager.libs.process_utils import get_class, class_from_module |
| 7 | +from manager.ram_logging.log_manager import LogManager |
| 8 | +from manager.manager.launcher.launcher_interface import ILauncher |
| 9 | + |
| 10 | +tools = { |
| 11 | + "console": { |
| 12 | + "module": "console", |
| 13 | + "display": ":1", |
| 14 | + "external_port": 1108, |
| 15 | + "internal_port": 5901, |
| 16 | + }, |
| 17 | + "gazebo": { |
| 18 | + "type": "module", |
| 19 | + "width": 1024, |
| 20 | + "height": 768, |
| 21 | + "module": "gazebo", |
| 22 | + "display": ":2", |
| 23 | + "external_port": 6080, |
| 24 | + "internal_port": 5900, |
| 25 | + }, |
| 26 | + "gzsim": { |
| 27 | + "type": "module", |
| 28 | + "width": 1024, |
| 29 | + "height": 768, |
| 30 | + "module": "gzsim", |
| 31 | + "display": ":2", |
| 32 | + "external_port": 6080, |
| 33 | + "internal_port": 5900, |
| 34 | + }, |
| 35 | + "web_gui": { |
| 36 | + "type": "module", |
| 37 | + "module": "web_gui", |
| 38 | + "internal_port": 2303, |
| 39 | + "consumer": None, |
| 40 | + }, |
| 41 | + "state_monitor": { |
| 42 | + "type": "module", |
| 43 | + "module": "state_monitor", |
| 44 | + "file": "/tmp/tree_state", |
| 45 | + "consumer": None, |
| 46 | + }, |
| 47 | +} |
| 48 | + |
| 49 | +simulator = { |
| 50 | + "gazebo": {"tool": "gazebo"}, |
| 51 | + "gz": {"tool": "gzsim"}, |
| 52 | +} |
| 53 | + |
| 54 | + |
| 55 | +class LauncherTools(BaseModel): |
| 56 | + module: str = ".".join(__name__.split(".")[:-1]) |
| 57 | + world_type: Optional[str] = None |
| 58 | + tools: list[str] |
| 59 | + tools_config: Optional[dict] = None |
| 60 | + launchers: Optional[ILauncher] = [] |
| 61 | + |
| 62 | + def run(self, consumer): |
| 63 | + for tool in self.tools: |
| 64 | + if tool == "simulator": |
| 65 | + tool = simulator[self.world_type]["tool"] |
| 66 | + module = tools[tool] |
| 67 | + launcher = self.launch_module(tool, module, consumer) |
| 68 | + self.launchers.append(launcher) |
| 69 | + |
| 70 | + def terminate(self): |
| 71 | + LogManager.logger.info("Terminating tools launcher") |
| 72 | + for launcher in self.launchers: |
| 73 | + if launcher.is_running(): |
| 74 | + launcher.terminate() |
| 75 | + self.launchers = [] |
| 76 | + |
| 77 | + def launch_module(self, name, configuration, consumer): |
| 78 | + def process_terminated(name, exit_code): |
| 79 | + LogManager.logger.info( |
| 80 | + f"LauncherEngine: {name} exited with code {exit_code}" |
| 81 | + ) |
| 82 | + if self.terminated_callback is not None: |
| 83 | + self.terminated_callback(name, exit_code) |
| 84 | + |
| 85 | + # Replace consumer |
| 86 | + if "consumer" in configuration: |
| 87 | + configuration["consumer"] = consumer |
| 88 | + |
| 89 | + launcher_module_name = configuration["module"] |
| 90 | + launcher_module = f"{self.module}.launcher_{launcher_module_name}.Launcher{class_from_module(launcher_module_name)}" |
| 91 | + launcher_class = get_class(launcher_module) |
| 92 | + config = None |
| 93 | + if self.tools_config is not None and name in self.tools_config: |
| 94 | + config = self.tools_config[name] |
| 95 | + |
| 96 | + launcher = launcher_class.from_config(launcher_class, configuration) |
| 97 | + launcher.run(config, process_terminated) |
| 98 | + return launcher |
| 99 | + |
| 100 | + def pause(self): |
| 101 | + for launcher in self.launchers: |
| 102 | + launcher.pause() |
| 103 | + |
| 104 | + def unpause(self): |
| 105 | + for launcher in self.launchers: |
| 106 | + launcher.unpause() |
| 107 | + |
| 108 | + def reset(self): |
| 109 | + for launcher in self.launchers: |
| 110 | + launcher.reset() |
| 111 | + |
| 112 | + def pass_msg(self, data): |
| 113 | + for launcher in self.launchers: |
| 114 | + if launcher.acceptsMsgs: |
| 115 | + launcher.get_msg(data) |
| 116 | + |
| 117 | + def launch_command(self, configuration): |
| 118 | + pass |
| 119 | + |
| 120 | + |
| 121 | +class LauncherToolsException(Exception): |
| 122 | + def __init__(self, message): |
| 123 | + super(LauncherToolsException, self).__init__(message) |
0 commit comments