Release version for 1.1.1-2

- Improved fetch_data script
- Improved quecwatch script
- Added scripts for cell scan
- Added a working UI for cell scan
This commit is contained in:
Russel Yasol
2025-01-23 20:17:02 +08:00
parent f2a451e862
commit cf9387c736
66 changed files with 37772 additions and 239 deletions

View File

@@ -0,0 +1,111 @@
#!/bin/sh
# Set content type to JSON
echo "Content-type: application/json"
echo ""
# Configuration
QUEUE_FILE="/tmp/at_pipe.txt"
RESULT_FILE="/tmp/qscan_result.json"
WORKER_SCRIPT="/www/cgi-bin/experimental/cell_scanner/cell_scan_worker.sh"
PID_FILE="/tmp/cell_scan.pid"
CELL_SCAN_KEYWORD="CELL_SCAN"
# Function to log messages
log_message() {
logger -t cell_scan "$1"
}
# Function to output JSON response
output_json() {
local status="$1"
local message="$2"
log_message "Sending response: status=$status, message=$message"
printf '{"status":"%s","message":"%s"}\n' "$status" "$message"
exit 0
}
# Function to check if worker is running
check_worker_running() {
if [ -f "$PID_FILE" ]; then
pid=$(cat "$PID_FILE")
if kill -0 "$pid" 2>/dev/null; then
log_message "Worker process $pid is running"
return 0
fi
log_message "Removing stale PID file for process $pid"
rm -f "$PID_FILE"
fi
return 1
}
# Function to wait for queue to be ready
wait_for_queue() {
local retries=0
while [ ! -f "$QUEUE_FILE" ] && [ $retries -lt 10 ]; do
touch "$QUEUE_FILE" 2>/dev/null || {
log_message "Waiting for queue file to be available (attempt $retries)"
sleep 1
retries=$((retries + 1))
continue
}
chmod 666 "$QUEUE_FILE" 2>/dev/null
log_message "Queue file created and permissions set"
break
done
}
# Function to add scan entry to queue
add_scan_entry() {
# Wait for queue file to exist
wait_for_queue
local entry
entry=$(printf '{"command":"%s","id":"%s","pid":"%s","timestamp":"%s","priority":"high","status":"queued"}\n' \
"$CELL_SCAN_KEYWORD" \
"cell_scan_$$" \
"$$" \
"$(date '+%H:%M:%S')")
echo "$entry" >> "$QUEUE_FILE"
log_message "Added scan entry to queue: $entry"
# Verify entry was added
if ! grep -q "\"pid\":\"$$\"" "$QUEUE_FILE"; then
log_message "Failed to verify scan entry in queue"
return 1
fi
sync
return 0
}
# Ensure worker script is executable
chmod +x "$WORKER_SCRIPT" 2>/dev/null
log_message "Ensured worker script is executable"
# Main execution
{
# If scan is running, return running status
if check_worker_running; then
output_json "running" "Cell scan is in progress"
fi
# Start new scan
rm -f "$RESULT_FILE"
log_message "Starting new worker script: $WORKER_SCRIPT"
# Add scan entry to queue before starting worker
if ! add_scan_entry; then
log_message "Failed to add scan entry to queue"
output_json "error" "Failed to acquire queue lock"
fi
sh "$WORKER_SCRIPT" >/tmp/cell_scan_worker.log 2>&1 &
log_message "Worker script started with PID $!"
output_json "running" "Started new cell scan"
} || {
# Error handler
log_message "Script failed with error"
output_json "error" "Internal error occurred"
}

View File

