MeshDD-Bot/meshbot/config.py
ppfeiffer 511ff20842 feat(ui): Sidebar-Gruppe Konfigurationen, /config-Seite, MeshDD-Dashboard (closes #4)
- app.js: Sidebar um Gruppen-Support erweitert; Konfigurationen-Gruppe
  mit Scheduler, NINA, Einstellungen (/config) als Untereinträge
- style.css: .sidebar-group-label + .sidebar-link-sub
- config.py: save()-Funktion für persistentes Schreiben in config.yaml
- webserver.py: GET/PUT /api/config + GET /config Route (Admin)
- static/config.html + static/js/config.js: neue Konfigurationsseite
  (Bot, Meshtastic, Web, Links editierbar)
- Alle HTML-Dateien: MeshDD-Bot → MeshDD-Dashboard

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-20 22:33:18 +01:00

84 lines
2 KiB
Python

import asyncio
import logging
import os
import yaml
logger = logging.getLogger(__name__)
CONFIG_PATH = os.environ.get("CONFIG_PATH", os.path.join(os.path.dirname(os.path.dirname(__file__)), "config", "config.yaml"))
_config = {}
_mtime = 0.0
_callbacks = []
def _load():
global _config, _mtime
with open(CONFIG_PATH, "r") as f:
_config = yaml.safe_load(f)
_mtime = os.path.getmtime(CONFIG_PATH)
logger.info("Config loaded from %s", CONFIG_PATH)
def _reload_if_changed():
global _mtime
try:
current_mtime = os.path.getmtime(CONFIG_PATH)
if current_mtime != _mtime:
old_config = _config.copy()
_load()
logger.info("Config reloaded (changed)")
for cb in _callbacks:
try:
cb(old_config, _config)
except Exception:
logger.exception("Error in config reload callback")
except Exception:
logger.exception("Error checking config file")
def on_reload(callback):
_callbacks.append(callback)
async def watch(interval: float = 2.0):
while True:
await asyncio.sleep(interval)
_reload_if_changed()
def _deep_merge(base: dict, updates: dict):
for k, v in updates.items():
if isinstance(v, dict) and isinstance(base.get(k), dict):
_deep_merge(base[k], v)
else:
base[k] = v
def save(updates: dict):
"""Deep-merge updates into the live config and persist to config.yaml."""
global _config, _mtime
_deep_merge(_config, updates)
with open(CONFIG_PATH, "w") as f:
yaml.dump(_config, f, default_flow_style=False, allow_unicode=True, sort_keys=False)
_mtime = os.path.getmtime(CONFIG_PATH)
logger.info("Config saved")
def get(key: str, default=None):
keys = key.split(".")
val = _config
for k in keys:
if isinstance(val, dict):
val = val.get(k)
else:
return default
if val is None:
return default
return val
# Load on import
_load()