Send a command and return the result. Args: command: Command name (e.g., "io.getStatus") params: Optional parameters dict timeout: Optional timeout override Returns: Result dict on success Raises: APIErro
(
self,
command: str,
params: Optional[dict] = None,
timeout: Optional[float] = None,
)
| 119 | self._socket.sendall(data.encode("utf-8")) |
| 120 | |
| 121 | def command( |
| 122 | self, |
| 123 | command: str, |
| 124 | params: Optional[dict] = None, |
| 125 | timeout: Optional[float] = None, |
| 126 | ) -> dict: |
| 127 | """ |
| 128 | Send a command and return the result. |
| 129 | |
| 130 | Args: |
| 131 | command: Command name (e.g., "io.getStatus") |
| 132 | params: Optional parameters dict |
| 133 | timeout: Optional timeout override |
| 134 | |
| 135 | Returns: |
| 136 | Result dict on success |
| 137 | |
| 138 | Raises: |
| 139 | APIError: If command fails |
| 140 | TimeoutError: If response not received in time |
| 141 | """ |
| 142 | request_id = str(uuid.uuid4()) |
| 143 | message = {"type": "command", "id": request_id, "command": command} |
| 144 | |
| 145 | if params: |
| 146 | message["params"] = params |
| 147 | |
| 148 | self._send_message(message) |
| 149 | |
| 150 | deadline = time.time() + (timeout or self.timeout) |
| 151 | |
| 152 | while True: |
| 153 | remaining = deadline - time.time() |
| 154 | if remaining <= 0: |
| 155 | raise TimeoutError("Timeout waiting for response") |
| 156 | |
| 157 | response = self._recv_message(remaining) |
| 158 | |
| 159 | if response.get("type") == "response" and response.get("id") == request_id: |
| 160 | if response.get("success"): |
| 161 | return response.get("result", {}) |
| 162 | else: |
| 163 | error = response.get("error", {}) |
| 164 | raise APIError( |
| 165 | error.get("code", "UNKNOWN"), |
| 166 | error.get("message", "Unknown error"), |
| 167 | ) |
| 168 | |
| 169 | def batch( |
| 170 | self, commands: list[dict], timeout: Optional[float] = None |