refactor the sdr.hu updater into the new server, too

This commit is contained in:
Jakob Ketterl 2019-05-12 14:35:25 +02:00
parent 697e177f00
commit 210fe5352f
4 changed files with 64 additions and 31 deletions

View File

@ -3,6 +3,7 @@ from owrx.http import RequestHandler
from owrx.config import PropertyManager, FeatureDetector from owrx.config import PropertyManager, FeatureDetector
from owrx.source import SdrService from owrx.source import SdrService
from socketserver import ThreadingMixIn from socketserver import ThreadingMixIn
from owrx.sdrhu import SdrHuUpdater
import logging import logging
logging.basicConfig(level = logging.DEBUG, format = "%(asctime)s - %(name)s - %(levelname)s - %(message)s") logging.basicConfig(level = logging.DEBUG, format = "%(asctime)s - %(name)s - %(levelname)s - %(message)s")
@ -22,12 +23,7 @@ Author contact info: Andras Retzler, HA7ILM <randras@sdr.hu>
""") """)
cfg = __import__("config_webrx") pm = PropertyManager.getSharedInstance().loadConfig("config_webrx")
pm = PropertyManager.getSharedInstance()
for name, value in cfg.__dict__.items():
if name.startswith("__"):
continue
pm[name] = value
featureDetector = FeatureDetector() featureDetector = FeatureDetector()
if not featureDetector.is_available("core"): if not featureDetector.is_available("core"):
@ -39,6 +35,10 @@ Author contact info: Andras Retzler, HA7ILM <randras@sdr.hu>
# Get error messages about unknown / unavailable features as soon as possible # Get error messages about unknown / unavailable features as soon as possible
SdrService.loadProps() 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 = ThreadedHttpServer(('0.0.0.0', pm.getPropertyValue("web_port")), RequestHandler)
server.serve_forever() server.serve_forever()

View File

@ -53,6 +53,9 @@ class PropertyManager(object):
prop.wire(fireCallbacks) prop.wire(fireCallbacks)
return self return self
def __contains__(self, name):
return self.hasProperty(name)
def __getitem__(self, name): def __getitem__(self, name):
return self.getPropertyValue(name) return self.getPropertyValue(name)
@ -61,6 +64,9 @@ class PropertyManager(object):
self.add(name, Property()) self.add(name, Property())
self.getProperty(name).setValue(value) self.getProperty(name).setValue(value)
def __dict__(self):
return {k:v.getValue() for k, v in self.properties.items()}
def hasProperty(self, name): def hasProperty(self, name):
return name in self.properties return name in self.properties
@ -86,6 +92,15 @@ class PropertyManager(object):
p.setValue(other_pm[key]) p.setValue(other_pm[key])
return self 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): class UnknownFeatureException(Exception):
pass pass

36
owrx/sdrhu.py Normal file
View File

@ -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)

View File

@ -20,31 +20,13 @@
""" """
import config_webrx as cfg, time, subprocess from owrx.sdrhu import SdrHuUpdater
from owrx.config import PropertyManager
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)
if __name__=="__main__": if __name__=="__main__":
run(False) pm = PropertyManager.getSharedInstance().loadConfig("config_webrx")
if not "sdrhu_key" in pm:
exit(1)
SdrHuUpdater().update()