added sms, deviceinfo, settings, and watchat changes

This commit is contained in:
Russel Yasol
2024-05-25 07:54:30 +08:00
parent 0ada4e6f64
commit 9f800af0fd
9 changed files with 1061 additions and 547 deletions

View File

@@ -1,20 +1,12 @@
#!/bin/bash
# This is a simple scrip that fetches the SMS messages from the device
SMS_MESSAGE_INDICATION="AT+CNMI=2,1"
SMS_FORMAT="AT+CMGF=1"
SMS_LIST="AT+CMGL=\"ALL\""
send_at_command() {
local command="$1"
echo -en "$command\r\n" | microcom -t 2000 /dev/ttyOUT2
}
send_at_command "$SMS_FORMAT"
runcmd=$(send_at_command "$SMS_LIST")
while true; do
runcmd=$(echo -en "$SMS_LIST\r\n" | microcom -t 2000 /dev/ttyOUT2)
if [[ $runcmd =~ "OK" ]] || [[ $runcmd =~ "ERROR" ]]; then
break
fi
((wait_time++))
done
# Print the list of SMS messages as JSON plain text
echo "Content-type: text/plain"
echo ""
echo $runcmd
echo "$runcmd"

View File

@@ -0,0 +1,18 @@
#!/bin/bash
# Content type header
echo "Content-type: application/json"
echo ""
# This script fetches the watchCat parameters from the /tmp/watchCatParams.json file and returns it as JSON
# Check if the file exists
if [ -f /tmp/watchCatParams.json ]; then
# Read the file and return the content
cat /tmp/watchCatParams.json
else
# Return an empty JSON object
echo "{}"
fi
exit 0

View File

