diff --git a/install.sh b/install.sh index 169c290..bbe6f19 100644 --- a/install.sh +++ b/install.sh @@ -10,3 +10,8 @@ cd recalbox-savesync cp *.ash /recalbox/share/userscripts/ mkdir -p /recalbox/share/system/config/savesync cp *.conf /recalbox/share/system/config/savesync/ + +mount -o remount,rw / +curl -L -o /usr/bin/nc https://github.com/therealsaumil/static-arm-bins/raw/refs/heads/master/nc-arm-static +chmod 755 /usr/bin/nc +mount -o remount,ro diff --git a/pub_event[rungame,endgame](sync).ash b/pub_event[rungame,endgame](sync).ash index 041cb00..93118d7 100644 --- a/pub_event[rungame,endgame](sync).ash +++ b/pub_event[rungame,endgame](sync).ash @@ -1,14 +1,15 @@ #!/bin/ash # shellcheck shell=ash -# -ERROR_LOG="/recalbox/share/system/logs/savesync-error.log" + +# -- Load Configs --- +. /recalbox/share/system/config/savesync/savesync.conf # --- Logger Function --- log() { local timestamp local level="$1" local msg="$2" - mkdir -p "$(dirname \"$ERROR_LOG\")" + mkdir -p "$(dirname "$ERROR_LOG")" timestamp=$(date '+%Y-%m-%d %H:%M:%S') local log_line="[$timestamp] [$level] $msg" @@ -16,7 +17,7 @@ log() { printf "%s\n" "$log_line" >> "$ERROR_LOG" fi - mosquitto_pub -h 127.0.0.1 -p 1883 -t /Recalbox/EmulationStation/Event "SaveLog=${log_line}" + mosquitto_pub -h 127.0.0.1 -p 1883 -t "$TOPIC" "SaveLog=${log_line}" # printf "%s\n" "$log_line" >> "$LOG_FILE" 2>/dev/null [ "$DEBUG_MODE" -eq 1 ] && printf "%s\n" "$log_line" @@ -33,20 +34,31 @@ fi # 2. Read the payload safely PAYLOAD=$(cat "$eventfile") -# 3. Publish (using double quotes to handle spaces/newlines in the file) -mosquitto_pub -h 127.0.0.1 -p 1883 -t /Recalbox/EmulationStation/Event "$PAYLOAD" +connect_success=0 +for i in $(seq 10); do + if [ nc -z "$RCLONE_ENDPOINT" "$RCLONE_PORT" -eq 0 ] { + connect_success=1 + break + fi -# 4. Optional: check if the publish succeeded -if [ $? -eq 0 ]; then +# 3. Publish and log success/failure +if [ mosquitto_pub -h 127.0.0.1 -p 1883 -t "$TOPIC" "$PAYLOAD" -eq 0 ]; then log "INFO" "Successfully published event." else log "ERROR" "Failed to connect to mosquitto broker." exit 1 fi -mosquitto_sub -h 127.0.0.1 -p 1883 -t /Recalbox/EmulationStation/Event | while IFS="=" read -r key value +# 4. Wait for response from daemon +mosquitto_sub -h 127.0.0.1 -p 1883 -t "$TOPIC" | while IFS="=" read -r key value do case "$key" in - "SaveSync") exit "$value" ;; + "SaveSync") break;; esac done + +if [ "$value" -eq 0 ]; then + log "WARN" "pub_event exited normally" +else + log "INFO" "pub_event exited with warnings" +fi diff --git a/savesync-logger[start](permanent).ash b/savesync-logger[start](permanent).ash new file mode 100644 index 0000000..463355b --- /dev/null +++ b/savesync-logger[start](permanent).ash @@ -0,0 +1,11 @@ +#!/bin/ash +#shellcheck shell=dash +# +. /recalbox/share/system/configs/savesync/savesync.conf + +mosquitto_sub -h 127.0.0.1 -p 1883 -t "$TOPIC" | while IFS="=" read -r key value +do + case "$key" in + "SaveLog") printf "%s\n" "$value" >> "$LOG_FILE" ;; + esac +done diff --git a/savesync.conf b/savesync.conf index 091b38a..bdc053d 100644 --- a/savesync.conf +++ b/savesync.conf @@ -1,3 +1,5 @@ -LOG_FILE="/recalbox/share/system/logs/savesync_monitor.log" -DEBUG_MODE=1 -REMOTE_BASE="saves:gamepi-tv" +export LOG_FILE="/recalbox/share/system/logs/savesync_monitor.log" +export ERROR_FILE="/recalbox/share/system/logs/savesync_error.log" +export DEBUG_MODE=1 +export REMOTE_BASE="saves:gamepi-tv" +export TOPIC="/Recalbox/SaveSync/Event" diff --git a/savesync[start](permanent).ash b/savesync[start](permanent).ash index 1bb93b0..78c349a 100644 --- a/savesync[start](permanent).ash +++ b/savesync[start](permanent).ash @@ -5,14 +5,6 @@ . /recalbox/share/system/config/savesync/savesync.conf -# --- MQTT Publish Function --- -mqtt_publish() { - - local msg="$1" - -} - - # --- Logger Function --- log() { local timestamp @@ -22,20 +14,24 @@ log() { timestamp=$(date '+%Y-%m-%d %H:%M:%S') local log_line="[$timestamp] [$level] $msg" - mosquitto_pub -h 127.0.0.1 -p 1883 -t /Recalbox/EmulationStation/Event "SaveLog=${log_line}" + mosquitto_pub -h 127.0.0.1 -p 1883 -t "$TOPIC" "SaveLog=${log_line}" # printf "%s\n" "$log_line" >> "$LOG_FILE" 2>/dev/null [ "$DEBUG_MODE" -eq 1 ] && printf "%s\n" "$log_line" } +# --- Continue message function --- send_continue() { - mosquitto_pub -h 127.0.0.1 -p 1883 -t /Recalbox/EmulationStation/Event "SaveContinue=1" } +# --- Sleep to ensure that the logger daemon has started --- +# + + log "INFO" "--- ES Event Daemon Started ---" # --- Main Listener Loop --- -mosquitto_sub -h 127.0.0.1 -p 1883 -t /Recalbox/EmulationStation/Event | while IFS="=" read -r key value +mosquitto_sub -h 127.0.0.1 -p 1883 -t "$TOPIC" | while IFS="=" read -r key value do # 1. Clean Carriage Returns from Windows-style line endings value=$(echo "$value" | tr -d '\r') @@ -74,6 +70,7 @@ do rclone update "$remote_full" "$this_save_path" fi + mosquitto_pub -h 127.0.0.1 -p 1883 -t "$TOPIC" "SaveContinue=0" elif [ "$this_state" = "endgame" ]; then log "INFO" "Game Ended. Backing up save..." filename=$(basename "$this_save_path") @@ -82,6 +79,8 @@ do # Push the local save to the cloud if it's newer rclone update "$this_save_path" "$REMOTE_BASE/$this_system_id/" log "INFO" "Sync Complete." + + mosquitto_pub -h 127.0.0.1 -p 1883 -t "$TOPIC" "SaveContinue=0" fi # Reset variables for the next event block