|
| 1 | +import asyncio |
| 2 | + |
| 3 | +import typer |
| 4 | +from rich.console import Console |
| 5 | +from rich.theme import Theme |
| 6 | + |
| 7 | +from antares import AntaresClient, ShipConfig |
| 8 | +from antares.config_loader import load_config |
| 9 | + |
| 10 | +app = typer.Typer(help="Antares Simulation CLI") |
| 11 | + |
| 12 | +console = Console( |
| 13 | + theme=Theme( |
| 14 | + { |
| 15 | + "info": "green", |
| 16 | + "warn": "yellow", |
| 17 | + "error": "bold red", |
| 18 | + } |
| 19 | + ) |
| 20 | +) |
| 21 | + |
| 22 | + |
| 23 | +def build_client(config_path: str | None, verbose: bool) -> AntaresClient: |
| 24 | + settings = load_config(config_path) |
| 25 | + |
| 26 | + if verbose: |
| 27 | + console.print(f"[info]Using settings: {settings.model_dump()}") |
| 28 | + |
| 29 | + return AntaresClient( |
| 30 | + base_url=settings.base_url, |
| 31 | + tcp_host=settings.tcp_host, |
| 32 | + tcp_port=settings.tcp_port, |
| 33 | + timeout=settings.timeout, |
| 34 | + auth_token=settings.auth_token, |
| 35 | + ) |
| 36 | + |
| 37 | + |
| 38 | +@app.command() |
| 39 | +def reset( |
| 40 | + config: str = typer.Option(None, help="Path to config TOML file"), |
| 41 | + verbose: bool = typer.Option(False, "--verbose", "-v"), |
| 42 | +): |
| 43 | + """Reset the simulation.""" |
| 44 | + client = build_client(config, verbose) |
| 45 | + client.reset_simulation() |
| 46 | + console.print("[info]✅ Simulation reset.") |
| 47 | + |
| 48 | + |
| 49 | +@app.command() |
| 50 | +def add_ship( |
| 51 | + x: float, |
| 52 | + y: float, |
| 53 | + config: str = typer.Option(None, help="Path to config TOML file"), |
| 54 | + verbose: bool = typer.Option(False, "--verbose", "-v"), |
| 55 | +): |
| 56 | + """Add a ship to the simulation at (x, y).""" |
| 57 | + client = build_client(config, verbose) |
| 58 | + ship = ShipConfig(initial_position=(x, y)) |
| 59 | + client.add_ship(ship) |
| 60 | + console.print(f"[info]🚢 Added ship at ({x}, {y})") |
| 61 | + |
| 62 | + |
| 63 | +@app.command() |
| 64 | +def subscribe( |
| 65 | + config: str = typer.Option(None, help="Path to config TOML file"), |
| 66 | + verbose: bool = typer.Option(False, "--verbose", "-v"), |
| 67 | +): |
| 68 | + """Subscribe to simulation data stream.""" |
| 69 | + client = build_client(config, verbose) |
| 70 | + |
| 71 | + async def _sub(): |
| 72 | + async for event in client.subscribe(): |
| 73 | + console.print_json(data=event) |
| 74 | + |
| 75 | + asyncio.run(_sub()) |
| 76 | + |
| 77 | + |
| 78 | +if __name__ == "__main__": |
| 79 | + app() |
0 commit comments