From 445340704f2c94daa2e4e44f757244943e2d123b Mon Sep 17 00:00:00 2001 From: Arne Johannessen Date: Sun, 27 Oct 2019 12:43:33 +0100 Subject: [PATCH] Add skeleton of nav data flash/erase CLI proposed in #1 --- hxtool/cli/nav.py | 49 ++++++++++++++++++++++++++++++++++++++++++++++- hxtool/config.py | 6 ++++++ 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/hxtool/cli/nav.py b/hxtool/cli/nav.py index d3dbc42..b1a061d 100644 --- a/hxtool/cli/nav.py +++ b/hxtool/cli/nav.py @@ -48,7 +48,7 @@ def run(self): result = max(self.dump(hx), result) if self.args.flash or self.args.erase: - raise NotImplementedError + result = max(self.flash_erase(hx), result) return result @@ -62,6 +62,53 @@ def dump(self, hx): return 0 + def flash_erase(self, hx): + nav_data = { "waypoints": [], "routes": [] } + + if self.args.flash: + if self.args.gpx: + logger.info("Reading GPX nav data from `{}`".format(self.args.gpx)) + raise NotImplementedError + + logger.info(log_nav_data("Read {w} waypoint{ws} and {r} route{rs} from file", nav_data)) + if self.args.erase: + logger.info("Will replace nav data on device") + else: + logger.info("Will append to nav data on device") + + logger.info("Reading nav data from handset") + raise NotImplementedError + + logger.info(log_nav_data("In total {w} waypoint{ws} and {r} route{rs}", nav_data)) + if nav_data_oversized(nav_data, hx): + return 10 + + logger.info("Writing nav data to handset") + raise NotImplementedError + + return 0 + + +def log_nav_data(text: str, nav_data: dict) -> str: + waypoint_count = len(nav_data["waypoints"]) + route_count = len(nav_data["routes"]) + return text.format( + w = waypoint_count, ws = "s" if waypoint_count != 1 else "", + r = route_count, rs = "s" if route_count != 1 else "" ) + + +def nav_data_oversized(nav_data: dict, hx: object) -> bool: + limits = hx.config.limits() + oversized = False + if len(nav_data["waypoints"]) > limits["waypoints"]: + logger.critical("Too many waypoints to fit on device (maximum: {})".format(limits["waypoints"])) + oversized = True + if len(nav_data["routes"]) > limits["routes"]: + logger.critical("Too many routes to fit on device (maximum: {})".format(limits["routes"])) + oversized = True + return oversized + + def write_gpx(nav_data: dict, file_name: str) -> int: if len(nav_data["waypoints"]) == 0: logger.warning("No waypoints in device. Not writing empty GPX file") diff --git a/hxtool/config.py b/hxtool/config.py index 13d0cb8..7a06008 100644 --- a/hxtool/config.py +++ b/hxtool/config.py @@ -14,6 +14,12 @@ class GenericHXConfig(object): def __init__(self, protocol: GenericHXProtocol): self.p = protocol + def limits(self): + return { + "waypoints": 200, + "routes": 20, + } + def config_read(self, progress=False): config_data = b'' bytes_to_go = 0x8000