This commit is contained in:
PaulVua
2025-02-10 17:25:34 +01:00
parent e609c38ca0
commit 62c729b63b
8 changed files with 223 additions and 41 deletions

View File

@@ -64,26 +64,33 @@ def load_config():
with open(CONFIG_FILE, "r") as f:
return json.load(f)
def run_script(script_name, interval):
"""Run a script in a loop with a delay."""
def run_script(script_name, interval, delay=0):
"""Run a script in a synchronized loop with an optional start delay."""
script_path = os.path.join(SCRIPT_DIR, script_name) # Build full path
next_run = time.monotonic() + delay # Apply the initial delay
while True:
config = load_config()
if config.get(script_name, True): # Default to True if not found
subprocess.run(["python3", script_path])
time.sleep(interval)
# Wait until the next exact interval
next_run += interval
sleep_time = max(0, next_run - time.monotonic()) # Prevent negative sleep times
time.sleep(sleep_time)
# Define scripts and their execution intervals (seconds)
SCRIPTS = [
("NPM/get_data_v2.py", 60), # Get NPM data every 60s
("loop/SARA_send_data_v2.py", 60), # Send data every 60 seconds
("RTC/save_to_db.py", 1), # SAVE RTC time every 1 second
("BME280/get_data_v2.py", 120) # Get BME280 data every 120 seconds
("RTC/save_to_db.py", 1, 0), # SAVE RTC time every 1 second, no delay
("NPM/get_data_v2.py", 60, 0), # Get NPM data every 60s, no delay
("NPM/get_data_modbus.py", 10, 2), # Get NPM data (modbus 5 channels) every 10s, with 2s delay
("loop/SARA_send_data_v2.py", 60, 1), # Send data every 60 seconds, with 2s delay
("BME280/get_data_v2.py", 120, 0) # Get BME280 data every 120 seconds, no delay
]
# Start threads for enabled scripts
for script_name, interval in SCRIPTS:
thread = threading.Thread(target=run_script, args=(script_name, interval), daemon=True)
for script_name, interval, delay in SCRIPTS:
thread = threading.Thread(target=run_script, args=(script_name, interval, delay), daemon=True)
thread.start()
# Keep the main script running