Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions luxtronik/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,22 @@

def is_socket_closed(sock: socket.socket) -> bool:
"""Check is socket closed."""
# Alternative to socket.MSG_DONTWAIT in recv.
# Works on Windows and Linux.
sock.setblocking(False)
try:
# this will try to read bytes without blocking and also without removing them from buffer
data = sock.recv(LUXTRONIK_SOCKET_READ_SIZE_PEEK, socket.MSG_DONTWAIT | socket.MSG_PEEK)
if len(data) == 0:
return True
data = sock.recv(LUXTRONIK_SOCKET_READ_SIZE_PEEK, socket.MSG_PEEK)
is_closed = len(data) == 0
except BlockingIOError:
return False # socket is open and reading from it would block
is_closed = False # socket is open and reading from it would block
except ConnectionResetError: # pylint: disable=broad-except
return True # socket was closed for some other reason
is_closed = True # socket was closed for some other reason
except Exception as err: # pylint: disable=broad-except
LOGGER.exception("Unexpected exception when checking if socket is closed", exc_info=err)
return False
return False
is_closed = False
sock.setblocking(True)
return is_closed


class LuxtronikData:
Expand Down
7 changes: 5 additions & 2 deletions luxtronik/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ def main() -> int:
description="CLI for Luxtronik controllers",
usage="""luxtronik <command> [<args>]
The supported commands are:
dump Dump all available data from the Luxtronik controller
changes Dump all value changes from Luxtronik controller
dump Dump all available data from the Luxtronik controller
changes Dump all value changes from Luxtronik controller
discover Discover Luxtronik controllers on the network (via magic packet) and output results
""",
)
Expand All @@ -47,3 +47,6 @@ def main() -> int:
sys.argv.pop(1)
# call the corresponding command
commands[args.command]()

if __name__ == "__main__":
main()
6 changes: 5 additions & 1 deletion tests/test_socket_interaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def __init__(self, prot, stream):
assert stream == socket.SOCK_STREAM
self._connected = False
self._buffer = b""
self._blocking = False

# Offer some more entries
self._num_paras = len(Parameters()._data) + 10
Expand All @@ -44,6 +45,9 @@ def __init__(self, prot, stream):

self.written_values = {}

def setblocking(self, blocking):
self._blocking = blocking

def connect(self, info):
assert not self._connected

Expand Down Expand Up @@ -127,7 +131,7 @@ def sendall(self, data):
def recv(self, cnt, flag=0):
assert self._connected

if (flag & socket.MSG_DONTWAIT) and len(self._buffer) < cnt:
if (not self._blocking) and len(self._buffer) < cnt:
raise BlockingIOError("Not enough bytes in buffer.")

assert len(self._buffer) >= cnt
Expand Down