mirror of
https://github.com/ChuckBuilds/LEDMatrix.git
synced 2026-04-10 21:03:01 +00:00
add web page to config and enable autostart
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
{
|
{
|
||||||
|
"web_display_autostart": true,
|
||||||
"timezone": "America/Chicago",
|
"timezone": "America/Chicago",
|
||||||
"location": {
|
"location": {
|
||||||
"city": "Dallas",
|
"city": "Dallas",
|
||||||
|
|||||||
@@ -13,45 +13,114 @@ fi
|
|||||||
# Get the home directory of the actual user
|
# Get the home directory of the actual user
|
||||||
USER_HOME=$(eval echo ~$ACTUAL_USER)
|
USER_HOME=$(eval echo ~$ACTUAL_USER)
|
||||||
|
|
||||||
|
# Determine the Project Root Directory (where this script is located)
|
||||||
|
PROJECT_ROOT_DIR=$(cd "$(dirname "$0")" && pwd)
|
||||||
|
|
||||||
echo "Installing LED Matrix Display Service for user: $ACTUAL_USER"
|
echo "Installing LED Matrix Display Service for user: $ACTUAL_USER"
|
||||||
echo "Using home directory: $USER_HOME"
|
echo "Using home directory: $USER_HOME"
|
||||||
|
echo "Project root directory: $PROJECT_ROOT_DIR"
|
||||||
|
|
||||||
# Create a temporary service file with the correct paths
|
# Create a temporary service file for the main display with the correct paths
|
||||||
sed "s|/home/ledpi|$USER_HOME|g" ledmatrix.service > /tmp/ledmatrix.service.tmp
|
# Assuming ledmatrix.service template exists and uses /home/ledpi as a placeholder for user home
|
||||||
|
if [ -f "ledmatrix.service" ]; then
|
||||||
|
sed "s|/home/ledpi|$USER_HOME|g; s|__PROJECT_ROOT_DIR__|$PROJECT_ROOT_DIR|g; s|__USER__|$ACTUAL_USER|g" ledmatrix.service > /tmp/ledmatrix.service.tmp
|
||||||
# Copy the service file to the systemd directory
|
# Copy the service file to the systemd directory
|
||||||
sudo cp /tmp/ledmatrix.service.tmp /etc/systemd/system/ledmatrix.service
|
sudo cp /tmp/ledmatrix.service.tmp /etc/systemd/system/ledmatrix.service
|
||||||
|
|
||||||
# Clean up
|
# Clean up
|
||||||
rm /tmp/ledmatrix.service.tmp
|
rm /tmp/ledmatrix.service.tmp
|
||||||
|
else
|
||||||
|
echo "WARNING: ledmatrix.service template not found. Main display service not configured."
|
||||||
|
fi
|
||||||
|
|
||||||
# Reload systemd to recognize the new service
|
|
||||||
|
# Reload systemd to recognize the new service (or modified service)
|
||||||
sudo systemctl daemon-reload
|
sudo systemctl daemon-reload
|
||||||
|
|
||||||
# Enable the service to start on boot
|
if [ -f "/etc/systemd/system/ledmatrix.service" ]; then
|
||||||
|
echo "Enabling ledmatrix.service (main display) to start on boot..."
|
||||||
sudo systemctl enable ledmatrix.service
|
sudo systemctl enable ledmatrix.service
|
||||||
|
echo "Starting ledmatrix.service (main display)..."
|
||||||
# Start the service now
|
|
||||||
sudo systemctl start ledmatrix.service
|
sudo systemctl start ledmatrix.service
|
||||||
|
else
|
||||||
|
echo "Skipping enable/start for ledmatrix.service as it was not configured."
|
||||||
|
fi
|
||||||
|
|
||||||
|
# === LEDMatrix Web Interface service (ledmatrix-web.service) ===
|
||||||
|
echo "Installing LEDMatrix Web Interface service (ledmatrix-web.service)..."
|
||||||
|
|
||||||
|
WEB_SERVICE_FILE_CONTENT=$(cat <<EOF
|
||||||
|
[Unit]
|
||||||
|
Description=LED Matrix Web Interface (Conditional Start)
|
||||||
|
After=network.target
|
||||||
|
# Wants=ledmatrix.service
|
||||||
|
# After=network.target ledmatrix.service
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
Type=simple
|
||||||
|
ExecStart=/usr/bin/python3 ${PROJECT_ROOT_DIR}/start_web_conditionally.py
|
||||||
|
WorkingDirectory=${PROJECT_ROOT_DIR}
|
||||||
|
StandardOutput=journal
|
||||||
|
StandardError=journal
|
||||||
|
User=${ACTUAL_USER}
|
||||||
|
Restart=on-failure
|
||||||
|
# Environment="PYTHONUNBUFFERED=1"
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
||||||
|
EOF
|
||||||
|
)
|
||||||
|
|
||||||
|
# Write the new service file
|
||||||
|
echo "$WEB_SERVICE_FILE_CONTENT" | sudo tee /etc/systemd/system/ledmatrix-web.service > /dev/null
|
||||||
|
|
||||||
|
echo "Reloading systemd daemon for web service..."
|
||||||
|
sudo systemctl daemon-reload
|
||||||
|
|
||||||
|
echo "Enabling ledmatrix-web.service to start on boot..."
|
||||||
|
sudo systemctl enable ledmatrix-web.service
|
||||||
|
|
||||||
|
echo "Starting ledmatrix-web.service..."
|
||||||
|
sudo systemctl start ledmatrix-web.service
|
||||||
|
|
||||||
|
echo "LEDMatrix Web Interface service (ledmatrix-web.service) installation complete."
|
||||||
|
echo "It will start based on the 'web_display_autostart' setting in config/config.json."
|
||||||
|
# === End of LEDMatrix Web Interface service ===
|
||||||
|
|
||||||
|
|
||||||
# Check the status
|
# Check the status
|
||||||
echo "Service status:"
|
echo "Service status for main display (ledmatrix.service):"
|
||||||
sudo systemctl status ledmatrix.service
|
sudo systemctl status ledmatrix.service || echo "ledmatrix.service not found or failed to get status."
|
||||||
|
echo "Service status for web interface (ledmatrix-web.service):"
|
||||||
|
sudo systemctl status ledmatrix-web.service || echo "ledmatrix-web.service not found or failed to get status."
|
||||||
|
|
||||||
echo ""
|
echo ""
|
||||||
echo "LED Matrix Display Service has been installed and started."
|
echo "LED Matrix Services have been processed."
|
||||||
echo ""
|
echo ""
|
||||||
echo "To stop the display when you SSH in:"
|
echo "To stop the main display when you SSH in:"
|
||||||
echo " sudo systemctl stop ledmatrix.service"
|
echo " sudo systemctl stop ledmatrix.service"
|
||||||
|
echo "To stop the web interface:"
|
||||||
|
echo " sudo systemctl stop ledmatrix-web.service"
|
||||||
|
|
||||||
echo ""
|
echo ""
|
||||||
echo "To check if the service is running:"
|
echo "To check if the main display service is running:"
|
||||||
echo " sudo systemctl status ledmatrix.service"
|
echo " sudo systemctl status ledmatrix.service"
|
||||||
|
echo "To check if the web interface service is running:"
|
||||||
|
echo " sudo systemctl status ledmatrix-web.service"
|
||||||
|
|
||||||
echo ""
|
echo ""
|
||||||
echo "To restart the service:"
|
echo "To restart the main display service:"
|
||||||
echo " sudo systemctl restart ledmatrix.service"
|
echo " sudo systemctl restart ledmatrix.service"
|
||||||
|
echo "To restart the web interface service:"
|
||||||
|
echo " sudo systemctl restart ledmatrix-web.service"
|
||||||
|
|
||||||
echo ""
|
echo ""
|
||||||
echo "To view logs:"
|
echo "To view logs for the main display:"
|
||||||
echo " journalctl -u ledmatrix.service"
|
echo " journalctl -u ledmatrix.service"
|
||||||
|
echo "To view logs for the web interface:"
|
||||||
|
echo " journalctl -u ledmatrix-web.service"
|
||||||
|
|
||||||
echo ""
|
echo ""
|
||||||
echo "To disable autostart:"
|
echo "To disable autostart for the main display:"
|
||||||
echo " sudo systemctl disable ledmatrix.service"
|
echo " sudo systemctl disable ledmatrix.service"
|
||||||
|
echo "To disable autostart for the web interface:"
|
||||||
|
echo " sudo systemctl disable ledmatrix-web.service"
|
||||||
40
start_web_conditionally.py
Normal file
40
start_web_conditionally.py
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
import json
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import subprocess
|
||||||
|
|
||||||
|
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||||
|
CONFIG_FILE = os.path.join(PROJECT_DIR, 'config', 'config.json')
|
||||||
|
WEB_INTERFACE_SCRIPT = os.path.join(PROJECT_DIR, 'web_interface.py')
|
||||||
|
PYTHON_EXEC = sys.executable # Use the same python that runs this script
|
||||||
|
|
||||||
|
def main():
|
||||||
|
try:
|
||||||
|
with open(CONFIG_FILE, 'r') as f:
|
||||||
|
config_data = json.load(f)
|
||||||
|
except FileNotFoundError:
|
||||||
|
print(f"Config file {CONFIG_FILE} not found. Web interface will not start.")
|
||||||
|
sys.exit(0) # Exit gracefully, don't start
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error reading config file {CONFIG_FILE}: {e}. Web interface will not start.")
|
||||||
|
sys.exit(1) # Exit with error, service might restart depending on config
|
||||||
|
|
||||||
|
autostart_enabled = config_data.get("web_display_autostart", False)
|
||||||
|
|
||||||
|
if autostart_enabled is True: # Explicitly check for True
|
||||||
|
print("Configuration 'web_display_autostart' is true. Starting web interface...")
|
||||||
|
try:
|
||||||
|
# Replace the current process with web_interface.py
|
||||||
|
# This is important for systemd to correctly manage the web server process.
|
||||||
|
# Ensure PYTHONPATH is set correctly if web_interface.py has relative imports to src
|
||||||
|
# The WorkingDirectory in systemd service should handle this for web_interface.py
|
||||||
|
os.execvp(PYTHON_EXEC, [PYTHON_EXEC, WEB_INTERFACE_SCRIPT])
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Failed to exec web interface: {e}")
|
||||||
|
sys.exit(1) # Failed to start
|
||||||
|
else:
|
||||||
|
print("Configuration 'web_display_autostart' is false or not set. Web interface will not be started.")
|
||||||
|
sys.exit(0) # Exit gracefully, service considered successful
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
Reference in New Issue
Block a user