add min_timeout for some lora commands

This commit is contained in:
Florent
2025-09-17 22:01:30 +02:00
parent 4829680435
commit 60e065b5f6
4 changed files with 25 additions and 17 deletions

View File

@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
[project] [project]
name = "meshcore" name = "meshcore"
version = "2.1.7" version = "2.1.8"
authors = [ authors = [
{ name="Florent de Lamotte", email="florent@frizoncorrea.fr" }, { name="Florent de Lamotte", email="florent@frizoncorrea.fr" },
{ name="Alex Wolden", email="awolden@gmail.com" }, { name="Alex Wolden", email="awolden@gmail.com" },

View File

@@ -152,7 +152,7 @@ class CommandHandlerBase:
return Event(EventType.OK, {}) return Event(EventType.OK, {})
# attached at base because its a common method # 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) dst_bytes = _validate_destination(dst, prefix_length=32)
pubkey_prefix = _validate_destination(dst, prefix_length=6) pubkey_prefix = _validate_destination(dst, prefix_length=6)
logger.debug(f"Binary request to {dst_bytes.hex()}") logger.debug(f"Binary request to {dst_bytes.hex()}")
@@ -168,6 +168,7 @@ class CommandHandlerBase:
exp_tag = result.payload["expected_ack"].hex() exp_tag = result.payload["expected_ack"].hex()
# Use provided timeout or fallback to suggested timeout (with 5s default) # 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 = 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) self._reader.register_binary_request(pubkey_prefix.hex(), exp_tag, request_type, actual_timeout)
return result return result

View File

@@ -10,21 +10,23 @@ logger = logging.getLogger("meshcore")
class BinaryCommandHandler(CommandHandlerBase): class BinaryCommandHandler(CommandHandlerBase):
"""Helper functions to handle binary requests through binary commands""" """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") 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( res = await self.send_binary_req(
contact, contact,
BinaryReqType.STATUS, BinaryReqType.STATUS,
timeout=timeout timeout=timeout,
min_timeout=min_timeout
) )
if res.type == EventType.ERROR: if res.type == EventType.ERROR:
return None return None
exp_tag = res.payload["expected_ack"].hex() exp_tag = res.payload["expected_ack"].hex()
timeout = res.payload["suggested_timeout"] / 800 if timeout == 0 else timeout 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: if self.dispatcher is None:
return None return None
@@ -37,20 +39,22 @@ class BinaryCommandHandler(CommandHandlerBase):
return status_event.payload if status_event else None 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") 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( res = await self.send_binary_req(
contact, contact,
BinaryReqType.TELEMETRY, BinaryReqType.TELEMETRY,
timeout=timeout timeout=timeout,
min_timeout=min_timeout
) )
if res.type == EventType.ERROR: if res.type == EventType.ERROR:
return None return None
timeout = res.payload["suggested_timeout"] / 800 if timeout == 0 else timeout 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: if self.dispatcher is None:
return None return None
@@ -64,11 +68,11 @@ class BinaryCommandHandler(CommandHandlerBase):
return telem_event.payload["lpp"] if telem_event else None 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") 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 = ( req = (
start.to_bytes(4, "little", signed=False) start.to_bytes(4, "little", signed=False)
+ end.to_bytes(4, "little", signed=False) + end.to_bytes(4, "little", signed=False)
@@ -84,6 +88,7 @@ class BinaryCommandHandler(CommandHandlerBase):
return None return None
timeout = res.payload["suggested_timeout"] / 800 if timeout == 0 else timeout 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: if self.dispatcher is None:
return None return None
@@ -97,11 +102,11 @@ class BinaryCommandHandler(CommandHandlerBase):
return mma_event.payload["mma_data"] if mma_event else None 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") 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" req = b"\0\0"
res = await self.send_binary_req( res = await self.send_binary_req(
contact, contact,
@@ -113,6 +118,7 @@ class BinaryCommandHandler(CommandHandlerBase):
return None return None
timeout = res.payload["suggested_timeout"] / 800 if timeout == 0 else timeout 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: if self.dispatcher is None:
return None return None

View File

@@ -81,7 +81,7 @@ class MessagingCommands(CommandHandlerBase):
async def send_msg_with_retry ( async def send_msg_with_retry (
self, dst: DestinationType, msg: str, timestamp: Optional[int] = None, 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: ) -> Event:
dst_bytes = _validate_destination(dst) dst_bytes = _validate_destination(dst)
@@ -116,6 +116,7 @@ class MessagingCommands(CommandHandlerBase):
exp_ack = result.payload["expected_ack"].hex() exp_ack = result.payload["expected_ack"].hex()
timeout = result.payload["suggested_timeout"] / 1000 * 1.2 if timeout==0 else timeout 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, res = await self.dispatcher.wait_for_event(EventType.ACK,
attribute_filters={"code": exp_ack}, attribute_filters={"code": exp_ack},
timeout=timeout) timeout=timeout)