diff --git a/luxtronik/__init__.py b/luxtronik/__init__.py index 4cb9fb0e..6754531d 100755 --- a/luxtronik/__init__.py +++ b/luxtronik/__init__.py @@ -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: diff --git a/luxtronik/__main__.py b/luxtronik/__main__.py index 872e3450..075145d7 100755 --- a/luxtronik/__main__.py +++ b/luxtronik/__main__.py @@ -25,8 +25,8 @@ def main() -> int: description="CLI for Luxtronik controllers", usage="""luxtronik [] 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 """, ) @@ -47,3 +47,6 @@ def main() -> int: sys.argv.pop(1) # call the corresponding command commands[args.command]() + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/tests/test_socket_interaction.py b/tests/test_socket_interaction.py index 88c0402e..e7e2f60b 100644 --- a/tests/test_socket_interaction.py +++ b/tests/test_socket_interaction.py @@ -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 @@ -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 @@ -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