@@ -0,0 +1,104 @@
# Modified cell_scan_worker.sh
#!/bin/sh
# Configuration
QUEUE_FILE="/tmp/at_pipe.txt"
RESULT_FILE="/tmp/qscan_result.json"
PID_FILE="/tmp/cell_scan.pid"
CELL_SCAN_KEYWORD="CELL_SCAN"
SCAN_COMMAND="AT+QSCAN=3,1"
SCAN_TIMEOUT=200
# Function to log messages
log_message() {
logger -t cell_scan_worker "$1"
}
# Function to clean AT command output
clean_output() {
while IFS= read -r line; do
case "$line" in
"OK" | "" | *"ERROR"*)
continue
;;
*)
printf '%s\n' "$line"
;;
esac
done | sed 's/\r//g' | tr '\n' '\r' | sed 's/\r$//' | tr '\r' '\n'
}
# Function to check if scan is already running
check_running() {
if [ -f "$PID_FILE" ]; then
pid=$(cat "$PID_FILE")
if kill -0 "$pid" 2>/dev/null; then
return 0
fi
rm -f "$PID_FILE"
fi
return 1
}
# Function to remove scan entry
remove_scan_entry() {
if [ -f "$QUEUE_FILE" ]; then
sed -i "/\"pid\":\"$$\"/d" "$QUEUE_FILE"
sync
fi
if [ -f "$PID_FILE" ]; then
rm -f "$PID_FILE"
fi
log_message "Scan entry and PID file removed"
}
# Main execution
main() {
# Start logging
log_message "Worker script started"
# Check if already running
if check_running; then
log_message "Cell scan already running"
exit 1
fi
# Create PID file
echo "$$" >"$PID_FILE"
chmod 666 "$PID_FILE" 2>/dev/null
log_message "Created PID file: $$"
# Execute scan with timeout and process output
log_message "Executing scan command: $SCAN_COMMAND"
SCAN_OUTPUT=$(timeout $SCAN_TIMEOUT sms_tool at "$SCAN_COMMAND" -t $SCAN_TIMEOUT 2>&1 | clean_output)
SCAN_STATUS=$?
# Process and store result
if [ $SCAN_STATUS -eq 0 ] && [ -n "$SCAN_OUTPUT" ]; then
TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')
printf '{"status":"success","timestamp":"%s","output":"%s"}\n' \
"$TIMESTAMP" \
"$(printf '%s' "$SCAN_OUTPUT" | jq -R -s '.')" >"$RESULT_FILE"
chmod 666 "$RESULT_FILE" 2>/dev/null
log_message "Scan completed successfully"
remove_scan_entry
exit 0
else
log_message "Scan failed with status: $SCAN_STATUS"
printf '{"status":"error","timestamp":"%s","message":"Scan failed"}\n' \
"$(date '+%Y-%m-%d %H:%M:%S')" >"$RESULT_FILE"
chmod 666 "$RESULT_FILE" 2>/dev/null
remove_scan_entry
exit 1
fi
}
# Execute main function with proper error handling
{
main
} || {
log_message "Script failed with error"
remove_scan_entry
exit 1
}

View File

@@ -0,0 +1,60 @@
#!/bin/sh
# Set content type to JSON
echo "Content-type: application/json"
echo ""
# Configuration
RESULT_FILE="/tmp/qscan_result.json"
PID_FILE="/tmp/cell_scan.pid"
# Function to output JSON response
output_json() {
local status="$1"
local message="$2"
if [ "$status" = "success" ] && [ -f "$RESULT_FILE" ]; then
# Remove trailing quotes from output field and clean up formatting
sed 's/"output":""/"output":"/; s/""}/"}/' "$RESULT_FILE"
else
printf '{"status":"%s","message":"%s","timestamp":"","output":""}\n' "$status" "$message"
fi
}
# Check if a scan is already in progress
check_scan_progress() {
if [ -f "$PID_FILE" ]; then
pid=$(cat "$PID_FILE")
if kill -0 "$pid" 2>/dev/null; then
output_json "running" "Scan in progress"
exit 0
else
rm -f "$PID_FILE"
fi
fi
}
# Check for existing results
check_results() {
if [ -f "$RESULT_FILE" ]; then
output_json "success" "Scan results available"
exit 0
fi
}
# Main execution
{
# First check if a scan is in progress
check_scan_progress
# Then check for existing results
check_results
# If no results and no running scan, indicate idle state
output_json "idle" "No active scan"
exit 0
} || {
# Error handler
output_json "error" "Failed to check scan status"
exit 1
}

View File

