From 2755f6b3831efba2ab19a3894c1b4049a7a6ca73 Mon Sep 17 00:00:00 2001 From: WildSoft Date: Tue, 17 Nov 2026 14:57:52 +0300 Subject: [PATCH] Update public source --- reports/map_service.py | 16 ++++-- src/lifecycle_manager.py | 43 ++++++---------- src/tileserver_config.py | 105 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 132 insertions(+), 32 deletions(-) create mode 100644 src/tileserver_config.py diff --git a/reports/map_service.py b/reports/map_service.py index 2468139..48356d6 100644 --- a/reports/map_service.py +++ b/reports/map_service.py @@ -2,9 +2,19 @@ Centralized map service configuration. """ -MAP_STYLE_URL = ( - "http://127.0.0.1:8080/styles/server-map/style.json" -) +import sys +from pathlib import Path + + +PROJECT_ROOT = Path(__file__).resolve().parent.parent + +if str(PROJECT_ROOT) not in sys.path: + sys.path.insert(0, str(PROJECT_ROOT)) + +from src.tileserver_config import load_tileserver_config + + +MAP_STYLE_URL = load_tileserver_config(PROJECT_ROOT)["style_url"] def get_map_style_url(): diff --git a/src/lifecycle_manager.py b/src/lifecycle_manager.py index d15665e..c69da21 100644 --- a/src/lifecycle_manager.py +++ b/src/lifecycle_manager.py @@ -16,40 +16,25 @@ from pathlib import Path PROJECT_ROOT = Path(__file__).resolve().parent.parent + +if str(PROJECT_ROOT) not in sys.path: + sys.path.insert(0, str(PROJECT_ROOT)) + +from tileserver_config import load_tileserver_config + +TILESERVER_CONFIG_DATA = load_tileserver_config(PROJECT_ROOT) + UNIT_NAME = "wifi-gps-mapper" APP_HOST = "127.0.0.1" APP_PORT = 5000 -TILE_HOST = "127.0.0.1" -TILE_PORT = int(os.environ.get("WIFI_GPS_TILESERVER_PORT", "8080")) -TILE_STYLE_URL = ( - f"http://{TILE_HOST}:{TILE_PORT}" - "/styles/server-map/style.json" -) -TILESERVER_DIR = Path( - os.environ.get( - "WIFI_GPS_TILESERVER_DIR", - str(Path.home() / "Projects" / "server-map") - ) -).expanduser() +TILE_HOST = TILESERVER_CONFIG_DATA["host"] +TILE_PORT = TILESERVER_CONFIG_DATA["port"] +TILE_STYLE_URL = TILESERVER_CONFIG_DATA["style_url"] -TILESERVER_ENTRY = Path( - os.environ.get( - "WIFI_GPS_TILESERVER_ENTRY", - str( - TILESERVER_DIR - / "node_modules" - / "tileserver-gl-light" - / "src" - / "main.js" - ) - ) -).expanduser() - -TILESERVER_CONFIG = os.environ.get( - "WIFI_GPS_TILESERVER_CONFIG", - "config.json" -) +TILESERVER_DIR = TILESERVER_CONFIG_DATA["directory"] +TILESERVER_ENTRY = TILESERVER_CONFIG_DATA["entry"] +TILESERVER_CONFIG = TILESERVER_CONFIG_DATA["config"] STARTUP_TIMEOUT = 30 POLL_INTERVAL = 0.5 diff --git a/src/tileserver_config.py b/src/tileserver_config.py new file mode 100644 index 0000000..4d816fa --- /dev/null +++ b/src/tileserver_config.py @@ -0,0 +1,105 @@ +""" +Shared TileServer configuration loader. +""" + +import os +from pathlib import Path + +import yaml + + +DEFAULT_ENTRY = "node_modules/tileserver-gl-light/src/main.js" +DEFAULT_CONFIG = "config.json" +DEFAULT_STYLE = "server-map" + + +def load_tileserver_config(project_root): + project_root = Path(project_root).expanduser().resolve() + config_file = project_root / "config" / "config.yaml" + + try: + with config_file.open("r", encoding="utf-8") as file: + application_config = yaml.safe_load(file) or {} + except (OSError, yaml.YAMLError) as error: + raise RuntimeError( + f"Не удалось загрузить конфигурацию {config_file}: {error}" + ) from error + + if not isinstance(application_config, dict): + raise RuntimeError( + "Корень config/config.yaml должен быть YAML-объектом." + ) + + section = application_config.get("tileserver", {}) + + if not isinstance(section, dict): + raise RuntimeError( + "Раздел tileserver в config/config.yaml должен быть YAML-объектом." + ) + + directory_value = os.environ.get( + "WIFI_GPS_TILESERVER_DIR", + section.get("directory", "map-server") + ) + + directory = Path(directory_value).expanduser() + + if "WIFI_GPS_TILESERVER_DIR" not in os.environ and not directory.is_absolute(): + directory = project_root / directory + + directory = directory.resolve() + + entry_from_environment = "WIFI_GPS_TILESERVER_ENTRY" in os.environ + entry_value = os.environ.get( + "WIFI_GPS_TILESERVER_ENTRY", + section.get("entry", DEFAULT_ENTRY) + ) + + entry = Path(entry_value).expanduser() + + if not entry.is_absolute() and not entry_from_environment: + entry = directory / entry + + config_value = os.environ.get( + "WIFI_GPS_TILESERVER_CONFIG", + section.get("config", DEFAULT_CONFIG) + ) + + config = Path(config_value).expanduser() + config_path = config if config.is_absolute() else directory / config + + host = str(section.get("host", "127.0.0.1")).strip() + + try: + port = int( + os.environ.get( + "WIFI_GPS_TILESERVER_PORT", + section.get("port", 8080) + ) + ) + except (TypeError, ValueError) as error: + raise RuntimeError("Некорректный порт TileServer.") from error + + if not host: + raise RuntimeError("Не задан host для TileServer.") + + if not 1 <= port <= 65535: + raise RuntimeError(f"Некорректный порт TileServer: {port}") + + style = str(section.get("style", DEFAULT_STYLE)).strip().strip("/") + + if not style: + raise RuntimeError("Не задан style для TileServer.") + + return { + "host": host, + "port": port, + "directory": directory, + "entry": entry, + "config": str(config), + "config_path": config_path, + "style": style, + "style_url": ( + f"http://{host}:{port}/styles/{style}/style.json" + ), + }