|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | +from typing import Any, Callable, TypedDict |
| 5 | + |
| 6 | +from geocompy.communication import open_serial |
| 7 | +from geocompy.geo import GeoCom |
| 8 | +from geocompy.geo.gctypes import GeoComSubsystem, GeoComResponse, GeoComCode |
| 9 | +from geocompy.gsi.dna import GsiOnlineDNA |
| 10 | +from geocompy.gsi.gsitypes import GsiOnlineResponse |
| 11 | +from geocompy.gsi.dna.settings import GsiOnlineDNASettings |
| 12 | + |
| 13 | +from ..utils import echo_red, echo_yellow |
| 14 | +from .io import read_settings |
| 15 | +from .validate import validate_settings |
| 16 | + |
| 17 | + |
| 18 | +class _SubsystemSettingsDict(TypedDict): |
| 19 | + subsystem: str |
| 20 | + options: dict[str, Any] |
| 21 | + |
| 22 | + |
| 23 | +class _SettingsDict(TypedDict): |
| 24 | + protocol: str |
| 25 | + settings: list[_SubsystemSettingsDict] |
| 26 | + |
| 27 | + |
| 28 | +def set_setting_geocom( |
| 29 | + system: GeoComSubsystem, |
| 30 | + setting: str, |
| 31 | + value: int | float | bool | str | list[int | float | bool | str] |
| 32 | +) -> None: |
| 33 | + if isinstance(value, bool): |
| 34 | + name = f"switch_{setting}" |
| 35 | + else: |
| 36 | + name = f"set_{setting}" |
| 37 | + |
| 38 | + method: Callable[ |
| 39 | + ..., |
| 40 | + GeoComResponse[Any] |
| 41 | + ] | None = getattr(system, name) |
| 42 | + if method is None: |
| 43 | + echo_yellow(f"Could not find '{name}' to set '{setting}'") |
| 44 | + return |
| 45 | + |
| 46 | + if isinstance(value, list): |
| 47 | + response = method(*value) |
| 48 | + else: |
| 49 | + response = method(value) |
| 50 | + |
| 51 | + if response.error != GeoComCode.OK: |
| 52 | + echo_yellow(f"Could not set '{setting}' ({response.error.name})") |
| 53 | + return |
| 54 | + |
| 55 | + |
| 56 | +def set_setting_gsidna( |
| 57 | + system: GsiOnlineDNASettings, |
| 58 | + setting: str, |
| 59 | + value: int | float | bool | str |
| 60 | +) -> None: |
| 61 | + name = f"set_{setting}" |
| 62 | + method: Callable[ |
| 63 | + ..., |
| 64 | + GsiOnlineResponse[bool] |
| 65 | + ] | None = getattr(system, name) |
| 66 | + if method is None: |
| 67 | + echo_yellow(f"Could not find '{name}' to set '{setting}'") |
| 68 | + return |
| 69 | + |
| 70 | + response = method(value) |
| 71 | + |
| 72 | + if response.value is None or not response.value: |
| 73 | + echo_yellow(f"Could not set '{setting}' ({response.response})") |
| 74 | + return |
| 75 | + |
| 76 | + |
| 77 | +def upload_settings_geocom( |
| 78 | + protocol: GeoCom, |
| 79 | + settings: _SettingsDict |
| 80 | +) -> None: |
| 81 | + for item in settings["settings"]: |
| 82 | + sysname = item["subsystem"] |
| 83 | + subsystem: Any = getattr(protocol, sysname) |
| 84 | + if subsystem is None: |
| 85 | + echo_red(f"Could not find '{sysname}' subsystem") |
| 86 | + exit(1) |
| 87 | + |
| 88 | + for option, value in item["options"].items(): |
| 89 | + if value is None: |
| 90 | + continue |
| 91 | + |
| 92 | + set_setting_geocom(subsystem, option, value) |
| 93 | + |
| 94 | + |
| 95 | +def upload_settings_gsidna( |
| 96 | + protocol: GsiOnlineDNA, |
| 97 | + settings: _SettingsDict |
| 98 | +) -> None: |
| 99 | + for item in settings["settings"]: |
| 100 | + sysname = item["subsystem"] |
| 101 | + subsystem: Any = getattr(protocol, sysname) |
| 102 | + if subsystem is None: |
| 103 | + echo_red(f"Could not find '{sysname}' subsystem") |
| 104 | + exit(1) |
| 105 | + |
| 106 | + for option, value in item["options"].items(): |
| 107 | + if value is None: |
| 108 | + continue |
| 109 | + |
| 110 | + set_setting_gsidna(subsystem, option, value) |
| 111 | + |
| 112 | + |
| 113 | +def main( |
| 114 | + port: str, |
| 115 | + settings: Path, |
| 116 | + baud: int = 9600, |
| 117 | + timeout: int = 15, |
| 118 | + retry: int = 1, |
| 119 | + sync_after_timeout: bool = False, |
| 120 | + format: str = "auto" |
| 121 | +) -> None: |
| 122 | + data = read_settings(settings, format) |
| 123 | + if not validate_settings(data): |
| 124 | + echo_red("Settings file does not follow schema") |
| 125 | + exit(1) |
| 126 | + |
| 127 | + with open_serial( |
| 128 | + port, |
| 129 | + retry=retry, |
| 130 | + sync_after_timeout=sync_after_timeout, |
| 131 | + speed=baud, |
| 132 | + timeout=timeout |
| 133 | + ) as com: |
| 134 | + if "geocom" in data: |
| 135 | + tps = GeoCom(com) |
| 136 | + upload_settings_geocom(tps, data["geocom"]) |
| 137 | + elif "gsidna" in data: |
| 138 | + dna = GsiOnlineDNA(com) |
| 139 | + upload_settings_gsidna(dna, data["gsidna"]) |
0 commit comments