This commit is contained in:
Your Name
2025-03-26 17:17:40 +01:00
parent d0e1ad18e9
commit 76a676925d
10 changed files with 283 additions and 18 deletions

View File

@@ -66,7 +66,7 @@ def load_config():
return json.load(f)
def run_script(script_name, interval, delay=0):
"""Run a script in a synchronized loop with an optional start delay."""
"""Run a PYTHON 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
@@ -80,22 +80,39 @@ def run_script(script_name, interval, delay=0):
sleep_time = max(0, next_run - time.monotonic()) # Prevent negative sleep times
time.sleep(sleep_time)
# Define scripts and their execution intervals (seconds)
SCRIPTS = [
("RTC/save_to_db.py", 1, 0), # SAVE RTC time every 1 second, no delay
("NPM/get_data_modbus_v3.py", 10, 0), # Get NPM data (modbus 5 channels) every 10s,
("envea/read_value_v2.py", 10, 0), # Get Envea data every 10s,
("MH-Z19/write_data.py", 10, 0), # Get CO2 values every 10s,
("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
("sqlite/flush_old_data.py", 86400, 0) # flush old data inside db every day ()
# ✅ **Scripts that should start immediately**
IMMEDIATE_SCRIPTS = [
("RTC/save_to_db.py", 1, 0),
("NPM/get_data_modbus_v3.py", 10, 0),
("envea/read_value_v2.py", 10, 0),
("MH-Z19/write_data.py", 10, 0),
("loop/SARA_send_data_v2.py", 60, 1),
("BME280/get_data_v2.py", 120, 0),
("sqlite/flush_old_data.py", 86400, 0)
]
# Start threads for enabled scripts
for script_name, interval, delay in SCRIPTS:
# ✅ **The compiled binary that must wait for welcome_screen**
WAIT_BINARY = "/var/www/moduleair_pro_4g/matrix/test_forms" # Adjust path if needed
# 🚀 **Start all scripts that can run immediately**
for script_name, interval, delay in IMMEDIATE_SCRIPTS:
thread = threading.Thread(target=run_script, args=(script_name, interval, delay), daemon=True)
thread.start()
# 🚀 **Run welcome_screen in parallel**
print("🚀 Running welcome screen...")
welcome_process = subprocess.Popen(["sudo", "/var/www/moduleair_pro_4g/matrix/welcome_screen"])
print("✅ Welcome screen started in parallel with other scripts.")
# 🕒 **Wait for welcome_screen to finish before running WAIT_BINARY**
welcome_process.wait()
print("✅ Welcome screen finished. Starting another_screen...")
# 🚀 **Now run the compiled binary `another_screen`**
subprocess.run(["sudo", WAIT_BINARY])
print("✅ another_screen execution completed.")
# Keep the main script running
while True:
time.sleep(1)