diff --git a/ipk-source/sdxpinn-quecmanager-beta/root/www/cgi-bin/home/fetch_signal_metrics.sh b/ipk-source/sdxpinn-quecmanager-beta/root/www/cgi-bin/home/fetch_signal_metrics.sh index 7e7b419..17ac893 100755 --- a/ipk-source/sdxpinn-quecmanager-beta/root/www/cgi-bin/home/fetch_signal_metrics.sh +++ b/ipk-source/sdxpinn-quecmanager-beta/root/www/cgi-bin/home/fetch_signal_metrics.sh @@ -5,7 +5,7 @@ echo "Content-Type: application/json" echo "" # Directory where JSON files are stored (adjust as needed) -JSON_DIR="/tmp/signal_graphs/" +JSON_DIR="/www/signal_graphs/" # Function to safely read JSON file read_json_file() { diff --git a/ipk-source/sdxpinn-quecmanager-beta/root/www/cgi-bin/home/log_signal_metrics.sh b/ipk-source/sdxpinn-quecmanager-beta/root/www/cgi-bin/home/log_signal_metrics.sh index 0eb8da1..12578cf 100755 --- a/ipk-source/sdxpinn-quecmanager-beta/root/www/cgi-bin/home/log_signal_metrics.sh +++ b/ipk-source/sdxpinn-quecmanager-beta/root/www/cgi-bin/home/log_signal_metrics.sh @@ -1,62 +1,104 @@ #!/bin/sh +# Configuration +LOGDIR="/www/signal_graphs" +MAX_ENTRIES=10 +INTERVAL=15 +QSCAN_FILE="$LOGDIR/qscan.json" +LOCK_FILE="/tmp/signal_logging.lock" +PAUSE_FILE="/tmp/signal_logging.pause" + # Ensure the directory exists -LOGDIR="/tmp/signal_graphs" mkdir -p "$LOGDIR" -# Maximum number of entries -MAX_ENTRIES=10 - -# Interval between logs (in seconds) -INTERVAL=15 - -# Function to clean and extract actual output from atinout +# Modified clean_atinout_output function - less aggressive cleaning clean_atinout_output() { - # Remove first line (echoed command), last line (OK), and trim whitespace - sed -n '2,/^OK$/p' | sed '$d' | tr -d '\r' | xargs + # Keep everything between the command and OK, including the actual response + sed '1d' | sed '/^OK$/d' | tr -d '\r' | grep -v '^$' | head -n1 } +# Function to perform cell scan and output JSON response for CGI +perform_cell_scan() { + # Print CGI headers first + printf "Content-Type: application/json\n\n" + + # Create pause file to stop continuous logging + touch "$PAUSE_FILE" + + # Wait for any ongoing logging to complete + sleep 2 + + # Perform cell scan sequence + echo "AT+COPS=2" | atinout - /dev/smd7 - + sleep 2 + + # Run QSCAN and save output to temporary file + echo "AT+QSCAN=3,1" | atinout - /dev/smd7 "$QSCAN_OUT" + sleep 2 + + # Process QSCAN output and convert to JSON + if [ -f "$QSCAN_OUT" ]; then + # Extract the relevant part and convert to JSON format + sed -n '2,/^OK$/p' < "$QSCAN_OUT" | sed '$d' | tr -d '\r' | \ + jq -R -s 'split("\n") | map(select(length > 0))' > "$QSCAN_FILE" + fi + + # Re-enable network registration + echo "AT+COPS=0" | atinout - /dev/smd7 - + sleep 2 + + # Clean up temporary file + rm -f "$QSCAN_OUT" + + # Remove pause file to resume logging + rm -f "$PAUSE_FILE" + + # Return QSCAN results as JSON + if [ -f "$QSCAN_FILE" ]; then + printf '{"status":"success","data":%s}\n' "$(cat "$QSCAN_FILE")" + else + printf '{"status":"error","message":"No scan results available"}\n' + fi +} + + # Function to log signal metric log_signal_metric() { + [ -f "$PAUSE_FILE" ] && return + local COMMAND="$1" local FILENAME="$2" local LOGFILE="$LOGDIR/$FILENAME" - + # Ensure log directory exists mkdir -p "$(dirname "$LOGFILE")" - + # Get current timestamp TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S") - + + # Add debug logging + logger -t signal_metrics "Running command: $COMMAND" + # Run the AT command and capture its output, then clean it SIGNAL_OUTPUT=$(echo "$COMMAND" | atinout - /dev/smd7 - | clean_atinout_output) - + + # Log the raw output for debugging + logger -t signal_metrics "Raw output for $COMMAND: $SIGNAL_OUTPUT" + # Ensure the file exists and is a valid JSON array - if [ ! -s "$LOGFILE" ]; then - echo "[]" > "$LOGFILE" - fi - - # Prepare new JSON entry - ESCAPED_TIMESTAMP=$(printf '%s' "$TIMESTAMP" | sed 's/"/\\"/g') - ESCAPED_OUTPUT=$(printf '%s' "$SIGNAL_OUTPUT" | sed 's/"/\\"/g') - - # Use jq with a more robust approach - jq --arg datetime "$ESCAPED_TIMESTAMP" \ - --arg output "$ESCAPED_OUTPUT" \ - ' - # Ensure the input is always an array - if type == "array" then . - else [] - end | - # Add new entry - . + [{"datetime": $datetime, "output": $output}] | - # Trim to max entries - .[-'"$MAX_ENTRIES"':] - ' "$LOGFILE" > "${LOGFILE}.tmp" && mv "${LOGFILE}.tmp" "$LOGFILE" + [ ! -s "$LOGFILE" ] && echo "[]" > "$LOGFILE" + + # Use jq to update the JSON file + jq --arg dt "$TIMESTAMP" \ + --arg out "$SIGNAL_OUTPUT" \ + '. + [{"datetime": $dt, "output": $out}] | .[-'"$MAX_ENTRIES"':]' \ + "$LOGFILE" > "${LOGFILE}.tmp" && mv "${LOGFILE}.tmp" "$LOGFILE" } # Function to log data usage log_data_usage() { + [ -f "$PAUSE_FILE" ] && return + local LOGFILE="$LOGDIR/data_usage.json" # Ensure log directory exists @@ -65,51 +107,72 @@ log_data_usage() { # Get current timestamp TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S") - # Run the AT command and capture its output, then clean it + # Run the AT command and capture its output DATA_OUTPUT=$(echo "AT+QGDCNT?;+QGDNRCNT?" | atinout - /dev/smd7 - | clean_atinout_output) # Ensure the file exists and is a valid JSON array - if [ ! -s "$LOGFILE" ]; then - echo "[]" > "$LOGFILE" + [ ! -s "$LOGFILE" ] && echo "[]" > "$LOGFILE" + + # Use jq to update the JSON file + jq --arg dt "$TIMESTAMP" \ + --arg out "$DATA_OUTPUT" \ + '. + [{"datetime": $dt, "output": $out}] | .[-'"$MAX_ENTRIES"':]' \ + "$LOGFILE" > "${LOGFILE}.tmp" && mv "${LOGFILE}.tmp" "$LOGFILE" +} + +# Main CGI request handler +handle_cgi_request() { + # Get query string from REQUEST_URI or QUERY_STRING + local QUERY="" + if [ -n "$REQUEST_URI" ]; then + QUERY=$(echo "$REQUEST_URI" | grep -o '[?&]request=[^&]*' | cut -d= -f2) + elif [ -n "$QUERY_STRING" ]; then + QUERY=$(echo "$QUERY_STRING" | grep -o 'request=[^&]*' | cut -d= -f2) fi - - # Prepare new JSON entry - ESCAPED_TIMESTAMP=$(printf '%s' "$TIMESTAMP" | sed 's/"/\\"/g') - ESCAPED_OUTPUT=$(printf '%s' "$DATA_OUTPUT" | sed 's/"/\\"/g') - - # Use jq with a more robust approach - jq --arg datetime "$ESCAPED_TIMESTAMP" \ - --arg output "$ESCAPED_OUTPUT" \ - ' - # Ensure the input is always an array - if type == "array" then . - else [] - end | - # Add new entry - . + [{"datetime": $datetime, "output": $output}] | - # Trim to max entries - .[-'"$MAX_ENTRIES"':] - ' "$LOGFILE" > "${LOGFILE}.tmp" && mv "${LOGFILE}.tmp" "$LOGFILE" + + case "$QUERY" in + "cellScan") + perform_cell_scan + ;; + *) + printf "Content-Type: application/json\n\n" + printf '{"status":"error","message":"Invalid request"}\n' + ;; + esac } -# Trap to handle script termination gracefully -cleanup() { - echo "Stopping signal logging..." - exit 0 -} -trap cleanup SIGINT SIGTERM +# Function to start continuous logging +start_continuous_logging() { + # Check if another instance is running + if [ -f "$LOCK_FILE" ]; then + logger -t signal_metrics "Another instance is already running" + exit 1 + fi -# Continuous logging loop -echo "Starting continuous signal metrics logging (Press Ctrl+C to stop)..." -while true; do - # Log RSRP - log_signal_metric "AT+QRSRP" "rsrp.json" - # Log RSRQ - log_signal_metric "AT+QRSRQ" "rsrq.json" - # Log SINR - log_signal_metric "AT+QSINR" "sinr.json" - # Log data usage after signal stats - log_data_usage - # Wait for the specified interval - sleep "$INTERVAL" -done \ No newline at end of file + # Create lock file + touch "$LOCK_FILE" + + # Cleanup on exit + trap 'rm -f "$LOCK_FILE" "$PAUSE_FILE"; exit 0' INT TERM + + # Log start to system log + logger -t signal_metrics "Starting continuous signal metrics logging" + + # Continuous logging loop + while true; do + if [ ! -f "$PAUSE_FILE" ]; then + log_signal_metric "AT+QRSRP" "rsrp.json" + log_signal_metric "AT+QRSRQ" "rsrq.json" + log_signal_metric "AT+QSINR" "sinr.json" + log_data_usage + fi + sleep "$INTERVAL" + done +} + +# Check if script is being run as CGI or directly +if [ -n "$REQUEST_URI" ] || [ -n "$QUERY_STRING" ]; then + handle_cgi_request +else + start_continuous_logging +fi \ No newline at end of file