Skip to content

Commit

Permalink
Handle broken received data
Browse files Browse the repository at this point in the history
  • Loading branch information
JohNan committed Sep 13, 2023
1 parent 8a9e870 commit 884a919
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
4 changes: 2 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

logging.basicConfig(level=logging.DEBUG)

CLIENT_READY_TIMEOUT = 60.0
CLIENT_READY_TIMEOUT = 10.0
HOST = ('192.168.1.64', 8124)


Expand All @@ -36,7 +36,7 @@ def client_disconnected():
client.on_connected = client_connected
client.on_disconnected = client_disconnected

asyncio.create_task(client.async_connect())
task = asyncio.create_task(client.async_connect())

try:
async with async_timeout.timeout(CLIENT_READY_TIMEOUT):
Expand Down
3 changes: 2 additions & 1 deletion pyflichub/button.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
class FlicButton:
class FlicButton(dict):
def __init__(self, bdaddr: str, serial_number: str, color: str, name: str, active_disconnect: bool, connected: bool,
ready: bool, battery_status: int, uuid: str, flic_version: int, firmware_version: int, key: str,
passive_mode: bool) -> None:
super().__init__()
self.bdaddr = bdaddr
self.serial_number = serial_number
self.color = color
Expand Down
21 changes: 15 additions & 6 deletions pyflichub/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@ def __init__(self, ip, port, loop, timeout=1.0, reconnect_timeout=10.0, event_ca
self._data = None
self.on_connected = None
self.on_disconnected = None
self._connecting = False

async def async_connect(self):
self._connecting = True
"""Connect to the socket."""
try:
while True:
while self._connecting:
_LOGGER.info("Trying to connect to %s", self._server_address)
try:
await asyncio.wait_for(
Expand All @@ -72,6 +74,9 @@ async def async_connect(self):
_LOGGER.debug("Connect attempt to %s cancelled", self._server_address)

def disconnect(self):
_LOGGER.info("Disconnected")
self._connecting = False

if self._transport is not None:
self._transport.close()

Expand Down Expand Up @@ -112,11 +117,15 @@ def data_received(self, data):
if decoded_data == 'pong':
return

msg = json.loads(decoded_data)
if 'event' in msg:
self._handle_event(Event(**msg))
if 'command' in msg:
self._handle_command(Command(**msg))
try:
msg = json.loads(decoded_data)
if 'event' in msg:
self._handle_event(Event(**msg))
if 'command' in msg:
self._handle_command(Command(**msg))
except Exception:
_LOGGER.warning('Unable to decode received data')


def connection_lost(self, exc):
_LOGGER.info("Connection lost")
Expand Down

0 comments on commit 884a919

Please sign in to comment.