<% local VALID_HOURS = {["0"]=0,["1"]=1,["2"]=2,["3"]=3,["4"]=4,["5"]=5,["6"]=6,["7"]=7,["8"]=8,["9"]=9,["10"]=10,["11"]=11,["12"]=12,["13"]=13,["14"]=14,["15"]=15,["16"]=16,["17"]=17,["18"]=18,["19"]=19,["20"]=20,["21"]=21,["22"]=22,["23"]=23} local function get_status() return luci.sys.exec("pgrep -x /etc/subconverter/subconverter") ~= "" and "running" or "stopped" end local function get_enabled() return luci.sys.exec("ls /etc/rc.d | grep subconverter") ~= "" and "enabled" or "disabled" end local function get_port() local port = luci.sys.exec("grep -i '^port=' /etc/subconverter/pref.ini | cut -d '=' -f 2 | head -n 1") return port ~= "" and port:gsub("[^%d]", "") or "25500" end local function get_scheduled_time() local output = luci.sys.exec("crontab -l | grep -m 1 '^0 [0-9]* \\* \\* \\* /etc/init.d/subconverter start'") return VALID_HOURS[output:match("^0 (%d+) %* %* %*")] end local function update_scheduled_time(scheduled_time) if type(scheduled_time) ~= "number" then return end scheduled_time = math.floor(scheduled_time) local current_crontab = luci.sys.exec("crontab -l | grep -v -E '^0 [0-9]* \\* \\* \\* /etc/init.d/subconverter|^$'") if scheduled_time >= 0 and scheduled_time <= 23 then local start_time = string.format('0 %d * * * /etc/init.d/subconverter start\n', scheduled_time) local stop_time = string.format('0 %d * * * /etc/init.d/subconverter stop', (scheduled_time + 2) % 24) current_crontab = current_crontab .. start_time .. stop_time end luci.sys.exec(string.format("echo '%s' | crontab -", current_crontab)) end local function update_port(port) if type(port) ~= "number" then return end port = math.floor(port) if port >= 1 and port <= 65535 then os.execute(string.format('sed -i \'s/^port=.*/port=%d/\' /etc/subconverter/pref.ini || echo "port=%d" >> /etc/subconverter/pref.ini', port, port)) end end local function start_service() luci.sys.exec("/etc/init.d/subconverter start ; /etc/init.d/subconverter enable") end local function stop_service() luci.sys.exec("/etc/init.d/subconverter stop ; /etc/init.d/subconverter disable") end if luci.http.formvalue("data") then luci.http.prepare_content("application/json") local json_string = "{" .. "\"status\":\"" .. tostring(get_status()) .. "\"," .. "\"enabled\":\"" .. tostring(get_enabled()) .. "\"," .. "\"scheduled_time\":\"" .. tostring(get_scheduled_time()) .. "\"," .. "\"port\":" .. tostring(get_port()) .. "}" luci.http.write(json_string) return end if luci.http.formvalue("status") then luci.http.prepare_content("text/plain") luci.http.write(get_status()) return end if luci.http.formvalue("start") then luci.sys.exec("/etc/init.d/subconverter start") luci.http.redirect(luci.dispatcher.build_url("admin", "services", "subconverter/subconverter")) return end if luci.http.formvalue("stop") then luci.sys.exec("/etc/init.d/subconverter stop") luci.http.redirect(luci.dispatcher.build_url("admin", "services", "subconverter/subconverter")) return end if luci.http.formvalue("settings") then if luci.http.formvalue("enable") == "on" then if luci.http.formvalue("schedule") == "on" then scheduled_time = VALID_HOURS[luci.http.formvalue("schedule-selection")] if scheduled_time ~= nil then stop_service() update_scheduled_time(scheduled_time) end else start_service() update_scheduled_time(-1) end else stop_service() update_scheduled_time(-1) end if luci.http.formvalue("port") then update_port(tonumber(luci.http.formvalue("port"))) end luci.http.redirect(luci.dispatcher.build_url("admin", "services", "subconverter/subconverter")) return end %> <%+header%>

Subconverter

luci-app-subconverter is an luci-app for subweb and subconverter. see the source code
<%+footer%>