diff --git a/src/ppclock/bridge.py b/src/ppclock/bridge.py index 292d7b8..b7e542a 100644 --- a/src/ppclock/bridge.py +++ b/src/ppclock/bridge.py @@ -76,7 +76,8 @@ class BridgeTransport: if not devices: raise BridgeError(f"bridge 未发现 {self.name_prefix!r} 前缀设备") self.address = devices[0]["address"] - await self._rpc("connect", address=self.address, timeout=self.timeout) + await self._rpc("connect", 90.0 + self.timeout, + address=self.address, timeout=self.timeout) except Exception: await self._teardown() raise @@ -122,15 +123,16 @@ class BridgeTransport: fut.set_exception(BridgeError("bridge 连接中断")) self._pending.clear() - async def _rpc(self, op: str, timeout: float = 30.0, **kw) -> dict: + async def _rpc(self, op: str, _timeout: float = 30.0, **kw) -> dict: self._next_id += 1 rid = self._next_id fut = asyncio.get_running_loop().create_future() self._pending[rid] = fut - self._writer.write(json.dumps({"id": rid, "op": op, **kw}).encode() + b"\n") + payload = {"id": rid, "op": op, **kw} + self._writer.write(json.dumps(payload).encode() + b"\n") await self._writer.drain() try: - resp = await asyncio.wait_for(fut, timeout) + resp = await asyncio.wait_for(fut, _timeout) finally: self._pending.pop(rid, None) if not resp.get("ok"): @@ -147,12 +149,13 @@ class BridgeTransport: return bytes.fromhex((await self._rpc("read", char="rxtx"))["data"]) async def request_device_id(self, timeout: float = 8.0) -> str: - return (await self._rpc("request_device_id", timeout=timeout + 5))["device_id"] + return (await self._rpc("request_device_id", timeout + 15, + timeout=timeout))["device_id"] async def run_ota(self, image: bytes, on_progress=None): self._on_progress = on_progress try: - return await self._rpc("ota", timeout=600.0, image=image.hex()) + return await self._rpc("ota", 600.0, image=image.hex()) finally: self._on_progress = None