Skip to content

Commit

Permalink
Convert _wait_for_configs to function instead of decorator
Browse files Browse the repository at this point in the history
It was complex for nothing and gateway parameter was wrongly interpreded
  • Loading branch information
GwendalRaoul committed Nov 7, 2024
1 parent 087f888 commit aa8926b
Showing 1 changed file with 34 additions and 47 deletions.
81 changes: 34 additions & 47 deletions wirepas_mqtt_library/wirepas_network_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,59 +477,45 @@ def _ask_gateway_config(self, gw_id):
# Call update gateway config when receiving it
self._wait_for_response(self._update_gateway_configs, request.req_id)

def _wait_for_configs(fn):
# Decorator to handle waiting for enough time for the setup
# of the network interface
def wrapper(*args, **kwargs):
# args[0] is self

gateways_subset = None
# Check for missing gateway config
if ("gateway" in kwargs):
gateways_subset = kwargs['gateway']
def _wait_for_configs(self, gateways=None):
gateways_to_wait_config = []
for gw in self._gateways.copy().values():
if gateways is not None and gw.id not in gateways:
# Not interested by this gateway, so no need for the config
continue

gateways_to_wait_config = []
for gw in args[0]._gateways.copy().values():
if gateways_subset is not None and gw.id not in gateways_subset:
# Not interested by this gateway, so no need for the config
if gw.online:
if gw.config_received_event.is_set():
# We have already received the config
continue

if gw.online:
if gw.config_received_event.is_set():
# We have already received the config
continue

# Time to ask the gateway config
args[0]._ask_gateway_config(gw.id)

gateways_to_wait_config.append(gw)
# Time to ask the gateway config
self._ask_gateway_config(gw.id)
gateways_to_wait_config.append(gw)

timeout_ts = time() + args[0]._gw_timeout_s

# We have asked config for gateway we never received it
# Check if we received it for all gateways we asked before timeout
for gw in gateways_to_wait_config:
timeout = timeout_ts - time()
if timeout <= 0:
timeout = None
timeout_ts = time() + self._gw_timeout_s

# We have asked config for gateway we never received it
# Check if we received it for all gateways we asked before timeout
for gw in gateways_to_wait_config:
timeout = timeout_ts - time()
if timeout > 0:
# We can still wait
gw.config_received_event.wait(timeout)
if not gw.config_received_event.is_set():
logging.error("Config timeout for gw %s" % gw.id)
logging.error("Is the gateway really online? If not, its status can be cleared by "
"calling clear_gateway_status(\"%s\")", gw.id)
# Mark the initial config as empty list to avoid waiting for it next time
# It may still come later
gw.update_all_sink_configs([])

if args[0]._strict_mode:
logging.error("This Timeout will generate an exception but you can "
"avoid it by starting WirepasNetworkInteface with strict_mode=False")
raise TimeoutError("Cannot get config from online GW %s", gw.id)

return fn(*args, **kwargs)
wrapper.__doc__ = fn.__doc__
return wrapper
if not gw.config_received_event.is_set():
logging.error("Config timeout for gw %s" % gw.id)
logging.error("Is the gateway really online? If not, its status can be cleared by "
"calling clear_gateway_status(\"%s\")", gw.id)
# Mark the initial config as empty list to avoid waiting for it next time
# It may still come later
gw.update_all_sink_configs([])

if self._strict_mode:
logging.error("This Timeout will generate an exception but you can "
"avoid it by starting WirepasNetworkInteface with strict_mode=False")
raise TimeoutError("Cannot get config from online GW %s", gw.id)

def close(self):
"""Explicitly close this network interface as well as the worker threads
Expand All @@ -552,7 +538,6 @@ def close(self):


@_wait_for_connection
@_wait_for_configs
def get_sinks(self, network_address=None, gateway=None):
"""
get_sinks(self, network_address=None, gateway=None)
Expand Down Expand Up @@ -597,6 +582,8 @@ def get_sinks(self, network_address=None, gateway=None):
:raises TimeoutError: If a gateway doesn't send its initial config fast enough
"""
# Wait for subset of gateways
self._wait_for_configs(gateways=gateway)
sinks = list()
for gw in self._gateways.copy().values():
if not gw.online:
Expand Down Expand Up @@ -1126,7 +1113,6 @@ def set_sink_config(self, gw_id, sink_id, new_config, cb=None, param=None):
return self._wait_for_response(cb, request.req_id, extra_timeout=3, param=param)

@_wait_for_connection
@_wait_for_configs
def set_config_changed_cb(self, cb):
"""
set_config_changed_cb(self, cb)
Expand All @@ -1138,6 +1124,7 @@ def set_config_changed_cb(self, cb):
and :meth:`~wirepas_mqtt_library.wirepas_network_interface.WirepasNetworkInterface.get_sinks`
can be used to discover what has changed
"""
self._wait_for_configs()
self._on_config_changed_cb = cb

# Call the cb a first time as an initial info
Expand Down

0 comments on commit aa8926b

Please sign in to comment.