Skip to content

Commit

Permalink
Simplify parser
Browse files Browse the repository at this point in the history
  • Loading branch information
elupus committed May 20, 2023
1 parent d487fdb commit 73be0f5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 32 deletions.
49 changes: 17 additions & 32 deletions RFXtrx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import time
import logging
import asyncio
from typing import Callable, Optional, Dict, List
from typing import Callable, Optional, Dict, List, Union


from time import sleep
Expand Down Expand Up @@ -1116,49 +1116,34 @@ class AsyncConnectBase(asyncio.protocols.BufferedProtocol,

def __init__(self) -> None:
super().__init__()
self._buffer = bytearray(256)
self._view = memoryview(self._buffer)
self._buffer = bytearray()
self._transport: 'asyncio.Future[asyncio.Transport]' = asyncio.Future()
self._pos = 0
self._len = 1

def data_received(self, data: bytes) -> None:
"""Wrapper for non buffer protocol transports."""
idx = 0
while idx < len(data):
cnt = len(data)-idx
buffer = self.get_buffer(cnt)
cnt = min(len(buffer), cnt)
assert cnt
buffer[0:cnt] = bytes(data[idx:idx+cnt])
self.buffer_updated(cnt)
idx += cnt

def get_buffer(self, _: int) -> memoryview:
"""Return view of buffer for next needed data."""
return self._view[self._pos:self._len]
self._buffer.extend(data)

while len(self._buffer) >= self._len:
if self._len == 1:
self._len = self._buffer[0] + 1
else:
try:
self._packet_received(self._buffer[:self._len])
finally:
self._packet_flush(self._len)

def _packet_received(self, data: bytes):
""" Packet received. """

def _packet_flush(self):
def _packet_flush(self, skip: Union[int, None] = None):
""" Flush any received data so far. """
self._pos = 0
self._len = 1

def buffer_updated(self, nbytes):
""" New data received. """
self._pos += nbytes
assert self._pos <= self._len

if self._len == 1:
self._len = self._buffer[0] + 1

if self._len == self._pos:
try:
self._packet_received(self._buffer[:self._pos])
finally:
self._packet_flush()
if skip is None:
self._buffer.clear()
else:
self._buffer = self._buffer[skip:]

def connection_made(self, transport: asyncio.BaseTransport) -> None:
""" Connection to server was made. """
Expand Down
2 changes: 2 additions & 0 deletions RFXtrx/lowlevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,8 @@ def _set_strings(self):
else:
self.firmware_type_string = 'Unknown ' + str(self.firmware_type)

self.id_string = "-"


###############################################################################
# ReceiverTransmitter class
Expand Down

0 comments on commit 73be0f5

Please sign in to comment.