system2mqtt/run.sh

73 lines
1.4 KiB
Bash
Executable File

#!/bin/bash
# Virtual environment directory
VENV_DIR=".venv"
# Function to show usage
show_usage() {
echo "Usage: $0 [start|stop|cleanup]"
echo
echo "Commands:"
echo " start - Start the system2mqtt service"
echo " stop - Stop the system2mqtt service"
echo " cleanup - Clean up all system2mqtt MQTT topics"
echo
exit 1
}
# Function to setup virtual environment
setup_venv() {
# Create virtual environment if it doesn't exist
if [ ! -d "$VENV_DIR" ]; then
echo "Creating virtual environment..."
python3 -m venv "$VENV_DIR"
fi
# Activate virtual environment
source "$VENV_DIR/bin/activate"
# Install/update dependencies
echo "Installing/updating dependencies..."
pip install -r requirements.txt
}
# Function to start the service
start_service() {
echo "Starting system2mqtt..."
setup_venv
python3 main.py
}
# Function to stop the service
stop_service() {
echo "Stopping system2mqtt..."
pkill -f "python3 main.py"
}
# Function to cleanup MQTT topics
cleanup_topics() {
echo "Running MQTT cleanup..."
setup_venv
python3 cleanup_mqtt.py
}
# Check if a command was provided
if [ $# -eq 0 ]; then
show_usage
fi
# Process command
case "$1" in
start)
start_service
;;
stop)
stop_service
;;
cleanup)
cleanup_topics
;;
*)
show_usage
;;
esac