refactor the sdr.hu updater into the new server, too
This commit is contained in:
parent
697e177f00
commit
210fe5352f
12
openwebrx.py
12
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 <randras@sdr.hu>
|
||||
|
||||
""")
|
||||
|
||||
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 <randras@sdr.hu>
|
||||
# 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()
|
||||
|
||||
|
@ -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
|
||||
|
||||
|
36
owrx/sdrhu.py
Normal file
36
owrx/sdrhu.py
Normal 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)
|
32
sdrhu.py
32
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()
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user