@@ -0,0 +1,40 @@
#!/bin/sh
# Set content type to JSON
echo "Content-type: application/json"
echo ""
# Configuration
JSON_FILE="/www/cgi-bin/mcc-mnc-list.json"
# Function to log messages
log_message() {
logger -t fetch_mccmnc "$1"
}
# Function to output JSON response
output_json() {
local status="$1"
local message="$2"
printf '{"status":"%s","message":"%s"}\n' "$status" "$message"
exit 1
}
# Main execution
{
# Check if file exists
if [ ! -f "$JSON_FILE" ]; then
log_message "MCC-MNC list file not found"
output_json "error" "MCC-MNC list file not found"
fi
# Read and output the file
cat "$JSON_FILE" 2>/dev/null || {
log_message "Failed to read MCC-MNC list file"
output_json "error" "Failed to read MCC-MNC list file"
}
} || {
# Error handler
log_message "Script failed with error"
output_json "error" "Internal error occurred"
}

View File

@@ -96,6 +96,21 @@ QUEUE_FILE="/tmp/at_pipe.txt"
LOG_FILE="/tmp/log/quecwatch/quecwatch.log"
[ ! -f "${QUEUE_FILE}" ] && touch "${QUEUE_FILE}"
# Function to persist retry count to a more permanent location
persist_retry_count() {
local count=$1
echo "$count" > /etc/quecmanager/quecwatch/retry_count
}
# Function to load persisted retry count
load_retry_count() {
if [ -f /etc/quecmanager/quecwatch/retry_count ]; then
cat /etc/quecmanager/quecwatch/retry_count
else
echo "0"
fi
}
# Enhanced logging function with debug level
log_message() {
local level="$1"
@@ -178,9 +193,12 @@ execute_at_command() {
return 1
}
# Function to update retry count in config
# Function to update retry count in config and persistent storage
update_retry_count() {
local new_retry_count=$1
# Update the persistent count file
persist_retry_count "$new_retry_count"
# Update the config file
sed -i "s/CURRENT_RETRIES=[0-9]*/CURRENT_RETRIES=${new_retry_count}/" /etc/quecmanager/quecwatch/quecwatch.conf
# Reload config to ensure latest values
. /etc/quecmanager/quecwatch/quecwatch.conf
@@ -215,7 +233,7 @@ switch_sim_card() {
if [ $? -ne 0 ]; then
log_message "ERROR" "Failed to get current SIM slot"
return 1
fi # Changed from } to fi
fi
# Toggle between SIM slots
new_sim_slot=$((current_sim_slot % 2 + 1))
@@ -254,7 +272,7 @@ perform_connection_recovery() {
if ! execute_at_command "AT+COPS=0"; then
log_message "ERROR" "Failed to reattach to network"
return 1
fi # <-- Changed from } to fi
fi
sleep 5
@@ -285,7 +303,7 @@ fi
# Main monitoring loop
failure_count=0
retry_trigger=0
retry_trigger=$(load_retry_count)
sim_failover_interval=0
while true; do
@@ -309,12 +327,19 @@ while true; do
failure_count=0
update_retry_count 0
else
log_message "ERROR" "SIM failover failed. Performing system reboot."
log_message "ERROR" "SIM failover failed. Updating retry count before reboot."
retry_trigger=$((retry_trigger + 1))
update_retry_count ${retry_trigger}
log_message "INFO" "Updated retry count to ${retry_trigger}. Performing system reboot."
reboot
fi
else
log_message "INFO" "Max retries exhausted. Auto SIM failover disabled. Removing QuecWatch."
# Clean up the retry count file
rm -f /etc/quecmanager/quecwatch/retry_count
# Remove from rc.local and disable
sed -i '\|/etc/quecmanager/quecwatch/quecwatch.sh|d' /etc/rc.local
sed -i 's/ENABLED=true/ENABLED=false/' /etc/quecmanager/quecwatch/quecwatch.conf
reboot
exit 0
fi
@@ -324,7 +349,10 @@ while true; do
failure_count=0
update_retry_count 0
else
log_message "ERROR" "Recovery failed. Performing system reboot."
log_message "ERROR" "Recovery failed. Updating retry count before reboot."
retry_trigger=$((retry_trigger + 1))
update_retry_count ${retry_trigger}
log_message "INFO" "Updated retry count to ${retry_trigger}. Performing system reboot."
reboot
fi
fi