From 210fe5352fa0afb41512d72c0e50746640490522 Mon Sep 17 00:00:00 2001 From: Jakob Ketterl Date: Sun, 12 May 2019 14:35:25 +0200 Subject: [PATCH] refactor the sdr.hu updater into the new server, too --- openwebrx.py | 12 ++++++------ owrx/config.py | 15 +++++++++++++++ owrx/sdrhu.py | 36 ++++++++++++++++++++++++++++++++++++ sdrhu.py | 32 +++++++------------------------- 4 files changed, 64 insertions(+), 31 deletions(-) create mode 100644 owrx/sdrhu.py diff --git a/openwebrx.py b/openwebrx.py index 07df451..b89d7e5 100644 --- a/openwebrx.py +++ b/openwebrx.py @@ -3,6 +3,7 @@ from owrx.http import RequestHandler from owrx.config import PropertyManager, FeatureDetector from owrx.source import SdrService from socketserver import ThreadingMixIn +from owrx.sdrhu import SdrHuUpdater import logging logging.basicConfig(level = logging.DEBUG, format = "%(asctime)s - %(name)s - %(levelname)s - %(message)s") @@ -22,12 +23,7 @@ Author contact info: Andras Retzler, HA7ILM """) - cfg = __import__("config_webrx") - pm = PropertyManager.getSharedInstance() - for name, value in cfg.__dict__.items(): - if name.startswith("__"): - continue - pm[name] = value + pm = PropertyManager.getSharedInstance().loadConfig("config_webrx") featureDetector = FeatureDetector() if not featureDetector.is_available("core"): @@ -39,6 +35,10 @@ Author contact info: Andras Retzler, HA7ILM # Get error messages about unknown / unavailable features as soon as possible SdrService.loadProps() + if "sdrhu_key" in pm and pm["sdrhu_public_listing"]: + updater = SdrHuUpdater() + updater.start() + server = ThreadedHttpServer(('0.0.0.0', pm.getPropertyValue("web_port")), RequestHandler) server.serve_forever() diff --git a/owrx/config.py b/owrx/config.py index cc84c26..a117228 100644 --- a/owrx/config.py +++ b/owrx/config.py @@ -53,6 +53,9 @@ class PropertyManager(object): prop.wire(fireCallbacks) return self + def __contains__(self, name): + return self.hasProperty(name) + def __getitem__(self, name): return self.getPropertyValue(name) @@ -61,6 +64,9 @@ class PropertyManager(object): self.add(name, Property()) self.getProperty(name).setValue(value) + def __dict__(self): + return {k:v.getValue() for k, v in self.properties.items()} + def hasProperty(self, name): return name in self.properties @@ -86,6 +92,15 @@ class PropertyManager(object): p.setValue(other_pm[key]) return self + def loadConfig(self, filename): + cfg = __import__(filename) + for name, value in cfg.__dict__.items(): + if name.startswith("__"): + continue + self[name] = value + return self + + class UnknownFeatureException(Exception): pass diff --git a/owrx/sdrhu.py b/owrx/sdrhu.py new file mode 100644 index 0000000..b2c6b0f --- /dev/null +++ b/owrx/sdrhu.py @@ -0,0 +1,36 @@ +import threading +import subprocess +import time +from owrx.config import PropertyManager + +import logging +logger = logging.getLogger(__name__) + + +class SdrHuUpdater(threading.Thread): + def __init__(self): + self.doRun = True + super().__init__() + + def update(self): + pm = PropertyManager.getSharedInstance() + cmd = "wget --timeout=15 -4qO- https://sdr.hu/update --post-data \"url=http://{server_hostname}:{web_port}&apikey={sdrhu_key}\" 2>&1".format(**pm.__dict__()) + logger.debug(cmd) + returned=subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).communicate() + returned=returned[0].decode('utf-8') + if "UPDATE:" in returned: + retrytime_mins = 20 + value=returned.split("UPDATE:")[1].split("\n",1)[0] + if value.startswith("SUCCESS"): + logger.info("Update succeeded!") + else: + logger.warning("Update failed, your receiver cannot be listed on sdr.hu! Reason: %s", value) + else: + retrytime_mins = 2 + logger.warning("wget failed while updating, your receiver cannot be listed on sdr.hu!") + return retrytime_mins + + def run(self): + while self.doRun: + retrytime_mins = self.update() + time.sleep(60*retrytime_mins) diff --git a/sdrhu.py b/sdrhu.py index d06ae05..3060789 100755 --- a/sdrhu.py +++ b/sdrhu.py @@ -20,31 +20,13 @@ """ -import config_webrx as cfg, time, subprocess - -def run(continuously=True): - if not cfg.sdrhu_key: return - firsttime="(Your receiver is soon getting listed on sdr.hu!)" - while True: - cmd = "wget --timeout=15 -4qO- https://sdr.hu/update --post-data \"url=http://"+cfg.server_hostname+":"+str(cfg.web_port)+"&apikey="+cfg.sdrhu_key+"\" 2>&1" - print "[openwebrx-sdrhu]", cmd - returned=subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).communicate() - returned=returned[0] - #print returned - if "UPDATE:" in returned: - retrytime_mins = 20 - value=returned.split("UPDATE:")[1].split("\n",1)[0] - if value.startswith("SUCCESS"): - print "[openwebrx-sdrhu] Update succeeded! "+firsttime - firsttime="" - else: - print "[openwebrx-sdrhu] Update failed, your receiver cannot be listed on sdr.hu! Reason:", value - else: - retrytime_mins = 2 - print "[openwebrx-sdrhu] wget failed while updating, your receiver cannot be listed on sdr.hu!" - if not continuously: break - time.sleep(60*retrytime_mins) +from owrx.sdrhu import SdrHuUpdater +from owrx.config import PropertyManager if __name__=="__main__": - run(False) + pm = PropertyManager.getSharedInstance().loadConfig("config_webrx") + + if not "sdrhu_key" in pm: + exit(1) + SdrHuUpdater().update()