control codes support: node_discover_req

This commit is contained in:
Florent
2025-11-06 22:32:53 +01:00
parent 057051c8c3
commit d3c9c8d984
5 changed files with 99 additions and 4 deletions

View File

@@ -7,10 +7,15 @@ from .binary import BinaryCommandHandler
from .contact import ContactCommands
from .device import DeviceCommands
from .messaging import MessagingCommands
from .control_data import ControlDataCommandHandler
class CommandHandler(
DeviceCommands, ContactCommands, MessagingCommands, BinaryCommandHandler
DeviceCommands,
ContactCommands,
MessagingCommands,
BinaryCommandHandler,
ControlDataCommandHandler
):
pass

View File

@@ -0,0 +1,45 @@
import logging
import random
from .base import CommandHandlerBase
from ..events import EventType, Event
from ..packets import ControlType, PacketType
logger = logging.getLogger("meshcore")
class ControlDataCommandHandler(CommandHandlerBase):
"""Helper functions to handle binary requests through binary commands"""
async def send_control_data (self, control_type: ControlType, payload: bytes) -> Event:
data = bytearray([PacketType.SEND_CONTROL_DATA.value])
data.extend(control_type.value.to_bytes(1, "little", signed = False))
data.extend(payload)
result = await self.send(data, [EventType.OK, EventType.ERROR])
return result
async def send_node_discover_req (
self,
filter: int,
tag: int=None,
since: int=None
) -> Event:
if tag is None:
tag = random.randint(1, 0xFFFFFFFF)
data = bytearray()
data.extend(filter.to_bytes(1, "little", signed=False))
data.extend(tag.to_bytes(4, "little"))
if not since is None:
data.extend(since.to_bytes(4, "little", signed=False))
logger.debug(f"sending node discover req {data.hex()}")
res = await self.send_control_data(ControlType.NODE_DISCOVER_REQ, data)
if res is None:
return None
else:
res.payload["tag"] = tag
return res