Skip to content

Commit

Permalink
Merge pull request #13 from NTIA/timeout
Browse files Browse the repository at this point in the history
Add timeout to WebRelay, ControlByWebWebRelay, and WebRelayPreselector.
  • Loading branch information
dboulware committed Sep 8, 2022
2 parents b954743 + b78ba31 commit f1eef6f
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
default_language_version:
python: python3.7
python: python3.8
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.1.0
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ name = "its-preselector"
dynamic = ["version"]
description = "A package to control the ITS web relay-based preselector"
readme = "README.md"
requires-python = ">=3.7"
requires-python = ">=3.8"
license = { file = "LICENSE.md" }

authors = [
Expand Down
2 changes: 1 addition & 1 deletion src/its_preselector/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "2.0.0"
__version__ = "2.1.0"
27 changes: 21 additions & 6 deletions src/its_preselector/controlbyweb_web_relay.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@


class ControlByWebWebRelay(WebRelay):
def __init__(self, config: dict):
super().__init__(config)
def __init__(self, config: dict, timeout: int = 1):
"""
:param config: The web relay configuration dictionary. The dictionary must
include "name" and "base_url" entries.
:param timeout: The timeout in seconds that will be used in any web requests.
"""
super().__init__(config, timeout)
if "base_url" not in config:
raise ConfigurationException("Config must include base_url.")
elif config["base_url"] is None:
Expand Down Expand Up @@ -46,13 +51,19 @@ def get_sensor_value(self, sensor_num):
return sensor.text

def set_state(self, key):
"""
Set the state of the relay.
:param key: The key for the desired state or states as defined in the config.
:return: None
:raises: requests.Timeout exception
"""
if key in self.config["control_states"]:
switches = self.config["control_states"][str(key)].split(",")
if self.base_url and self.base_url != "":
for i in range(len(switches)):
command = self.base_url + "?relay" + switches[i]
logger.debug(command)
response = requests.get(command)
response = requests.get(command, timeout=self.timeout)
if response.status_code != requests.codes.ok:
raise Exception(
"Unable to set preselector state. Verify configuration and connectivity."
Expand All @@ -62,9 +73,13 @@ def set_state(self, key):
else:
raise Exception("RF path " + key + " configuration does not exist.")

def healthy(self):
def healthy(self) -> bool:
"""
Check if the relay can be reached.
:return: True if the relay can be reached, or False if it cannot be reached.
"""
try:
response = requests.get(self.base_url)
response = requests.get(self.base_url, timeout=self.timeout)
return response.status_code == requests.codes.ok
except:
logger.error("Unable to connect to preselector")
Expand Down Expand Up @@ -155,7 +170,7 @@ def get_relay_state(state_xml, relay):

def get_state_xml(self):
if self.base_url and self.base_url != "":
response = requests.get(self.base_url, timeout=1)
response = requests.get(self.base_url, timeout=self.timeout)
return response
else:
raise Exception("base_url is None or blank")
12 changes: 7 additions & 5 deletions src/its_preselector/web_relay.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@


class WebRelay(ABC):
def __init__(self, config):
def __init__(self, config: dict, timeout: int = 1):
"""
:param config: The web relay configuration dictionary.
:param timeout: The timeout in seconds that will be used in any web requests.
"""
self.config = config

self.timeout = timeout

@abstractmethod
def get_sensor_value(sensor: str) -> str:
Expand All @@ -15,7 +19,6 @@ def get_sensor_value(sensor: str) -> str:
"""
pass


@abstractmethod
def set_state(self, state_key: str) -> None:
"""
Expand All @@ -25,7 +28,6 @@ def set_state(self, state_key: str) -> None:
"""
pass


@abstractmethod
def healthy(self) -> bool:
"""
Expand Down Expand Up @@ -60,4 +62,4 @@ def get_status(self) -> dict:
name of the web relay, a 'healthy' key that maps to the value returned from healthy(), and any keys defined in
the config that map to boolean values indicating whether or not those states are enabled.
"""
pass
pass
19 changes: 16 additions & 3 deletions src/its_preselector/web_relay_preselector.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,24 @@


class WebRelayPreselector(Preselector):
def __init__(self, sigmf: dict, config: dict):
super().__init__(sigmf, config)
self.web_relay = ControlByWebWebRelay(config)
def __init__(self, sensor_definition: dict, config: dict, timeout: int = 1):
"""
:param sensor_definition: JSON representation of the sensor definition including the preselector. The
sensor_definition may be SigMF that includes the sensor definition as specified in
https://github.com/NTIA/sigmf-ns-ntia, or it may be only the Sensor JSON.
:param config: The preselector configuration dictionary.
:param timeout: The timeout in seconds that will be used in any web requests.
"""
super().__init__(sensor_definition, config)
self.web_relay = ControlByWebWebRelay(config, timeout)

def set_state(self, state_name: str):
"""
Set the state of the preselector.
:param state_name: The key for the desired state or states as defined in the config.
:return: None
:raises: requests.Timeout exception
"""
self.web_relay.set_state(state_name)

def get_sensor_value(self, sensor_num) -> str:
Expand Down

0 comments on commit f1eef6f

Please sign in to comment.