From fd9e913a49a2cad190d5cd6ae8d5ba65af5293a7 Mon Sep 17 00:00:00 2001 From: Jakob Ketterl Date: Sun, 29 Mar 2020 18:08:26 +0200 Subject: [PATCH] config migration for receiver_gps --- htdocs/map.js | 4 ++-- htdocs/openwebrx.js | 2 +- owrx/config.py | 34 ++++++++++++++++++++++++++++++++-- owrx/controllers/status.py | 7 ++++--- owrx/kiss.py | 3 ++- owrx/locator.py | 3 ++- owrx/service/schedule.py | 3 ++- 7 files changed, 45 insertions(+), 11 deletions(-) diff --git a/htdocs/map.js b/htdocs/map.js index 1fb2ec5..948e83b 100644 --- a/htdocs/map.js +++ b/htdocs/map.js @@ -219,8 +219,8 @@ map = new google.maps.Map($('.openwebrx-map')[0], { center: { - lat: config.receiver_gps[0], - lng: config.receiver_gps[1] + lat: config.receiver_gps.lat, + lng: config.receiver_gps.lon }, zoom: 5, mapTypeId: mapTypeId diff --git a/htdocs/openwebrx.js b/htdocs/openwebrx.js index 0f8540a..6029463 100644 --- a/htdocs/openwebrx.js +++ b/htdocs/openwebrx.js @@ -1097,7 +1097,7 @@ function on_ws_recv(evt) { case "receiver_details": var r = json['value']; e('webrx-rx-title').innerHTML = r['receiver_name']; - var query = encodeURIComponent(r['receiver_gps'][0] + ',' + r['receiver_gps'][1]); + var query = encodeURIComponent(r['receiver_gps']['lat'] + ',' + r['receiver_gps']['lon']); e('webrx-rx-desc').innerHTML = r['receiver_location'] + ' | Loc: ' + r['locator'] + ', ASL: ' + r['receiver_asl'] + ' m, [maps]'; e('webrx-rx-photo-title').innerHTML = r['photo_title']; e('webrx-rx-photo-desc').innerHTML = r['photo_desc']; diff --git a/owrx/config.py b/owrx/config.py index b833efc..10f0dd7 100644 --- a/owrx/config.py +++ b/owrx/config.py @@ -3,6 +3,7 @@ import importlib.util import os import logging import json +from abc import ABC, abstractmethod logger = logging.getLogger(__name__) @@ -20,8 +21,27 @@ class ConfigError(object): return "Configuration Error (key: {0}): {1}".format(self.key, self.message) +class ConfigMigrator(ABC): + @abstractmethod + def migrate(self, config): + pass + + +class ConfigMigratorVersion1(ConfigMigrator): + def migrate(self, config): + gps = config["receiver_gps"] + config["receiver_gps"] = {"lat": gps[0], "lon": gps[1]} + + config["version"] = 2 + return config + + class Config: sharedConfig = None + currentVersion = 2 + migrators = { + 1: ConfigMigratorVersion1() + } @staticmethod def _loadPythonFile(file): @@ -45,7 +65,7 @@ class Config: @staticmethod def _loadConfig(): - for file in ["settings.json", "/etc/openwebrx/config_webrx.py", "./config_webrx.py"]: + for file in ["./settings.json", "/etc/openwebrx/config_webrx.py", "./config_webrx.py"]: try: if file.endswith(".py"): return Config._loadPythonFile(file) @@ -60,7 +80,7 @@ class Config: @staticmethod def get(): if Config.sharedConfig is None: - Config.sharedConfig = Config._loadConfig() + Config.sharedConfig = Config._migrate(Config._loadConfig()) return Config.sharedConfig @staticmethod @@ -89,3 +109,13 @@ class Config: if not os.access(pm[key], os.W_OK): return ConfigError(key, "temporary directory is not writable") return None + + @staticmethod + def _migrate(config): + version = config["version"] if "version" in config else 1 + if version == Config.currentVersion: + return config + + logger.debug("migrating config from version %i", version) + migrator = Config.migrators[version] + return migrator.migrate(config) diff --git a/owrx/controllers/status.py b/owrx/controllers/status.py index 12be28f..1c73f66 100644 --- a/owrx/controllers/status.py +++ b/owrx/controllers/status.py @@ -10,6 +10,8 @@ import json class StatusController(Controller): def indexAction(self): pm = Config.get() + # convert to old format + gps = (pm["receiver_gps"]["lat"], pm["receiver_gps"]["lon"]) # TODO keys that have been left out since they are no longer simple strings: sdr_hw, bands, antenna vars = { "status": "active", @@ -17,7 +19,7 @@ class StatusController(Controller): "op_email": pm["receiver_admin"], "users": ClientRegistry.getSharedInstance().clientCount(), "users_max": pm["max_clients"], - "gps": pm["receiver_gps"], + "gps": gps, "asl": pm["receiver_asl"], "loc": pm["receiver_location"], "sw_version": openwebrx_version, @@ -44,12 +46,11 @@ class StatusController(Controller): def jsonAction(self): pm = Config.get() - gps = pm["receiver_gps"] status = { "receiver": { "name": pm["receiver_name"], "admin": pm["receiver_admin"], - "gps": {"lat": gps[0], "lon": gps[1]}, + "gps": pm["receiver_gps"], "asl": pm["receiver_asl"], "location": pm["receiver_location"], }, diff --git a/owrx/kiss.py b/owrx/kiss.py index 5da2907..440cdab 100644 --- a/owrx/kiss.py +++ b/owrx/kiss.py @@ -39,7 +39,8 @@ IGLOGIN {callsign} {password} ) if pm["aprs_igate_beacon"]: - (lat, lon) = pm["receiver_gps"] + lat = pm["receiver_gps"]["lat"] + lon = pm["receiver_gps"]["lon"] direction_ns = "N" if lat > 0 else "S" direction_we = "E" if lon > 0 else "W" lat = abs(lat) diff --git a/owrx/locator.py b/owrx/locator.py index ec80b03..52c37e5 100644 --- a/owrx/locator.py +++ b/owrx/locator.py @@ -2,7 +2,8 @@ class Locator(object): @staticmethod def fromCoordinates(coordinates, depth=3): - lat, lon = coordinates + lat = coordinates["lat"] + lon = coordinates["lon"] lon = lon + 180 lat = lat + 90 diff --git a/owrx/service/schedule.py b/owrx/service/schedule.py index 62df44d..992757e 100644 --- a/owrx/service/schedule.py +++ b/owrx/service/schedule.py @@ -135,7 +135,8 @@ class DaylightSchedule(TimerangeSchedule): def getSunTimes(self, date): pm = Config.get() - lat, lng = pm["receiver_gps"] + lat = pm["receiver_gps"]["lat"] + lng = pm["receiver_gps"]["lon"] degtorad = math.pi / 180 radtodeg = 180 / math.pi