Skip to content

Commit

Permalink
Allow setting a connection timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
elupus committed Feb 10, 2024
1 parent 0111808 commit 701881e
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions RFXtrx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ def parse(data):
return obj
return None

def connect(self):
def connect(self, timeout=None):
""" connect to device """

def reset(self):
Expand Down Expand Up @@ -805,7 +805,7 @@ def __init__(self, port):
self.port = port
self.serial = None

def connect(self):
def connect(self, timeout=None):
""" Open a serial connexion """
try:
try:
Expand Down Expand Up @@ -877,10 +877,12 @@ def __init__(self, hostport):
self.hostport = hostport # must be a (host, port) tuple
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

def connect(self):
def connect(self, timeout=None):
""" Open a socket connection """
try:
self.sock.settimeout(timeout)
self.sock.connect(self.hostport)
self.sock.settimeout(None)
_LOGGER.debug("Connected to network socket")
except socket.error as exception:
raise RFXtrxTransportError("Connection failed: {0}".format(exception)) from exception
Expand Down Expand Up @@ -944,7 +946,7 @@ def __init__(self, device=""):
self.device = device
self._close_event = threading.Event()

def connect(self):
def connect(self, timeout=None):
pass

def receive(self, data=None):
Expand Down Expand Up @@ -984,7 +986,7 @@ def __init__(self, device=""):
self.serial = _dummySerial(device, 38400, timeout=0.1)
self._run_event = threading.Event()

def connect(self):
def connect(self, timeout=None):
self._run_event.set()


Expand All @@ -1003,11 +1005,12 @@ def __init__(self, device, event_callback=None,
self.event_callback = event_callback
self.transport: RFXtrxTransport = transport_protocol(device)

def connect(self):
self.transport.connect()
def connect(self, timeout=None):
self.transport.connect(timeout)
self._thread = threading.Thread(target=self._connect, daemon=True)
self._thread.start()
self._run_event.wait()
if not self._run_event.wait(timeout):
self.close_connection()

def _connect(self):
try:
Expand Down

0 comments on commit 701881e

Please sign in to comment.