diff --git a/pyproject.toml b/pyproject.toml index 73b679c..95759f7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "hatchling.build" [project] name = "meshcore" -version = "2.1.7" +version = "2.1.8" authors = [ { name="Florent de Lamotte", email="florent@frizoncorrea.fr" }, { name="Alex Wolden", email="awolden@gmail.com" }, diff --git a/src/meshcore/commands/base.py b/src/meshcore/commands/base.py index cf4ae16..7cdcce3 100644 --- a/src/meshcore/commands/base.py +++ b/src/meshcore/commands/base.py @@ -152,7 +152,7 @@ class CommandHandlerBase: return Event(EventType.OK, {}) # attached at base because its a common method - async def send_binary_req(self, dst: DestinationType, request_type: BinaryReqType, data: Optional[bytes] = None, timeout=None) -> Event: + async def send_binary_req(self, dst: DestinationType, request_type: BinaryReqType, data: Optional[bytes] = None, timeout=None, min_timeout=0) -> Event: dst_bytes = _validate_destination(dst, prefix_length=32) pubkey_prefix = _validate_destination(dst, prefix_length=6) logger.debug(f"Binary request to {dst_bytes.hex()}") @@ -168,6 +168,7 @@ class CommandHandlerBase: exp_tag = result.payload["expected_ack"].hex() # Use provided timeout or fallback to suggested timeout (with 5s default) actual_timeout = timeout if timeout is not None and timeout > 0 else result.payload.get("suggested_timeout", 4000) / 800.0 + actual_timeout = min_timeout if actual_timeout < min_timeout else actual_timeout self._reader.register_binary_request(pubkey_prefix.hex(), exp_tag, request_type, actual_timeout) return result diff --git a/src/meshcore/commands/binary.py b/src/meshcore/commands/binary.py index 2090aaa..4314bc5 100644 --- a/src/meshcore/commands/binary.py +++ b/src/meshcore/commands/binary.py @@ -10,21 +10,23 @@ logger = logging.getLogger("meshcore") class BinaryCommandHandler(CommandHandlerBase): """Helper functions to handle binary requests through binary commands""" - async def req_status(self, contact, timeout=0): + async def req_status(self, contact, timeout=0, min_timeout=0): logger.error("*** please consider using req_status_sync instead of req_status") - return await self.req_status_sync(contact, timeout) + return await self.req_status_sync(contact, timeout, min_timeout) - async def req_status_sync(self, contact, timeout=0): + async def req_status_sync(self, contact, timeout=0, min_timeout=0): res = await self.send_binary_req( contact, BinaryReqType.STATUS, - timeout=timeout + timeout=timeout, + min_timeout=min_timeout ) if res.type == EventType.ERROR: return None exp_tag = res.payload["expected_ack"].hex() timeout = res.payload["suggested_timeout"] / 800 if timeout == 0 else timeout + timeout = timeout if min_timeout < timeout else min_timeout if self.dispatcher is None: return None @@ -37,20 +39,22 @@ class BinaryCommandHandler(CommandHandlerBase): return status_event.payload if status_event else None - async def req_telemetry(self, contact, timeout=0): + async def req_telemetry(self, contact, timeout=0, min_timeout=0): logger.error("*** please consider using req_telemetry_sync instead of req_telemetry") - return await self.req_telemetry_sync(contact, timeout) + return await self.req_telemetry_sync(contact, timeout, min_timeout) - async def req_telemetry_sync(self, contact, timeout=0): + async def req_telemetry_sync(self, contact, timeout=0, min_timeout=0): res = await self.send_binary_req( contact, BinaryReqType.TELEMETRY, - timeout=timeout + timeout=timeout, + min_timeout=min_timeout ) if res.type == EventType.ERROR: return None timeout = res.payload["suggested_timeout"] / 800 if timeout == 0 else timeout + timeout = timeout if min_timeout < timeout else min_timeout if self.dispatcher is None: return None @@ -64,11 +68,11 @@ class BinaryCommandHandler(CommandHandlerBase): return telem_event.payload["lpp"] if telem_event else None - async def req_mma(self, contact, timeout=0): + async def req_mma(self, contact, timeout=0, min_timeout=0): logger.error("*** please consider using req_mma_sync instead of req_mma") - return await self.req_mma_sync(contact, start, end, timeout) + return await self.req_mma_sync(contact, start, end, timeout,min_timeout) - async def req_mma_sync(self, contact, start, end, timeout=0): + async def req_mma_sync(self, contact, start, end, timeout=0,min_timeout=0): req = ( start.to_bytes(4, "little", signed=False) + end.to_bytes(4, "little", signed=False) @@ -84,6 +88,7 @@ class BinaryCommandHandler(CommandHandlerBase): return None timeout = res.payload["suggested_timeout"] / 800 if timeout == 0 else timeout + timeout = timeout if min_timeout < timeout else min_timeout if self.dispatcher is None: return None @@ -97,11 +102,11 @@ class BinaryCommandHandler(CommandHandlerBase): return mma_event.payload["mma_data"] if mma_event else None - async def req_acl(self, contact, timeout=0): + async def req_acl(self, contact, timeout=0, min_timeout=0): logger.error("*** please consider using req_acl_sync instead of req_acl") - return await self.req_acl_sync(contact, timeout) + return await self.req_acl_sync(contact, timeout, min_timeout) - async def req_acl_sync(self, contact, timeout=0): + async def req_acl_sync(self, contact, timeout=0, min_timeout=0): req = b"\0\0" res = await self.send_binary_req( contact, @@ -113,6 +118,7 @@ class BinaryCommandHandler(CommandHandlerBase): return None timeout = res.payload["suggested_timeout"] / 800 if timeout == 0 else timeout + timeout = timeout if timeout > min_timeout else min_timeout if self.dispatcher is None: return None diff --git a/src/meshcore/commands/messaging.py b/src/meshcore/commands/messaging.py index 683ba8e..65b5cce 100644 --- a/src/meshcore/commands/messaging.py +++ b/src/meshcore/commands/messaging.py @@ -81,7 +81,7 @@ class MessagingCommands(CommandHandlerBase): async def send_msg_with_retry ( self, dst: DestinationType, msg: str, timestamp: Optional[int] = None, - max_attempts=3, max_flood_attempts=2, flood_after=2, timeout=0 + max_attempts=3, max_flood_attempts=2, flood_after=2, timeout=0, min_timeout=0 ) -> Event: dst_bytes = _validate_destination(dst) @@ -116,6 +116,7 @@ class MessagingCommands(CommandHandlerBase): exp_ack = result.payload["expected_ack"].hex() timeout = result.payload["suggested_timeout"] / 1000 * 1.2 if timeout==0 else timeout + timeout = timeout if timeout > min_timeout else min_timeout res = await self.dispatcher.wait_for_event(EventType.ACK, attribute_filters={"code": exp_ack}, timeout=timeout)