create binary_commands to handle binary data in req_binary

This commit is contained in:
Florent
2025-07-15 13:39:19 +02:00
parent cb6379e4c5
commit de00634c26
2 changed files with 32 additions and 0 deletions

View File

@@ -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

View File

@@ -3,6 +3,7 @@ import logging
import random import random
from typing import Any, Dict, List, Optional, Union from typing import Any, Dict, List, Optional, Union
from .events import Event, EventType from .events import Event, EventType
from .binary_commands import BinaryCommandHandler
# Define types for destination parameters # Define types for destination parameters
DestinationType = Union[bytes, str, Dict[str, Any]] DestinationType = Union[bytes, str, Dict[str, Any]]
@@ -52,6 +53,7 @@ class CommandHandler:
self._sender_func = None self._sender_func = None
self._reader = None self._reader = None
self.dispatcher = None self.dispatcher = None
self.binary = BinaryCommandHandler(self)
self.default_timeout = default_timeout if default_timeout is not None else self.DEFAULT_TIMEOUT self.default_timeout = default_timeout if default_timeout is not None else self.DEFAULT_TIMEOUT
def set_connection(self, connection: Any) -> None: def set_connection(self, connection: Any) -> None: