mirror of
https://github.com/meshcore-dev/meshcore_py.git
synced 2026-04-20 22:13:49 +00:00
create binary_commands to handle binary data in req_binary
This commit is contained in:
parent
cb6379e4c5
commit
de00634c26
2 changed files with 32 additions and 0 deletions
30
src/meshcore/binary_commands.py
Normal file
30
src/meshcore/binary_commands.py
Normal 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
|
||||
|
|
@ -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:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue