|
| 1 | +# SPDX-FileCopyrightText: 2025-present CodeMagic LTD |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +from pathlib import Path |
| 6 | +from typing import Any, Optional |
| 7 | + |
| 8 | +from .__version__ import get_version |
| 9 | +from .constants import DEFAULT_WS_URL |
| 10 | +from .file_ops import upload, upload_file |
| 11 | +from .protocol_types import ResponseMessage |
| 12 | +from .serial import monitor_lines |
| 13 | +from .simulation import pause, restart, resume, start |
| 14 | +from .transport import Transport |
| 15 | + |
| 16 | + |
| 17 | +class WokwiClient: |
| 18 | + version: str |
| 19 | + |
| 20 | + def __init__(self, token: str, server: Optional[str] = None): |
| 21 | + self.version = get_version() |
| 22 | + self._transport = Transport(token, server or DEFAULT_WS_URL) |
| 23 | + |
| 24 | + async def connect(self) -> dict[str, Any]: |
| 25 | + return await self._transport.connect() |
| 26 | + |
| 27 | + async def disconnect(self) -> None: |
| 28 | + await self._transport.close() |
| 29 | + |
| 30 | + async def upload(self, name: str, content: bytes) -> ResponseMessage: |
| 31 | + return await upload(self._transport, name, content) |
| 32 | + |
| 33 | + async def upload_file( |
| 34 | + self, filename: str, local_path: Optional[Path] = None |
| 35 | + ) -> ResponseMessage: |
| 36 | + return await upload_file(self._transport, filename, local_path) |
| 37 | + |
| 38 | + async def start_simulation(self, **kwargs: Any) -> ResponseMessage: |
| 39 | + return await start(self._transport, **kwargs) |
| 40 | + |
| 41 | + async def pause_simulation(self) -> ResponseMessage: |
| 42 | + return await pause(self._transport) |
| 43 | + |
| 44 | + async def resume_simulation(self, pause_after: Optional[int] = None) -> ResponseMessage: |
| 45 | + return await resume(self._transport, pause_after) |
| 46 | + |
| 47 | + async def restart_simulation(self, pause: bool = False) -> ResponseMessage: |
| 48 | + return await restart(self._transport, pause) |
| 49 | + |
| 50 | + async def serial_monitor_cat(self) -> None: |
| 51 | + async for line in monitor_lines(self._transport): |
| 52 | + print(line.decode("utf-8"), end="", flush=True) |
0 commit comments