Files
recalbox-savesync/pub_event[rungame,endgame](sync).ash
2026-02-27 21:19:38 -06:00

49 lines
1.1 KiB
Plaintext

#!/bin/ash
# shellcheck shell=dash
. /recalbox/share/system/configs/savesync/savesync.conf
# Constants
EVENT_FILE="/tmp/es_state.inf"
TIMEOUT_SEC=10
log() {
mosquitto_pub -h 127.0.0.1 -p 1883 -q 0 -t "$LOG_TOPIC" -m "SaveLog=[$(date '+%H:%M:%S')] [PUB] $1" 2>/dev/null
}
# 1. Validation
[ ! -s "$EVENT_FILE" ] && exit 0
PAYLOAD=$(cat "$EVENT_FILE")
# 2. Network Check (Short-circuit exit)
nc -z -w 1 "$RCLONE_ENDPOINT" "$RCLONE_PORT" || {
log "Offline: Skipping sync"
exit 0
}
# 3. Publish Event
mosquitto_pub -h 127.0.0.1 -p 1883 -t "$TOPIC" -m "$PAYLOAD" || {
log "MQTT Fail"
exit 1
}
# 4. Wait for Response (Crucial: Using -C 1 and the same Response Key)
(
sleep "$TIMEOUT_SEC"
mosquitto_pub -h 127.0.0.1 -t "$RESPONSE_TOPIC" -m "SaveSync=timeout"
) &
TIMER_PID=$!
log "Waiting for Daemon response..."
# Listen for the specific "SaveSync" key
RESPONSE=$(mosquitto_sub -h 127.0.0.1 -p 1883 -t "$RESPONSE_TOPIC" -C 1)
kill "$TIMER_PID" 2>/dev/null
case "$RESPONSE" in
*"timeout"*) log "WARN: Daemon timed out. Starting game anyway." ;;
*"0"*) log "INFO: Sync Complete. Launching game." ;;
*) log "INFO: Received response: $RESPONSE" ;;
esac
exit 0