Skip to content
Snippets Groups Projects
Commit 1c1d2dae authored by David Huss's avatar David Huss :speech_balloon:
Browse files

Implement dynamic id_pattern changes from server

parent dad102ce
No related branches found
No related tags found
No related merge requests found
...@@ -41,6 +41,15 @@ EVENT_FORMAT = "llHHI" ...@@ -41,6 +41,15 @@ EVENT_FORMAT = "llHHI"
EVENT_SIZE = struct.calcsize(EVENT_FORMAT) EVENT_SIZE = struct.calcsize(EVENT_FORMAT)
def string_to_list(s:str, delimiter: str="\n") -> List[str]:
"""
Split a string to a list derived from a delimiter. If the string contains
no delimiter, still return a list containing the string
"""
if delimiter in s:
return s.split(delimiter)
else:
return [s]
def repeat(l, index): def repeat(l, index):
...@@ -320,16 +329,36 @@ def read_key_input(input_queue, config, logger): ...@@ -320,16 +329,36 @@ def read_key_input(input_queue, config, logger):
time.sleep(0.01) time.sleep(0.01)
def update_id_patterns(config) -> 'Config': def update_id_patterns(config, logger) -> 'Config':
"""
Update the id_patterns from the server via http
"""
if config["server"]["port"] == 80: if config["server"]["port"] == 80:
protocol = "http" protocol = "http"
else: else:
protocol = "https" protocol = "https"
url = '{}://{}/config/database/id_patterns'.format(protocol, config["server"]["address"].rstrip("/")) url = '{}://{}/config/database/id_patterns'.format(protocol, config["server"]["address"].rstrip("/"))
r = requests.get(url) logger.debug("Requesting id_pattern update at address: {}".format(url))
print(r.text)
# config["client"]["id_patterns"] # Send request
r = requests.get(url, timeout=config["server"]["timeout"])
# If the response was ok update the pattern if it changed, else display a warning
if r.ok:
patterns = string_to_list(r.text)
if set(patterns) == set(config["client"]["id_patterns"]):
logger.debug("No change in serverside id_pattern, so didn't update")
else:
# Only compile the patterns once we know something changed
patterns = [re.compile(p) for p in patterns]
config["client"]["id_patterns"] = patterns
logger.info("Received a newly updated patterns list from the server")
else:
logger.warn("Server at \"{}\" responded with {}".format(url, r.status))
return config
def main(): def main():
logger = logging.getLogger(APPLICATION_NAME) logger = logging.getLogger(APPLICATION_NAME)
...@@ -374,9 +403,10 @@ def main(): ...@@ -374,9 +403,10 @@ def main():
else: else:
time.sleep(0.01) time.sleep(0.01)
# Update the patterns at the pace defined by config["server"]["update_frequency"]
if last_pattern_update is None or time.time() - last_pattern_update > config["server"]["update_frequency"]: if last_pattern_update is None or time.time() - last_pattern_update > config["server"]["update_frequency"]:
last_pattern_update = time.time() last_pattern_update = time.time()
update_id_patterns(config) config = update_id_patterns(config, logger)
time.sleep(0.01) time.sleep(0.01)
......
...@@ -26,7 +26,9 @@ address = "127.0.0.1" ...@@ -26,7 +26,9 @@ address = "127.0.0.1"
port = 80 port = 80
timeout = 5 timeout = 5
verify_cert = true verify_cert = true
update_frequency = 10
# Frequency of requesting pattern updates from the server in seconds
update_frequency = 600
[client] [client]
location = "lerchenfeld/mensa" location = "lerchenfeld/mensa"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment