Files
op-packages/luci-app-quickactions/root/usr/libexec/rpcd/luci.quickactions-taskplan
T

118 lines
3.9 KiB
Bash
Executable File

#!/bin/sh
# ubus RPC module for Quick Actions Task Plan
. /usr/share/libubox/jshn.sh
LOG_FILE="/etc/quickactions-taskplan/taskplan.log"
CONFIG_FILE="/etc/config/taskplan"
CRON_FILE="/etc/crontabs/root"
INIT_SCRIPT="/etc/init.d/quickactions-taskplan"
list_methods() {
cat <<'EOF'
{
"status": {},
"list": {},
"task_list": {},
"start": {},
"stop": {},
"restart": {},
"reload": {}
}
EOF
}
method_status() {
local running=0 cron_tasks=0 scheduled=0 startup=0 log_content=""
pgrep -f quickactions-taskplan >/dev/null 2>&1 && running=1
[ -f "$CRON_FILE" ] && cron_tasks=$(grep -c "quickactions-taskplanhandler" "$CRON_FILE" 2>/dev/null || echo 0)
[ -f "$CONFIG_FILE" ] && scheduled=$(grep -c "stime" "$CONFIG_FILE" 2>/dev/null || echo 0)
[ -f "$CONFIG_FILE" ] && startup=$(grep -c "ltime" "$CONFIG_FILE" 2>/dev/null || echo 0)
[ -f "$LOG_FILE" ] && log_content=$(tail -n 50 "$LOG_FILE" 2>/dev/null)
json_init
json_add_int "running" $running
json_add_int "cron_tasks" $cron_tasks
json_add_int "scheduled_tasks" $scheduled
json_add_int "startup_tasks" $startup
json_add_string "log" "$log_content"
json_dump
}
method_list() {
local ssum=0 lsum=0
[ -f "$CONFIG_FILE" ] && ssum=$(grep -c "stime" "$CONFIG_FILE" 2>/dev/null || echo 0)
[ -f "$CONFIG_FILE" ] && lsum=$(grep -c "ltime" "$CONFIG_FILE" 2>/dev/null || echo 0)
json_init
json_add_int "scheduled" $ssum
json_add_int "startup" $lsum
json_dump
}
method_task_list() {
json_init
json_add_array "tasks"
if [ -f "$CONFIG_FILE" ]; then
local index=0
while true; do
local section=$(uci -q get taskplan.@stime[$index] 2>/dev/null)
[ -z "$section" ] && break
json_add_object
json_add_string "type" "scheduled"
json_add_int "index" $index
json_add_int "enable" "$(uci -q get taskplan.@stime[$index].enable || echo 0)"
json_add_string "remarks" "$(uci -q get taskplan.@stime[$index].remarks || echo "")"
json_add_string "stype" "$(uci -q get taskplan.@stime[$index].stype || echo "")"
json_add_string "schedule" "$(uci -q get taskplan.@stime[$index].minute) $(uci -q get taskplan.@stime[$index].hour) $(uci -q get taskplan.@stime[$index].week) $(uci -q get taskplan.@stime[$index].month)"
json_close_object
index=$((index + 1))
done
index=0
while true; do
local section=$(uci -q get taskplan.@ltime[$index] 2>/dev/null)
[ -z "$section" ] && break
json_add_object
json_add_string "type" "startup"
json_add_int "index" $index
json_add_int "enable" "$(uci -q get taskplan.@ltime[$index].enable || echo 0)"
json_add_string "remarks" "$(uci -q get taskplan.@ltime[$index].remarks || echo "")"
json_add_string "stype" "$(uci -q get taskplan.@ltime[$index].stype || echo "")"
json_add_int "delay" "$(uci -q get taskplan.@ltime[$index].delay || echo 10)"
json_close_object
index=$((index + 1))
done
fi
json_close_array
json_dump
}
service_action() {
local action="$1"
[ -x "$INIT_SCRIPT" ] && "$INIT_SCRIPT" "$action" >/dev/null 2>&1
local ret=$?
json_init
json_add_int "result" $ret
[ $ret -eq 0 ] && json_add_string "message" "Service $action ok"
json_dump
}
call_method() {
local method="$1"
cat >/dev/null
case "$method" in
status) method_status ;;
list) method_list ;;
task_list) method_task_list ;;
start) service_action start ;;
stop) service_action stop ;;
restart) service_action restart ;;
reload) service_action reload ;;
*) json_init; json_add_string "error" "unknown_method"; json_dump ;;
esac
}
case "$1" in
list) list_methods ;;
call) call_method "$2" ;;
*) echo '{"error":"usage: '$0' {list|call <method>}"}' ;;
esac