@@ -1,44 +1,54 @@
#!/bin/bash
QUERY_STRING=$(echo "${QUERY_STRING}" | sed 's/;//g')
function urldecode() { : "${*//+/ }"; echo -e "${_//%/\\x}"; }
urldecode() {
local data
data="${*//+/ }"
echo -e "${data//%/\\x}"
}
if [ "${QUERY_STRING}" ]; then
export IFS="&"
for cmd in ${QUERY_STRING}; do
if [ "$(echo $cmd | grep '=')" ]; then
key=$(echo $cmd | awk -F '=' '{print $1}')
value=$(echo $cmd | awk -F '=' '{print $2}')
eval $key=$value
if [[ "$cmd" == *=* ]]; then
key=$(echo "$cmd" | awk -F '=' '{print $1}')
value=$(echo "$cmd" | awk -F '=' '{print $2}')
eval "$key"="$(urldecode "$value")"
fi
done
fi
# Extract phone number and message from inputs
phone_number="$number"
message="$msg"
message_encoded="$msg"
# Prepare AT command with phone number and message
ATCMD="AT+CMGS=\"$phone_number\""
phone_number="+86$phone_number"
MYATCMD=$(printf '%b\n' "${ATCMD//%/\\x}")
if [ -n "${MYATCMD}" ]; then
x=$(urldecode "$ATCMD")
# Send the AT command to initiate message sending
echo -en "$x\r\n" | microcom /dev/ttyOUT2
# Wait for a brief moment (assuming the message sending is instantaneous)
sleep 1
fi
send_at_command() {
local cmd=$1
echo "Sending command: $cmd" >&2
echo -en "$cmd\r" | microcom -t 1000 /dev/ttyOUT2
sleep 2
local response=$(microcom -t 1000 /dev/ttyOUT2)
echo "Response: $response" >&2
echo "$response"
}
# Send the message
echo -en "$message\c"
send_at_command "AT+CMGF=1"
send_at_command "AT+CSCS=\"UCS2\""
# Send Ctrl+Z to terminate the message
echo -en "\032"
encode_ucs2() {
local input="$1"
local output=""
local i
for ((i=0; i<${#input}; i++)); do
hex=$(printf "%04X" "'${input:$i:1}")
output="$output$hex"
done
echo "$output"
}
# Ensure microcom reads the response (assuming microcom will show response instantly)
sleep 1
phone_number_ucs2=$(encode_ucs2 "$phone_number")
ATCMD="AT+CMGS=\"$phone_number_ucs2\""
send_at_command "$ATCMD"
# Capture and output the response
runcmd=$(microcom /dev/ttyOUT2)
echo "Content-type: text/plain"
runcmd=$((echo -en "$message_encoded"; sleep 1; echo -en "\x1A") | microcom -t 1000 /dev/ttyOUT2)
echo "$runcmd"

View File

@@ -0,0 +1,125 @@
#!/bin/bash
QUERY_STRING=$(echo "${QUERY_STRING}" | sed 's/;//g')
function urldecode() { : "${*//+/ }"; echo -e "${_//%/\\x}"; }
if [ "${QUERY_STRING}" ]; then
export IFS="&"
for cmd in ${QUERY_STRING}; do
if [[ "$cmd" == *"="* ]]; then
key=$(echo $cmd | awk -F '=' '{print $1}')
value=$(echo $cmd | awk -F '=' '{print $2}')
eval $key=$(urldecode $value)
fi
done
fi
if [ -z "$status" ] || [ -z "$IpDNS" ] || [ -z "$cooldown" ] || [ -z "$failures" ] || [ -z "$action" ]; then
response="Missing parameters. Please provide the following parameters: IpDNS, cooldown, failures, action."
echo "Content-type: text/plain"
echo ""
echo "$response"
exit 1
fi
if [ "$status" == "enabled" ]; then
watch_script="/usrdata/simpleadmin/script/watchat.sh"
mount -o remount,rw /
cat <<EOF > $watch_script
#!/bin/bash
ip_or_dns="$IpDNS"
cooldown=$cooldown
action="$action"
fail_count=0
max_failures=$failures
# Process the action variable.
# Create a JSON file containing the parameters of the script
echo -n '{"ip_or_dns":"$ip_or_dns","cooldown":$cooldown,"action":"$action","fail_count":$fail_count,"max_failures":$max_failures}' > /tmp/watchatParams.json
while true; do
if ping -c 1 -W 1 \$ip_or_dns > /dev/null; then
fail_count=0
echo "Success at \$(date)" >> /tmp/watchat.log
# Convert /tmp/watchat.log to json format
echo -n '{"log":[' > /tmp/watchat.json
cat /tmp/watchat.log | sed 's/$/,/' | tr -d '\n' | sed 's/,$//' >> /tmp/watchat.json
echo -n ']}' >> /tmp/watchat.json
else
((fail_count++))
if [ \$fail_count -ge \$max_failures ]; then
case "\$action" in
reboot)
echo "Rebooting system..."
/sbin/reboot
;;
switch_sim)
echo "Switching SIM..."
echo -ne "AT+CNMI=2,1\r" > /dev/ttyOUT2
sleep 1
echo "Switching SIM at \$(date)" >> /tmp/watchat.log
;;
none)
echo "No action taken."
echo "No action taken at \$(date)" >> /tmp/watchat.log
;;
*)
echo "Unknown action: \$action"
;;
esac
# Reset fail count
fail_count=0
fi
fi
echo "Fail count: \$fail_count at \$(date)" >> /tmp/watchat.log
# Convert /tmp/watchat.log to json format
echo -n '{"log":[' > /tmp/watchat.json
cat /tmp/watchat.log | sed 's/$/,/' | tr -d '\n' | sed 's/,$//' >> /tmp/watchat.json
echo -n ']}' >> /tmp/watchat.json
sleep \$cooldown
done
EOF
chmod +x $watch_script
cat <<EOF > /lib/systemd/system/watchcat.service
[Unit]
Description=Ping Watcher Service
[Service]
ExecStart=$watch_script
Restart=always
[Install]
WantedBy=multi-user.target
EOF
ln -s /lib/systemd/system/watchcat.service /etc/systemd/system/multi-user.target.wants/watchcat.service
systemctl daemon-reload
systemctl start watchcat.service
response="Script created at $watch_script and made executable. Service created and started."
elif [ "$status" == "disabled" ]; then
watch_script="/usrdata/simpleadmin/script/watchat.sh"
rm -f $watch_script
systemctl stop watchcat.service
rm -f /lib/systemd/system/watchcat.service
rm -f /etc/systemd/system/multi-user.target.wants/watchcat.service
systemctl daemon-reload
response="Script removed at $watch_script. Service stopped and removed."
else
response="Invalid status. Please provide either enabled or disabled."
fi
echo "Content-type: text/plain"
echo ""
echo "$response"