v0.1.1 added config.json, removed hardcode config from code

This commit is contained in:
serversdwn
2025-12-08 16:50:46 -05:00
parent 26be5410e3
commit 5526a79557
2 changed files with 52 additions and 13 deletions

9
config.json Normal file
View File

@@ -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
}

View File

@@ -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). Micromate (Series 4) heartbeat emitter for Seismo Fleet Manager (SFM).
@@ -31,22 +31,52 @@ except ImportError:
# ---------------- Config ---------------- # ---------------- Config ----------------
# Root THORDATA folder on the THOR PC / VM def load_config(config_path: str = "config.json") -> Dict[str, Any]:
THORDATA_PATH = r"C:\THORDATA" """
Load configuration from a JSON file.
# Scan interval in seconds Falls back to defaults if file doesn't exist or has errors.
SCAN_INTERVAL = 60 # 1 minute """
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) # Try to find config file relative to script location
LATE_DAYS = 2 # between OK and STALE script_dir = os.path.dirname(os.path.abspath(__file__))
STALE_DAYS = 60 # stale if last call >= STALE_DAYS full_config_path = os.path.join(script_dir, config_path)
# Optional SFM backend endpoint (leave empty to disable HTTP) if not os.path.exists(full_config_path):
SFM_ENDPOINT = "" # e.g. "http://sfm-backend.local/api/telemetry/series4" print(f"[WARN] Config file not found at {full_config_path}, using defaults", file=sys.stderr)
SFM_TIMEOUT = 5 # seconds return defaults
# Enable/disable debug prints try:
DEBUG = True 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 # Regex: UM12345_YYYYMMDDHHMMSS.MLG
MLG_PATTERN = re.compile(r"^(UM\d+)_([0-9]{14})\.MLG$", re.IGNORECASE) MLG_PATTERN = re.compile(r"^(UM\d+)_([0-9]{14})\.MLG$", re.IGNORECASE)