v0.1.1 added config.json, removed hardcode config from code
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user