MQTT_HOST=your-mqtt-server.lan MQTT_PORT=1883 MQTT_USER=username MQTT_PASSWORD=password MQTT_BASE_TOPIC=batocera/$(hostname) SERVICE_NAME=$(basename "$0" .${0##*.}) ES_EVENTS=(game-start game-end game-selected system-selected theme-changed settings-changed controls-changed config-changed quit reboot shutdown sleep wake achievements screensaver-start screensaver-stop) ############################################################################### # Publish an event message to the MQTT broker as JSON dictionary. # # The first argument is the subtopic to publish to (source/event_name) # The remaining arguments are converted to a list of strings, which # is stored in the 'data' field of the message # An ISO-formatted 'timestamp' field is automatically added to the message # # Only executes if this service is enabled ############################################################################### publish() { if [[ " $(/usr/bin/batocera-settings-get system.services) " == *" $SERVICE_NAME "* ]]; then subtopic="$1" shift # Initialize an empty JSON array data='[' # Iterate over the remaining arguments for arg in "$@"; do # Convert each argument to a JSON string and append it to the array data+="$(jq -Rn --arg arg "$arg" '$arg')," done # Remove the trailing comma and close the JSON array data="${data%,}]" mosquitto_pub -h "$MQTT_HOST" -p "$MQTT_PORT" -u "$MQTT_USER" -P "$MQTT_PASSWORD" -t "$MQTT_BASE_TOPIC/$subtopic" -m '{"data": '"$data"', "timestamp": "'"$(date --iso-8601=ns)"'"}' -r fi } ############################################################################### # Start the MQTT service. # - Installs any missing support files # - Publishes a system startup event ############################################################################### start() { install publish "system/startup" } ############################################################################### # Stop the MQTT service. # - Publishes a system shutdown event ############################################################################### stop() { publish "system/shutdown" } ############################################################################### # Create scripts that capture events and publish them to the MQTT broker. ############################################################################### install() { # EmulationStation events for event in ${ES_EVENTS[@]}; do script_dir="/userdata/system/configs/emulationstation/scripts/$event" mkdir -p "$script_dir" script="$script_dir/$SERVICE_NAME.sh" if [[ -f "$script" ]]; then echo "WARNING: Skipping installation of '$script', as it already exists" continue fi echo "Installing '$script'" cat <<-EOF > "$script" #!/bin/bash /userdata/system/services/$SERVICE_NAME publish "emulationstation/$event" "\$@" EOF chmod +x "$script" done # Game events script="/userdata/system/scripts/$SERVICE_NAME.sh" echo "Installing '$script'" cat <<-EOF > "$script" #!/bin/bash /userdata/system/services/$SERVICE_NAME publish "game/\$1" "\${@:2}" EOF } ############################################################################### # Delete scripts that capture events and publish them to the MQTT broker. ############################################################################### uninstall() { # Delete EmulationStation event scripts, and remove event directories if empty for event in ${ES_EVENTS[@]}; do script_dir="/userdata/system/configs/emulationstation/scripts/$event" script="$script_dir/$SERVICE_NAME.sh" echo "Deleting '$script'" rm "$script" if [ -z "$(ls -A "$script_dir")" ]; then echo "Deleting empty directory '$script_dir'" rm -rf "$script_dir" fi done # Delete Game event script script="/userdata/system/scripts/$SERVICE_NAME.sh" echo "Deleting '$script'" rm "$script" } ############################################################################### # Script entrypoint ############################################################################### if [ $# -eq 0 ]; then echo "ERROR: No arguments provided" echo "Usage: $SERVICE_NAME publish|start|stop|install|uninstall [args]" exit 1 fi if [[ $(type -t "$1") == function ]]; then FUNCTION="$1" shift $FUNCTION "$@" else echo "ERROR: '$1' is not defined" echo "Usage: $SERVICE_NAME publish|start|stop|install|uninstall [args]" exit 1 fi