From de00634c2663fd3de4de24349095a9a0e052e318 Mon Sep 17 00:00:00 2001 From: Florent Date: Tue, 15 Jul 2025 13:39:19 +0200 Subject: [PATCH] create binary_commands to handle binary data in req_binary --- src/meshcore/binary_commands.py | 30 ++++++++++++++++++++++++++++++ src/meshcore/commands.py | 2 ++ 2 files changed, 32 insertions(+) create mode 100644 src/meshcore/binary_commands.py diff --git a/src/meshcore/binary_commands.py b/src/meshcore/binary_commands.py new file mode 100644 index 0000000..681868c --- /dev/null +++ b/src/meshcore/binary_commands.py @@ -0,0 +1,30 @@ +import asyncio +import logging +from .events import Event, EventType + +logger = logging.getLogger("meshcore") + +class BinaryCommandHandler : + """ Helper functions to handle binary requests through binary commands """ + def __init__ (self, c): + self.commands = c + + @property + def dispatcher(self): + return self.commands.dispatcher + + async def req_binary (self, contact, request) : + res = await self.commands.send_binary_req(contact, request) + logger.debug(res) + if res.type == EventType.ERROR: + logger.error(f"Error while requesting binary data") + return None + else: + exp_tag = res.payload["expected_ack"].hex() + timeout = res.payload["suggested_timeout"] / 1000 + res2 = await self.dispatcher.wait_for_event(EventType.BINARY_RESPONSE, attribute_filters={"tag": exp_tag}, timeout=timeout) + logger.debug(res2) + if res2 is None : + return None + else: + return res2.payload diff --git a/src/meshcore/commands.py b/src/meshcore/commands.py index 4e084e8..d49e8ec 100644 --- a/src/meshcore/commands.py +++ b/src/meshcore/commands.py @@ -3,6 +3,7 @@ import logging import random from typing import Any, Dict, List, Optional, Union from .events import Event, EventType +from .binary_commands import BinaryCommandHandler # Define types for destination parameters DestinationType = Union[bytes, str, Dict[str, Any]] @@ -52,6 +53,7 @@ class CommandHandler: self._sender_func = None self._reader = None self.dispatcher = None + self.binary = BinaryCommandHandler(self) self.default_timeout = default_timeout if default_timeout is not None else self.DEFAULT_TIMEOUT def set_connection(self, connection: Any) -> None: