mmrelaynode/app/command_wrapper.py

31 wiersze
1.2 KiB
Python
Czysty Zwykły widok Historia

2023-09-09 17:52:25 +00:00
import os
import subprocess
import time
2023-11-26 22:46:14 +00:00
def log_to_file(message):
with open('/home/mesh/app/command_output.txt', 'a') as f:
f.write(message + "\n")
2023-09-09 17:52:25 +00:00
def execute_meshtastic_command(options):
"""Execute a meshtastic command with the given options."""
2023-11-26 21:51:34 +00:00
command = ["meshtastic", "--host", "mmrelaydevice", "--port", "4403"] + options.split()
2023-11-26 22:46:14 +00:00
log_to_file(f"Executing command: {' '.join(command)}")
result = subprocess.run(command, capture_output=True, text=True)
2023-11-26 22:46:14 +00:00
log_to_file("Standard Output:\n" + result.stdout)
log_to_file("Standard Error:\n" + result.stderr)
time.sleep(1) # Pause for 1 second between commands
2023-11-26 22:46:14 +00:00
# Print all environment variables at the start
log_to_file("All environment variables:\n" + str(os.environ))
2023-09-09 17:52:25 +00:00
# Loop through environment variables in sequence
index = 1
while True:
command = os.environ.get(f'MESHTASTIC_COMMAND_{index}')
if command:
2023-11-26 22:46:14 +00:00
log_to_file(f"Found command variable: MESHTASTIC_COMMAND_{index} with value: {command}")
2023-09-09 17:52:25 +00:00
execute_meshtastic_command(command)
index += 1
else:
2023-11-26 22:46:14 +00:00
log_to_file(f"No more MESHTASTIC_COMMAND variables found, ending at index {index-1}.")
break