From 5526a79557b795043f433696af031a8ec4b91095 Mon Sep 17 00:00:00 2001 From: serversdwn Date: Mon, 8 Dec 2025 16:50:46 -0500 Subject: [PATCH] v0.1.1 added config.json, removed hardcode config from code --- config.json | 9 ++++++++ series4_emitter.py | 56 +++++++++++++++++++++++++++++++++++----------- 2 files changed, 52 insertions(+), 13 deletions(-) create mode 100644 config.json diff --git a/config.json b/config.json new file mode 100644 index 0000000..c85be52 --- /dev/null +++ b/config.json @@ -0,0 +1,9 @@ +{ + "thordata_path": "C:\\THORDATA", + "scan_interval": 60, + "late_days": 2, + "stale_days": 60, + "sfm_endpoint": "http://10.0.0.41:1001", + "sfm_timeout": 5, + "debug": true +} diff --git a/series4_emitter.py b/series4_emitter.py index 4ebd21b..0979ee4 100644 --- a/series4_emitter.py +++ b/series4_emitter.py @@ -1,5 +1,5 @@ """ -Series 4 Emitter — v0.1.0 +Series 4 Emitter — v0.1.1 Micromate (Series 4) heartbeat emitter for Seismo Fleet Manager (SFM). @@ -31,22 +31,52 @@ except ImportError: # ---------------- Config ---------------- -# Root THORDATA folder on the THOR PC / VM -THORDATA_PATH = r"C:\THORDATA" +def load_config(config_path: str = "config.json") -> Dict[str, Any]: + """ + Load configuration from a JSON file. -# Scan interval in seconds -SCAN_INTERVAL = 60 # 1 minute + Falls back to defaults if file doesn't exist or has errors. + """ + defaults = { + "thordata_path": r"C:\THORDATA", + "scan_interval": 60, + "late_days": 2, + "stale_days": 60, + "sfm_endpoint": "", + "sfm_timeout": 5, + "debug": True + } -# Age thresholds (in days) -LATE_DAYS = 2 # between OK and STALE -STALE_DAYS = 60 # stale if last call >= STALE_DAYS + # Try to find config file relative to script location + script_dir = os.path.dirname(os.path.abspath(__file__)) + full_config_path = os.path.join(script_dir, config_path) -# Optional SFM backend endpoint (leave empty to disable HTTP) -SFM_ENDPOINT = "" # e.g. "http://sfm-backend.local/api/telemetry/series4" -SFM_TIMEOUT = 5 # seconds + if not os.path.exists(full_config_path): + print(f"[WARN] Config file not found at {full_config_path}, using defaults", file=sys.stderr) + return defaults -# Enable/disable debug prints -DEBUG = True + try: + with open(full_config_path, 'r') as f: + config = json.load(f) + # Merge with defaults to ensure all keys exist + return {**defaults, **config} + except json.JSONDecodeError as e: + print(f"[WARN] Invalid JSON in config file: {e}, using defaults", file=sys.stderr) + return defaults + except Exception as e: + print(f"[WARN] Error loading config file: {e}, using defaults", file=sys.stderr) + return defaults + +# Load configuration +config = load_config() + +THORDATA_PATH = config["thordata_path"] +SCAN_INTERVAL = config["scan_interval"] +LATE_DAYS = config["late_days"] +STALE_DAYS = config["stale_days"] +SFM_ENDPOINT = config["sfm_endpoint"] +SFM_TIMEOUT = config["sfm_timeout"] +DEBUG = config["debug"] # Regex: UM12345_YYYYMMDDHHMMSS.MLG MLG_PATTERN = re.compile(r"^(UM\d+)_([0-9]{14})\.MLG$", re.IGNORECASE)