Skip to content

Commit

Permalink
Fix: JSON parsing error on too large payloads & server_info errors in…
Browse files Browse the repository at this point in the history
… log (#7)

* fix list tyoe for FlicButton

* fix dict type for _data_ready

* Match command server info in tcpserver to client lib (server_info -> server)

* add buffer for too long packets

---------

Co-authored-by: martin <[email protected]>
  • Loading branch information
blade5502 and blade5502 authored Jun 19, 2024
1 parent 96945e0 commit df59302
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ async def client_disconnected():
print(f"Client not connected after {CLIENT_READY_TIMEOUT} secs so terminating")
exit()

buttons: [FlicButton] = await client.get_buttons()
buttons: list[FlicButton] = await client.get_buttons()
for button in buttons:
print(f"Button name: {button.name} - Connected: {button.connected}")

Expand Down
21 changes: 18 additions & 3 deletions pyflichub/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ async def run(*args, loop=None, executor=None, **kwargs):


class FlicHubTcpClient(asyncio.Protocol):
buttons: [FlicButton] = []
buttons: list[FlicButton] = []
network: FlicHubInfo

def __init__(self, ip, port, loop, timeout=1.0, reconnect_timeout=10.0, event_callback=None, command_callback=None):
self._data_ready: {str: Union[asyncio.Event, None]} = {}
self._data_ready: dict[str: Union[asyncio.Event, None]] = {}
self._transport = None
self._command_callback = command_callback
self._event_callback = event_callback
Expand All @@ -48,6 +48,7 @@ def __init__(self, ip, port, loop, timeout=1.0, reconnect_timeout=10.0, event_ca
self._reconnect_timeout = reconnect_timeout
self._timeout = timeout
self._data: dict = {}
self._buffer = None
self._connecting = False
self._forced_disconnect = False
self.async_on_connected = None
Expand Down Expand Up @@ -97,7 +98,7 @@ async def async_connect(self):
def send_command(self, cmd: ServerCommand):
return self._async_send_command(cmd)

async def get_buttons(self) -> [FlicButton]:
async def get_buttons(self) -> list[FlicButton]:
command: Command = await self._async_send_command_and_wait_for_data(ServerCommand.BUTTONS)
return command.data if command is not None else []

Expand Down Expand Up @@ -139,6 +140,18 @@ def connection_made(self, transport):

def data_received(self, data):
decoded_data = data.decode()
if "\n" not in decoded_data:
if not self._buffer:
_LOGGER.debug('First data fragment received: {!r}'.format(decoded_data))
self._buffer = decoded_data
else:
_LOGGER.debug('Data fragment received: {!r}'.format(decoded_data))
self._buffer += decoded_data
return
else:
if self._buffer:
decoded_data = self._buffer + decoded_data

_LOGGER.debug('Data received: {!r}'.format(decoded_data))
for data_part in [data_part for data_part in decoded_data.split("\n") if data_part.strip()]:
if data_part == 'pong':
Expand All @@ -154,6 +167,8 @@ def data_received(self, data):
_LOGGER.warning(e, exc_info=True)
_LOGGER.warning('Unable to decode received data')

self._buffer = None

def connection_lost(self, exc):
_LOGGER.info("Connection lost")
self._connecting = True
Expand Down
4 changes: 2 additions & 2 deletions tcpserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const network = require('network');
const net = require('net');
const buttons = require('buttons');
const EOL = "\n";
const VERSION = "0.1.8";
const VERSION = "0.1.9";

// Configuration - start
const PORT = 8124;
Expand Down Expand Up @@ -42,7 +42,7 @@ net.createServer(function (socket) {

function sendServerInfo() {
const payload = {
'command': 'server_info',
'command': 'server',
'data': {
'version': VERSION
}
Expand Down

0 comments on commit df59302

Please sign in to comment.