Formatting
This commit is contained in:
@@ -6,25 +6,25 @@
|
|||||||
|
|
||||||
# --- Logger Function ---
|
# --- Logger Function ---
|
||||||
log() {
|
log() {
|
||||||
local level="$1"
|
local level="$1"
|
||||||
local msg="$2"
|
local msg="$2"
|
||||||
local timestamp
|
local timestamp
|
||||||
|
|
||||||
timestamp=$(date '+%Y-%m-%d %H:%M:%S')
|
timestamp=$(date '+%Y-%m-%d %H:%M:%S')
|
||||||
local log_line="[$timestamp] [$level] $msg"
|
local log_line="[$timestamp] [$level] $msg"
|
||||||
|
|
||||||
# Handle local emergency error log immediately
|
# Handle local emergency error log immediately
|
||||||
if [ "$level" = "ERROR" ]; then
|
if [ "$level" = "ERROR" ]; then
|
||||||
mkdir -p "$(dirname "$ERROR_LOG")"
|
mkdir -p "$(dirname "$ERROR_LOG")"
|
||||||
printf "%s\n" "$log_line" >> "$ERROR_LOG"
|
printf "%s\n" "$log_line" >>"$ERROR_LOG"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Dispatch to the Central Logging Daemon via MQTT
|
# Dispatch to the Central Logging Daemon via MQTT
|
||||||
# We use -q 0 (fire and forget) so the game script doesn't wait
|
# We use -q 0 (fire and forget) so the game script doesn't wait
|
||||||
mosquitto_pub -h 127.0.0.1 -p 1833 -q 0 -t "$LOG_TOPIC" -m "SaveLog=$log_line" 2>/dev/null
|
mosquitto_pub -h 127.0.0.1 -p 1833 -q 0 -t "$LOG_TOPIC" -m "SaveLog=$log_line" 2>/dev/null
|
||||||
|
|
||||||
# Local debugging
|
# Local debugging
|
||||||
[ "${DEBUG_MODE:-0}" -eq 1 ] && printf "%s\n" "$log_line"
|
[ "${DEBUG_MODE:-0}" -eq 1 ] && printf "%s\n" "$log_line"
|
||||||
}
|
}
|
||||||
|
|
||||||
# --- Exit function ---
|
# --- Exit function ---
|
||||||
@@ -42,8 +42,8 @@ eventfile="/tmp/es_state.inf"
|
|||||||
|
|
||||||
# 1. Check if the file exists and is not empty
|
# 1. Check if the file exists and is not empty
|
||||||
if [ ! -s "$eventfile" ]; then
|
if [ ! -s "$eventfile" ]; then
|
||||||
log "ERROR" "$eventfile is missing or empty."
|
log "ERROR" "$eventfile is missing or empty."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# 2. Read the payload safely
|
# 2. Read the payload safely
|
||||||
@@ -51,46 +51,47 @@ PAYLOAD=$(cat "$eventfile")
|
|||||||
|
|
||||||
# Step 3: Connection Check
|
# Step 3: Connection Check
|
||||||
for i in $(seq 5); do
|
for i in $(seq 5); do
|
||||||
log "INFO" "Waiting on rclone endpoint... $i"
|
log "INFO" "Waiting on rclone endpoint... $i"
|
||||||
if nc -z "$RCLONE_ENDPOINT" "$RCLONE_PORT"; then
|
if nc -z "$RCLONE_ENDPOINT" "$RCLONE_PORT"; then
|
||||||
connect_success=1
|
connect_success=1
|
||||||
break
|
break
|
||||||
fi
|
fi
|
||||||
sleep 0.5
|
sleep 0.5
|
||||||
done
|
done
|
||||||
|
|
||||||
# Step 4: Publish
|
# Step 4: Publish
|
||||||
# Added -m flag and removed the [ "$( ... )" ] wrapper
|
# Added -m flag and removed the [ "$( ... )" ] wrapper
|
||||||
if mosquitto_pub -h 127.0.0.1 -p 1883 -t "$TOPIC" -m "$PAYLOAD"; then
|
if mosquitto_pub -h 127.0.0.1 -p 1883 -t "$TOPIC" -m "$PAYLOAD"; then
|
||||||
log "INFO" "Successfully published event."
|
log "INFO" "Successfully published event."
|
||||||
else
|
else
|
||||||
log "ERROR" "Failed to connect to mosquitto broker."
|
log "ERROR" "Failed to connect to mosquitto broker."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# 5. Wait for response from daemon
|
# 5. Wait for response from daemon
|
||||||
log "INFO" "Waiting for sync confirmation on $RESPONSE_TOPIC..."
|
log "INFO" "Waiting for sync confirmation on $RESPONSE_TOPIC..."
|
||||||
|
|
||||||
# Start a subshell that kills the subscriber after 10 seconds if no message arrives
|
# Start a subshell that kills the subscriber after 10 seconds if no message arrives
|
||||||
( sleep 10; mosquitto_pub -h 127.0.0.1 -t "$RESPONSE_TOPIC" -m "SaveSync=timeout" ) &
|
(
|
||||||
|
sleep 10
|
||||||
|
mosquitto_pub -h 127.0.0.1 -t "$RESPONSE_TOPIC" -m "SaveSync=timeout"
|
||||||
|
) &
|
||||||
TIMEOUT_PID=$!
|
TIMEOUT_PID=$!
|
||||||
|
|
||||||
# -C 1 ensures we exit after receiving either the real response or the timeout message
|
# -C 1 ensures we exit after receiving either the real response or the timeout message
|
||||||
mosquitto_sub -h 127.0.0.1 -p 1883 -t "$RESPONSE_TOPIC" -C 1 | while IFS="=" read -r key value
|
mosquitto_sub -h 127.0.0.1 -p 1883 -t "$RESPONSE_TOPIC" -C 1 | while IFS="=" read -r key value; do
|
||||||
do
|
# Kill the background sleep timer since we got a message
|
||||||
# Kill the background sleep timer since we got a message
|
kill "$TIMEOUT_PID" 2>/dev/null
|
||||||
kill "$TIMEOUT_PID" 2>/dev/null
|
|
||||||
|
|
||||||
case "$key" in
|
case "$key" in
|
||||||
"SaveSync")
|
"SaveSync")
|
||||||
if [ "$value" = "timeout" ]; then
|
if [ "$value" = "timeout" ]; then
|
||||||
log "ERROR" "Sync timed out! Daemon might be down."
|
log "ERROR" "Sync timed out! Daemon might be down."
|
||||||
call_exit 1
|
call_exit 1
|
||||||
else
|
else
|
||||||
log "INFO" "Sync confirmed with status: $value"
|
log "INFO" "Sync confirmed with status: $value"
|
||||||
call_exit "$value"
|
call_exit "$value"
|
||||||
fi
|
fi
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|
||||||
|
|||||||
@@ -7,17 +7,16 @@ touch "$LOG_FILE"
|
|||||||
touch "$ERROR_FILE"
|
touch "$ERROR_FILE"
|
||||||
|
|
||||||
log_to_file() {
|
log_to_file() {
|
||||||
printf "%s\n" "$1" >> "$LOG_FILE"
|
printf "%s\n" "$1" >>"$LOG_FILE"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Subscribe and wait for log entries
|
# Subscribe and wait for log entries
|
||||||
mosquitto_sub -h 127.0.0.1 -t "$LOG_TOPIC" | while read -r line
|
mosquitto_sub -h 127.0.0.1 -t "$LOG_TOPIC" | while read -r line; do
|
||||||
do
|
# The line will look like: SaveLog=[2026-...] [INFO] ...
|
||||||
# The line will look like: SaveLog=[2026-...] [INFO] ...
|
# We strip the "SaveLog=" prefix
|
||||||
# We strip the "SaveLog=" prefix
|
msg_content="${line#SaveLog=}"
|
||||||
msg_content="${line#SaveLog=}"
|
|
||||||
|
|
||||||
if [ -n "$msg_content" ]; then
|
if [ -n "$msg_content" ]; then
|
||||||
log_to_file "$msg_content"
|
log_to_file "$msg_content"
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|||||||
@@ -5,82 +5,85 @@
|
|||||||
. /recalbox/share/system/configs/savesync/savesync.conf
|
. /recalbox/share/system/configs/savesync/savesync.conf
|
||||||
|
|
||||||
log() {
|
log() {
|
||||||
local level="$1"
|
local level="$1"
|
||||||
local msg="$2"
|
local msg="$2"
|
||||||
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
|
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
|
||||||
local log_line="[$timestamp] [$level] $msg"
|
local log_line="[$timestamp] [$level] $msg"
|
||||||
|
|
||||||
if [ "$level" = "ERROR" ]; then
|
if [ "$level" = "ERROR" ]; then
|
||||||
mkdir -p "$(dirname "$ERROR_LOG")"
|
mkdir -p "$(dirname "$ERROR_LOG")"
|
||||||
printf "%s\n" "$log_line" >> "$ERROR_LOG"
|
printf "%s\n" "$log_line" >>"$ERROR_LOG"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# FIX: Corrected port to 1883 and added -m
|
# FIX: Corrected port to 1883 and added -m
|
||||||
mosquitto_pub -h 127.0.0.1 -p 1883 -q 0 -t "$LOG_TOPIC" -m "SaveLog=$log_line" 2>/dev/null
|
mosquitto_pub -h 127.0.0.1 -p 1883 -q 0 -t "$LOG_TOPIC" -m "SaveLog=$log_line" 2>/dev/null
|
||||||
|
|
||||||
[ "${DEBUG_MODE:-0}" -eq 1 ] && printf "%s\n" "$log_line"
|
[ "${DEBUG_MODE:-0}" -eq 1 ] && printf "%s\n" "$log_line"
|
||||||
}
|
}
|
||||||
|
|
||||||
sleep 2
|
sleep 2
|
||||||
log "INFO" "--- ES Event Daemon Started ---"
|
log "INFO" "--- ES Event Daemon Started ---"
|
||||||
|
|
||||||
# --- Main Listener Loop ---
|
# --- Main Listener Loop ---
|
||||||
mosquitto_sub -h 127.0.0.1 -p 1883 -q 0 -t "$TOPIC" | while IFS="=" read -r key value
|
mosquitto_sub -h 127.0.0.1 -p 1883 -q 0 -t "$TOPIC" | while IFS="=" read -r key value; do
|
||||||
do
|
# 1. Clean input
|
||||||
# 1. Clean input
|
value=$(echo "$value" | tr -d '\r')
|
||||||
value=$(echo "$value" | tr -d '\r')
|
|
||||||
|
|
||||||
case "$key" in
|
case "$key" in
|
||||||
"SystemId") this_system_id="$value" ;;
|
"SystemId") this_system_id="$value" ;;
|
||||||
"GamePath")
|
"GamePath")
|
||||||
this_game_path="$value"
|
this_game_path="$value"
|
||||||
# Corrected logic for paths
|
# Corrected logic for paths
|
||||||
this_save_path=$(echo "$value" | sed 's|roms|saves|; s|\.[^.]*$|.srm|')
|
this_save_path=$(echo "$value" | sed 's|roms|saves|; s|\.[^.]*$|.srm|')
|
||||||
this_backup_path=$(echo "$this_save_path" | sed 's|saves|archives|')
|
this_backup_path=$(echo "$this_save_path" | sed 's|saves|archives|')
|
||||||
;;
|
;;
|
||||||
"Action") this_action="$value" ;;
|
"Action") this_action="$value" ;;
|
||||||
"State")
|
"State")
|
||||||
this_state="$value"
|
this_state="$value"
|
||||||
|
|
||||||
if [ "$this_state" = "playing" ] && [ "$this_action" = "rungame" ]; then
|
if [ "$this_state" = "playing" ] && [ "$this_action" = "rungame" ]; then
|
||||||
# We wrap the whole operation in ( ) & to background the entire sequence
|
# We wrap the whole operation in ( ) & to background the entire sequence
|
||||||
(
|
(
|
||||||
log "INFO" "Game Started: $(basename "$this_game_path")"
|
log "INFO" "Game Started: $(basename "$this_game_path")"
|
||||||
|
|
||||||
filename=$(basename "$this_save_path")
|
filename=$(basename "$this_save_path")
|
||||||
remote_full="$REMOTE_BASE/$this_system_id/$filename"
|
remote_full="$REMOTE_BASE/$this_system_id/$filename"
|
||||||
|
|
||||||
local_size=$(stat -c %s "$this_save_path" 2>/dev/null || echo 0)
|
local_size=$(stat -c %s "$this_save_path" 2>/dev/null || echo 0)
|
||||||
remote_size=$(rclone lsjson "$remote_full" 2>/dev/null | grep -o '"Size":[0-9]*' | cut -d: -f2)
|
remote_size=$(rclone lsjson "$remote_full" 2>/dev/null | grep -o '"Size":[0-9]*' | cut -d: -f2)
|
||||||
: "${remote_size:=0}"
|
: "${remote_size:=0}"
|
||||||
|
|
||||||
if [ "$local_size" -lt "$remote_size" ]; then
|
if [ "$local_size" -lt "$remote_size" ]; then
|
||||||
log "WARN" "Cloud save LARGER. Restoring..."
|
log "WARN" "Cloud save LARGER. Restoring..."
|
||||||
mkdir -p "$(dirname "$this_backup_path")"
|
mkdir -p "$(dirname "$this_backup_path")"
|
||||||
rclone copyto "$remote_full" "$this_save_path" --backup-dir "$(dirname "$this_backup_path")"
|
rclone copyto "$remote_full" "$this_save_path" --backup-dir "$(dirname "$this_backup_path")"
|
||||||
else
|
else
|
||||||
log "INFO" "Local save safe. Updating..."
|
log "INFO" "Local save safe. Updating..."
|
||||||
rclone update "$remote_full" "$this_save_path"
|
rclone update "$remote_full" "$this_save_path"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Signal that sync is done
|
# Signal that sync is done
|
||||||
mosquitto_pub -h 127.0.0.1 -p 1883 -t "$TOPIC" -m "SaveContinue=0"
|
mosquitto_pub -h 127.0.0.1 -p 1883 -t "$TOPIC" -m "SaveContinue=0"
|
||||||
) &
|
) &
|
||||||
log "DEBUG" "Sync process backgrounded for Start Game."
|
log "DEBUG" "Sync process backgrounded for Start Game."
|
||||||
|
|
||||||
elif [ "$this_state" = "endgame" ]; then
|
elif [ "$this_state" = "endgame" ]; then
|
||||||
(
|
(
|
||||||
log "INFO" "Game Ended. Syncing..."
|
log "INFO" "Game Ended. Syncing..."
|
||||||
rclone update "$this_save_path" "$REMOTE_BASE/$this_system_id/"
|
rclone update "$this_save_path" "$REMOTE_BASE/$this_system_id/"
|
||||||
log "INFO" "Sync Complete."
|
log "INFO" "Sync Complete."
|
||||||
|
|
||||||
mosquitto_pub -h 127.0.0.1 -p 1883 -t "$TOPIC" -m "SaveContinue=0"
|
mosquitto_pub -h 127.0.0.1 -p 1883 -t "$TOPIC" -m "SaveContinue=0"
|
||||||
) &
|
) &
|
||||||
log "DEBUG" "Sync process backgrounded for End Game."
|
log "DEBUG" "Sync process backgrounded for End Game."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Reset variables immediately so the loop is ready for the next line
|
# Reset variables immediately so the loop is ready for the next line
|
||||||
this_system_id=""; this_game_path=""; this_save_path=""; this_action=""; this_backup_path=""
|
this_system_id=""
|
||||||
;;
|
this_game_path=""
|
||||||
esac
|
this_save_path=""
|
||||||
|
this_action=""
|
||||||
|
this_backup_path=""
|
||||||
|
;;
|
||||||
|
esac
|
||||||
done
|
done
|
||||||
|
|||||||
Reference in New Issue
Block a user