78 lines
1.9 KiB
Plaintext
78 lines
1.9 KiB
Plaintext
#!/bin/ash
|
|
# shellcheck shell=ash
|
|
|
|
# -- Load Configs ---
|
|
. /recalbox/share/system/configs/savesync/savesync.conf
|
|
|
|
# --- Logger Function ---
|
|
log() {
|
|
local level="$1" local msg="$2"
|
|
local timestamp
|
|
|
|
timestamp=$(date '+%Y-%m-%d %H:%M:%S')
|
|
local log_line="[$timestamp] [$level] $msg"
|
|
|
|
# Handle local emergency error log immediately
|
|
if [ "$level" = "ERROR" ]; then
|
|
mkdir -p "$(dirname "$ERROR_LOG")"
|
|
printf "%s\n" "$log_line" >> "$ERROR_LOG"
|
|
fi
|
|
|
|
# Dispatch to the Central Logging Daemon via MQTT
|
|
# We use -q 0 (fire and forget) so the game script doesn't wait
|
|
mosquitto_pub -h 127.0.0.1 -t "$LOG_TOPIC" -m "SaveLog=$log_line" 2>/dev/null
|
|
|
|
# Local debugging
|
|
[ "${DEBUG_MODE:-0}" -eq 1 ] && printf "%s\n" "$log_line"
|
|
}
|
|
|
|
# --- Exit function ---
|
|
call_exit() {
|
|
value="$1"
|
|
if [ "$value" -eq 0 ]; then
|
|
log "WARN" "pub_event exited normally"
|
|
else
|
|
log "INFO" "pub_event exited with warnings"
|
|
fi
|
|
exit "$value"
|
|
}
|
|
|
|
eventfile="/tmp/es_state.inf"
|
|
|
|
# 1. Check if the file exists and is not empty
|
|
if [ ! -s "$eventfile" ]; then
|
|
log "ERROR" "$eventfile is missing or empty."
|
|
exit 1
|
|
fi
|
|
|
|
# 2. Read the payload safely
|
|
PAYLOAD=$(cat "$eventfile")
|
|
|
|
# 3. Wait for connection to rclone endpoint
|
|
connect_success=0
|
|
for i in $(seq 5); do
|
|
log "INFO" "Waiting on rclone endpoint... $i"
|
|
[ "$(nc -z "$RCLONE_ENDPOINT" "$RCLONE_PORT")" ] || connect_success=1
|
|
[ "$connect_success" ] && break
|
|
sleep 0.5
|
|
done
|
|
|
|
[ "$connect_success" ] || ( log "ERROR" "Remote repository not accessable" && exit 1 )
|
|
|
|
# 4. Publish and log success/failure
|
|
if [ "$(mosquitto_pub -h 127.0.0.1 -p 1883 -t "$TOPIC" "$PAYLOAD")" ]; then
|
|
log "ERROR" "Failed to connect to mosquitto broker."
|
|
exit 1
|
|
else
|
|
log "INFO" "Successfully published event."
|
|
fi
|
|
|
|
# 5. Wait for response from daemon
|
|
mosquitto_sub -h 127.0.0.1 -p 1883 -q 0 -t "$TOPIC" | while IFS="=" read -r key value
|
|
do
|
|
case "$key" in
|
|
"SaveSync") call_exit "$value";;
|
|
esac
|
|
done
|
|
|