from owrx.controllers.admin import AuthorizationMixin
from owrx.controllers.template import WebpageController
from owrx.breadcrumb import Breadcrumb, BreadcrumbItem, BreadcrumbMixin
from owrx.service import Services
from owrx.repeaters import Repeaters
from owrx.eibi import EIBI
from datetime import datetime
import json
import re
import logging
logger = logging.getLogger(__name__)
class ServiceController(AuthorizationMixin, WebpageController):
def indexAction(self):
self.serve_template("services.html", **self.template_variables())
def template_variables(self):
variables = super().template_variables()
variables["services"] = self.renderServices()
return variables
@staticmethod
def renderServices():
return """
| Service |
SDR Profile |
Frequency |
{services}
| {status} |
""".format(
services="".join(ServiceController.renderService(c) for c in Services.listAll()),
status=ServiceController.renderStatus()
)
@staticmethod
def renderStatus():
result = ""
ts = datetime.fromtimestamp(EIBI.lastStarted())
td = str(datetime.now() - ts).split(".", 1)[0]
ts = ts.astimezone().strftime("%H:%M:%S %Z, %d %b %Y")
result += "Server started at {0}, {1} ago.
\n".format(ts, td)
ts = EIBI.lastDownloaded()
if ts > 0:
ts = datetime.fromtimestamp(ts).astimezone().strftime("%H:%M:%S %Z, %d %b %Y")
result += "Shortwave schedule downloaded at {0}.
\n".format(ts)
else:
result += "Shortwave schedule not downloaded.
\n"
ts = Repeaters.lastDownloaded()
if ts > 0:
ts = datetime.fromtimestamp(ts).astimezone().strftime("%H:%M:%S %Z, %d %b %Y")
result += "Repeaters database downloaded at {0}.
\n".format(ts)
else:
result += "Repeaters database not downloaded.
\n"
return result
@staticmethod
def renderService(c):
# Choose units based on frequency
freq = c["freq"]
if freq >= 1000000000:
freq = freq / 1000000000
unit = "GHz"
elif freq >= 30000000:
freq = freq / 1000000
unit = "MHz"
elif freq >= 1000:
freq = freq / 1000
unit = "kHz"
else:
unit = "Hz"
# Removing trailing zeros, converting mode to upper case
freq = re.sub(r"\.?0+$", "", "{0}".format(freq))
# Format row
return "| {0} | {1} {2} | {3}{4} |
".format(
c["mode"].upper(), c["sdr"], c["band"], freq, unit
)