diff --git a/updateWifi.lua b/updateWifi.lua new file mode 100644 index 0000000..629f2fb --- /dev/null +++ b/updateWifi.lua @@ -0,0 +1,1083 @@ +==============================第一版============================= +--function: 定时wifi开关 +--author: rh_Jameson +function smart_wifi_shutdown() + local wnet = 'mt7628.network1' + local table = {} + + --get para + --local close_time = luci.http.formvalue("close_time") + --local open_time = luci.http.formvalue("open_time") + + --test normal + --local close_time = os.time() + 5 + --local restart_time = os.time() + 10 + + --test exception + local close_time = os.time() - 5 + local restart_time = os.time() - 10 + + --para err manage + if close_time < os.time() or restart_time < close_time then + table["err"] = true + luci.http.write_json(table) + return + end + --do close + while true do + if os.time() ~= close_time then + posix.sleep(1) + else + wifi_shutdown(wnet) + table["close"] = true + luci.http.write_json(table) + break + end + end + + --do restart + while true do + if os.time() ~= restart_time then + posix.sleep(1) + else + wifi_reconnect(wnet) + table["restart"] = true + luci.http.write_json(table) + break + end + end +end + +============================第二版================================ + + +-----------------------api/index.lua------------------------------ +set_smart_wifi_updown = nwfs.set_smart_wifi_updown +set_wifi_up = nwfs.set_wifi_up +set_wifi_down = nwfs.set_wifi_down +set_smart_wifi_update = nwfs.set_smart_wifi_update +set_smart_wifi_stop = nwfs.set_smart_wifi_stop + +--rh +page = entry({"api", "wifiUp"}, call("set_wifi_up"), nil) +page.leaf = true +page = entry({"api", "wifiDown"}, call("set_wifi_down"), nil) +page.leaf = true +page = entry({"api", "setSmartWifiUpdate"}, call("set_smart_wifi_update"), nil) +page.leaf = true +page = entry({"api", "setSmartWifiStop"}, call("set_smart_wifi_stop"), nil) +page.leaf = true +page = entry({"api", "setSmartWifiUpdown"}, call("set_smart_wifi_updown"), nil) +page.leaf = true + + + + +------------------------meizu/nwfs.lua---------------------------- +function set_wifi_up() + --local cmd = "/sbin/wifi up".. wnet + local cmd = "/sbin/wifi up ".. "mt7628" + exec_cmd_in_sh(cmd) + luci.http.write_json("true") +end + +function set_wifi_down() + local cmd = "/sbin/wifi down ".."mt7628" + --local cmd = "/sbin/wifi down"..wnet + exec_cmd_in_sh(cmd) + luci.http.write_json("true") +end + +function set_smart_wifi_stop() + local pid = luci.http.formvalue("pid") + --exception manage + if(close_time == nil or open_time == nil) then + luci.http.write_json("smart_wifi not set") + return + end + if(pid == nil) then + luci.http.write_json("pid err") + end + + --到点关闭前 + if os.time() < close_time then + local cmd = "kill "..pid + --到点关闭后,到点启动前 + elseif os.time() >= close_time and os.time() <= open_time then + local cmd = "kill "..pid.."; /sbin/wifi up" + --到点启动后 + else + luci.http.write_json("smart wifi has finished,can not stop!") + return + end + exec_cmd_in_sh(cmd) + luci.http.write_json("true") +end + +function set_smart_wifi_update() + local pid = luci.http.formvalue("pid") + + --exception manage + if(close_time == nil or open_time == nil) then + luci.http.write_json("smart_wifi not set") + return + end + if(pid == nil) then + luci.http.write_json("pid err") + end + + --到点关闭前 + if os.time() < close_time then + local cmd = "kill "..pid + --到点关闭后,到点启动前 + elseif os.time() >= close_time and os.time() <= open_time then + local cmd = "kill "..pid.."; /sbin/wifi up" + --到点启动后 + end + exec_cmd_in_sh(cmd) + + --get para + --close_time = os.time(lucimZhttp.formvalue("close_time")) + --open_time = os.time(luci.http.formvalue("open_time")) + + --test normal + close_time = os.time() + 5 + restart_time = os.time() + 10 + + --test exception + --close_time = os.time() - 5 + --restart_time = os.time() - 10 + + set_smart_wifi_updown(close_time, restart_time) +end + + +function fork_smart_wifi_updown( wnet, close_time, open_time) + local close_interval = close_time - os.time() + local open_interval = open_time - os.time() + local cmd = string.format("sleep 15;/sbin/wifi down "..wnet.."; sleep 15; /sbin/wifi up "..wnet) + + return exec_cmd_in_sh(cmd) + --exec_cmd_in_sh(cmd) +end + + +--function: 定时wifi开关shell形式 +--author: rh_Jameson +function set_smart_wifi_updown() + local wnet = 'mt7628' + local info = {} + + --get para + --close_time = lucimZhttp.formvalue("close_time") + --open_time = luci.http.formvalue("open_time") + + --test normal + close_time = os.time() + 5 + restart_time = os.time() + 10 + + --test exception + --close_time = os.time() - 5 + --restart_time = os.time() - 10 + + --para err manage + if close_time < os.time() or restart_time < close_time then + info["SUCCESS"] = false + else + info["SUCCESS"] = true + end + + local pid = fork_smart_wifi_updown(wnet, close_time, restart_time) + info["PID"] = pid + luci.http.write_json(info) +end +--重载 +function set_smart_wifi_updown(close_time,restart_time) + + local wnet = 'mt7628' + local info = {} + + --para err manage + if close_time < os.time() or restart_time < close_time then + info["SUCCESS"] = false + else + info["SUCCESS"] = true + end + + local pid = fork_smart_wifi_updown(wnet, close_time, restart_time) + info["PID"] = pid + luci.http.write_json(info) +end + + +-------------------------------bfs.lua--line_92------------------------------- +function exec_cmd_in_sh(command) + local nio = require("nixio") + require "MZLog".log(3, command) + local pid = nio.fork() + if pid > 0 then + return pid + elseif pid == 0 then + nio.chdir("/") + local null = nio.open("/dev/null", "w+") + if null then + nio.dup(null, nio.stderr) + nio.dup(null, nio.stdout) + nio.dup(null, nio.stdin) + if null:fileno() > 2 then + null:close() + end + end + nio.exec("/bin/sh", "-c", command) + end +end + + +-------------------------UTC-CODE-字符串形式UTC时间转时间戳------------------------ +local lue = require("luci.util").exec +local t = lue([[date -d "2010-10-10 10:10:10" +%s]]) +print(tonumber(t)) + + +local lue = require("luci.util").exec + + +--local t = lue('date -d "2010-10-10 10:10:10" +%s') + +local t = lue([[date -d "2010-10-10 10:10:10" +%s]]) + +print(tonumber(t)) +print(t) + + +----------------------------Lua-多线程回调函数实现--------------------------------- +--回调 +function fork_exec_in_lua(func) + local nio = require("nixio") +-- require "MZLog".log(3, command) + local pid = nio.fork() + if pid > 0 then + print(pid) + return + elseif pid == 0 then + func() + end +end + + +--主调 +local bfs = require("meizu.bfs") +function test() + local i = 1 + while(i < 10) do + print(i) + i = i + 1 + end +end + +bfs.fork_exec_in_lua(test) + + + +----------------------------Lua-函数不可以重载----------------------------------- +--test +function fun() + print("11111") +end + +function fun(str) + print(str) +end + +fun() +fun("2222") +fun() + + +--result +nil +2222 +nil + +----------------------------crontabs & crond------------------------------------ + +crontab -u //设定某个用户的cron服务,一般root用户在执行这个命令的时候需要此参数 +crontab -l //列出某个用户cron服务的详细内容 +crontab -r //删除没个用户的cron服务 +crontab -e //编辑某个用户的cron服务 + +基本用法: +1. crontab -l +列出当前的crontab任务 +2. crontab -d +删除当前的crontab任务 +3. crontab -e (solaris5.8上面是 crontab -r) +编辑一个crontab任务,ctrl_D结束 + + +4. crontab filename +以filename做为crontab的任务列表文件并载入 + +/*以下是多启动一个进程进行服务*/ +crond start //启动服务 + +crond stop //关闭服务 + +crond restart //重启服务 + +crond reload //重新载入配置 + +--openwrt设置/etc/crontabs/root: +* * * * * echo "hello" >> /cron.txt + +--每次做修改时使用crontab /etc/crondtabs/root +--crontabs/root文件下写的定时任务是多线程运行,不按顺序执行 + + +Sed命令: +sed -n '/hello/p' cron.txt 查询hello关键字,打印相应行 + +sed '/hello/d' cron.txt 删除含hello的行,输出到屏幕(原文件没删除) + +sed -i '/hello/d' cron.txt 直接修改原文件 + + + +--[[non-repeat任务]]-- +* * * * * echo "flag non-repeat">> /cron.txt; sed -i '/sed/d' /etc/crontabs/root + +--[[repeat任务]]-- +--添加定时任务exam +echo "* * * * * date >>/cron.txt">>/etc/crontabs/root;flag >/dev/null 2>/ + +--删除定时任务exam +sed -i '/hello/d' /etc/crontabs/root + +--修改定时任务exam + +==================================第三版-TEST================================ + +echo '* * * * non-repeat /sbin/wifi down mt7628; sed -i "/* * * * non-repeat/d" etc/crontabs/root' >> /etc/crontabs/root; crontab /etc/crontabs/root; + +* * * * * /sbin/wifi down mt7628; sed -i '/sed/d' etc/crontabs/root; crontab /etc/crontabs/root; +* * * * * date >>/cron.txt; sed -i '/sed/d' /etc/crontabs/root + + +--non-repeat任务: +echo "$1 $2 * * $3 /sbin/wifi down mt7628; + sleep 4; + sed -i '/$1$2$3/d' /etc/crontabs/root; + crontab /etc/crontab/root; + $1$2$3 >/dev/null 2>/dev/null " >> /etc/crontabs/root; crontab /etc/crontabs/root; + +bin cron.txt * * dev /sbin/wifi down mt7628;sleep 4; sed -i '/bincron.txtdev/d' /etc/crontabs/root; crontab /etc/crontab/root; bincron.txtdev >/dev/null 2>/dev/null + + + +--non-repeate任务: + if week_repeat == "non-repeat" then + week_repeat = "0,1,2,3,4,5,6" + close_min = "40" + Segmentation fault + cmd = "/sbin/smart@OpenWrt:/usr/lib/lua/meizu# e_hour.." "..week_repeat + else + cmd = "echo '"..close_min.." "..close_hour.." * * "..week_repeat.." /sbin/wifi down "..wnet.."' >> /etc/crontabs/root; crontab /etc/cronta + ..close_min..close_hour..open_hour..open_min..week_repeat.." >/dev/null 2>/dev/null" + --cmd = "echo '"..open_min.." "..open_hour.." * * "..week_repeat.." /sbin/wifi up "..wnet.."' >> /etc/crontabs/root; crontab /etc/crontabs + end + luci.http.write_json(cmd) + exec_cmd_in_sh(cmd) + luci.http.write_json(true) + + + +if week_repeat == "non-repeat" then + week_repeat = "*" + cmd_close = "/sbin/smart_wifi "..close_hour.." "..close_min.." "..week_repeat + --cmd_open = "echo '"..close_min.." "..close_hour.." * * "..week_repeat.." /sbin/wifi down "..wnet + -- .."; sed -i '/"..close_min..close_hour..open_hour..open_min..week_repeat + -- .."/d' etc/crontabs/root >> /etc/crontabs/root; crontab /etc/crontabs/root; " + -- ..close_min..close_hour..open_hour..open_min..week_repeat.." >/dev/null 2>/dev/null" + else + cmd_close = "echo '"..close_min.." "..close_hour.." * * "..week_repeat.." /sbin/wifi down "..wnet + .."' >> /etc/crontabs/root; crontab /etc/crontabs/root; " + ..close_min..close_hour..open_hour..open_min..week_repeat.." >/dev/null 2>/dev/null" + + --cmd_open = "echo '"..open_min.." "..open_hour.." * * "..week_repeat.." /sbin/wifi up "..wnet.."' >> /etc/crontabs/root; crontab /etc/crontabs/root; "..close_min..close_hour..open_hour..open_min..week_repeat.." >/dev/null 2>/dev/null" + end + + luci.http.write_json(cmd_close) + --luci.http.write_json(cmd_open) + + exec_cmd_in_sh(cmd_close) + --exec_cmd_in_sh(cmd_open) + + luci.http.write_json(true) + + +===============================================================第三版-succeed==================================================== + +-------------------------./package/base-files/files/sbin/smart_wifi---------------------------------- + +#!/bin/sh +echo "" >> /etc/crontabs/root; +echo "$1 $2 * * $3 /sbin/wifi down mt7628;sleep 4; sed -i '/$1$2$3/d' /etc/crontabs/root; crontab /etc/crontab/root; $1$2$3 >/dev/null 2>/dev/null " >> /etc/crontabs/root; crontab /etc/crontabs/root; +echo "" >> /etc/crontabs/root; + + +------------------------------------------------nwfs.lua code------------------------------------- + +------------------------------------------------set smart wifi------------------------------------ +function fork_smart_wifi_updown(wnet, close_hour, close_min, open_hour, open_min, week_repeat) + if week_repeat == "non-repeat" then + week_repeat = "0,1,2,3,4,5,6" --"1,2,3,4,5,6,0 is error,可能会误删repeat的数据" + cmd_close = "/sbin/smart_wifi "..close_min.." "..close_hour.." "..week_repeat + cmd_open = "/sbin/smart_wifi "..open_min.." "..open_hour.." "..week_repeat + else + cmd_close = "echo '"..close_min.." "..close_hour.." * * "..week_repeat.." /sbin/wifi down "..wnet..";" + ..close_min..close_hour..open_min..open_hour..week_repeat + ..">/dev/null 2>/dev/null' >> /etc/crontabs/root; crontab /etc/crontabs/root;" + + cmd_open = "echo '"..open_min.." "..open_hour.." * * "..week_repeat.." /sbin/wifi up "..wnet..";" + ..close_min..close_hour..open_min..open_hour..week_repeat + ..">/dev/null 2>/dev/null' >> /etc/crontabs/root; echo '' >> /etc/crontabs/root; crontab /etc/crontabs/root;" + end + luci.http.write_json(cmd_close) + luci.http.write_json(cmd_open) + + exec_cmd_in_sh(cmd_close) + posix.sleep(1) + exec_cmd_in_sh(cmd_open) +end + +-----------------------------------------------stop smart wifi------------------------------------ +function set_smart_wifi_stop(update_flag) + local cmd = "sed -i '/"..update_flag.."/d' /etc/crontabs/root; crontab /etc/crontabs/root" + exec_cmd_in_sh(cmd) + luci.http.write_json(cmd) +end + + + +--------------------------------------------update smart wifi------------------------------------- +--(stop + set) smartwifi + + + + +----------------------------------------------smart wifi api-------------------------------------- +--function: 定时wifi开关shell形式 +--author: rh_Jameson +function set_smart_wifi_updown() + + --get para + --wnet = luci.http.formvalue("wnet") + --close_hour = luci.http.formvalue("close_hour") + --close_min = luci.http.formvalue("close_min") + --open_hour = luci.http.formvalue("open_hour") + --open_min = luci.http.formvalue("open_min") + --repeat_var_from_http = luci.http.formvalue("repeat_var") + --func = luci.http.formvalue("func") + + --test + local wnet = 'mt7628' + local close_hour = "07" + local close_min = "25" + local open_hour = "07" + local open_min = "26" + local repeat_var_from_http = "1111111" + local func = luci.http.formvalue("func") + --para err manage + if string.len(repeat_var_from_http) ~= 7 then + luci.http.write_json("len error") + return + end + + --get repeat + local week_repeat = "" + for i = 0, #repeat_var_from_http do + tmp = string.sub(repeat_var_from_http, i, i) + if tmp == "1" then + if i == #repeat_var_from_http then + week_repeat = week_repeat..0 + break + end + week_repeat = week_repeat..tostring(i).."," + end + end + if week_repeat == "" then + week_repeat = "non-repeat" + end + --exec + if func == "set" then + fork_smart_wifi_updown(wnet, close_hour, close_min, open_hour, open_min, week_repeat) + elseif func == "stop" or func == "update" then + local update_flag = luci.http.formvalue("flag") + set_smart_wifi_stop(update_flag) + + if func == "update" then + posix.sleep(1) + fork_smart_wifi_updown(wnet, close_hour, close_min, open_hour, open_min, week_repeat) + end + end +end + + + +-----------------------api/index.lua------------------------------ +set_smart_wifi_updown = nwfs.set_smart_wifi_updown +set_wifi_up = nwfs.set_wifi_up +set_wifi_down = nwfs.set_wifi_down +set_smart_wifi_update = nwfs.set_smart_wifi_update +set_smart_wifi_stop = nwfs.set_smart_wifi_stop + +--rh +page = entry({"api", "wifiUp"}, call("set_wifi_up"), nil) +page.leaf = true +page = entry({"api", "wifiDown"}, call("set_wifi_down"), nil) +page.leaf = true +page = entry({"api", "setSmartWifiUpdate"}, call("set_smart_wifi_update"), nil) +page.leaf = true +page = entry({"api", "setSmartWifiStop"}, call("set_smart_wifi_stop"), nil) +page.leaf = true +page = entry({"api", "setSmartWifiUpdown"}, call("set_smart_wifi_updown"), nil) +page.leaf = true + + + + + +-----------------------第三版阉割代码-------------------------------- + +off功能: +sed -n '/hello/p' /etc/crontabs/root >> /tmp/wifi_cron_off.txt; sed -i '/hello/d' /etc/crontabs/root + +on功能: +sed -n '/timer0/p' /tmp/wifi_cron_off.txt >> /etc/crontabs/root; sed -i '/timer0/d' /tmp/wifi_cron_off.txt + + + +get match: +11 08 * * 1,2,3,4,5,6,0 /sbin/wifi down mt7628; #110812081,2,3,4,5,6,0; up_timer0 + + + + + + + + + + +cmd_close = "echo '"..close_min.." "..close_hour.." * * "..week_repeat.." /sbin/wifi down "..wnet.."; #" + ..close_min..close_hour..open_min..open_hour..week_repeat.."~ down_"..time_flag + .."' >> /etc/crontabs/root; crontab /etc/crontabs/root;" +cmd_open = "echo '"..open_min.." "..open_hour.." * * "..week_repeat.." /sbin/wifi up "..wnet.."; #" + ..close_min..close_hour..open_min..open_hour..week_repeat.."~ up_"..time_flag + .."' >> /etc/crontabs/root; echo ''>>/etc/crontabs/root; crontab /etc/crontabs/root;" + +cmd_close = "echo '"..close_min.." "..close_hour.." * * "..week_repeat.." /sbin/wifi down "..wnet.."; #" + ..close_min..close_hour..open_min..open_hour..week_repeat + .."' >> /etc/crontabs/root; crontab /etc/crontabs/root;" + cmd_open = "echo '"..open_min.." "..open_hour.." * * "..week_repeat.." /sbin/wifi up "..wnet.."; #" + ..close_min..close_hour..open_min..open_hour..week_repeat + .."' >> /etc/crontabs/root; echo ''>>/etc/crontabs/root; crontab /etc/crontabs/root;" + + + +http://192.168.232.1/cgi-bin/luci/api/setSmartWifiUpdown?func=set;flag=timer0 +http://192.168.232.1/cgi-bin/luci/api/setSmartWifiUpdown?func=stop;flag=timer0 +192.168.232.1/cgi-bin/luci/api/setSmartWifiUpdown?func=update;flag=timer0;new_flag=timer0 + +--get para + wnet = luci.http.formvalue("wnet") + close_hour = luci.http.formvalue("close_hour") + close_min = luci.http.formvalue("close_min") + open_hour = luci.http.formvalue("open_hour") + open_min = luci.http.formvalue("open_min") + repeat_var_from_http = luci.http.formvalue("repeat_var") + func = luci.http.formvalue("func") + flag = luci.http.formvalue("flag") + +--test normal + local wnet = 'mt7628' + local close_hour = "08" + local close_min = "11" + local open_hour = "08" + local open_min = "12" + local repeat_var_from_http = "1111111" + local func = luci.http.formvalue("func") + local time_flag = luci.http.formvalue("flag") +--update +local new_flag = luci.http.formvalue("new_flag") + +设置的参数: +fork_smart_wifi_updown(wnet, close_hour, close_min, open_hour, open_min, week_repeat, time_flag) + +修改的参数: +192.168.232.1/cgi-bin/luci/api/setSmartWifiUpdown?func=update;flag=timer0;new_flag=timer0 + + + +on & off只要flag +http://192.168.232.1/cgi-bin/luci/api/setSmartWifiOff?flag=timer0 + +http://192.168.232.1/cgi-bin/luci/api/setSmartWifiOn?flag=timer0 + +--------------------------------------------------------------------------------------- +update: + +http://192.168.232.1/cgi-bin/luci/api/setSmartWifiUpdown?func=update;flag=timer1;new_flag=timer0 + + +stop: +http://192.168.232.1/cgi-bin/luci/api/setSmartWifiUpdown?func=stop;flag=timer2 + +set: + +192.168.232.1/cgi-bin/luci/api/setSmartWifiUpdown?func=set;flag=timer2 + +on: + +http://192.168.232.1/cgi-bin/luci/api/setSmartWifiUpdown?func=on;flag=timer1 + +off: +http://192.168.232.1/cgi-bin/luci/api/setSmartWifiUpdown?func=off;flag=timer1 + +get: +http://192.168.232.1/cgi-bin/luci/api/getSmartWifiInfo + + + + + + + + +------------------------------------第三版update------------------------------------------------- +--set +function fork_smart_wifi_updown(wnet, close_hour, close_min, open_hour, open_min, week_repeat, time_flag) + if week_repeat == "non-repeat" then + week_repeat = "0,1,2,3,4,5,6" + cmd_close = "/usr/sbin/wifi_cron_switch "..close_min.." "..close_hour.." "..week_repeat.. " "..time_flag + .." "..wnet.." down" + cmd_open = "/usr/sbin/wifi_cron_switch "..open_min.." "..open_hour.." "..week_repeat.." "..time_flag + .." "..wnet.." up" + else + cmd_close = "echo '"..close_min.." "..close_hour.." * * "..week_repeat.." /sbin/wifi down "..wnet.."; #" + ..close_min..close_hour..open_min..open_hour..week_repeat.."~ down_"..time_flag + .."' >> /etc/crontabs/root; crontab /etc/crontabs/root;" + cmd_open = "echo '"..open_min.." "..open_hour.." * * "..week_repeat.." /sbin/wifi up "..wnet.."; #" + ..close_min..close_hour..open_min..open_hour..week_repeat.."~ up_"..time_flag + .."' >> /etc/crontabs/root; echo ''>>/etc/crontabs/root; crontab /etc/crontabs/root;" + end + luci.http.write_json(cmd_close) + luci.http.write_json(cmd_open) + require "MZLog".log(3, cmd_close) + require "MZLog".log(3, cmd_open) + exec_cmd_in_sh(cmd_close) + posix.sleep(1) + exec_cmd_in_sh(cmd_open) +end +--stop(del) +function set_smart_wifi_stop(update_flag) + local cmd = "sed -i '/"..update_flag.."/d' /etc/crontabs/root; crontab /etc/crontabs/root" + require "MZLog".log(3, cmd) + exec_cmd_in_sh(cmd) + luci.http.write_json(cmd) +end + +--off +function set_smart_wifi_off(update_flag) + --local update_flag = luci.http.formvalue("flag") + local cmd = "sed -n '/"..update_flag.."/p' /etc/crontabs/root >> /tmp/wifi_cron_off.txt; sed -i '/" + ..update_flag.."/d' /etc/crontabs/root; crontab /etc/crontabs/root" + require "MZLog".log(3, cmd) + exec_cmd_in_sh(cmd) + luci.http.write_json(cmd) +end + +--on +function set_smart_wifi_on(update_flag) + --local update_flag = luci.http.formvalue("flag") + local cmd = "sed -n '/"..update_flag.."/p' /tmp/wifi_cron_off.txt >> /etc/crontabs/root; sed -i '/" + ..update_flag.."/d' /tmp/wifi_cron_off.txt; crontab /etc/crontabs/root" + + require "MZLog".log(3, cmd) + exec_cmd_in_sh(cmd) + luci.http.write_json(cmd) +end + +--get +function get_smart_wifi_info() + --exec_cmd_in_sh("echo '' >> /etc/crontabs/root", "r") + local smart_wifi_info = io.open("/etc/crontabs/root", "r") + local res = {} + if smart_wifi_info == nil then + luci.http.write_json("false") + return + end + for line in smart_wifi_info:lines() do + idx,_ = string.find(line, "up_timer") + if line and idx then + time_idx_begin = string.find(line, "#") + 1 + time_idx_end = string.find(line, "~") - 1 + local item = {} + item["close_min"] = string.sub(line, time_idx_begin, time_idx_begin + 1) + item["close_hour"] = string.sub(line, time_idx_begin + 2, time_idx_begin + 3) + item["open_min"] = string.sub(line, time_idx_begin + 4, time_idx_begin + 5) + item["open_hour"] = string.sub(line, time_idx_begin + 6, time_idx_begin + 7) + item["repeat"] = string.sub(line, time_idx_begin + 8, time_idx_end) + item["switch"] = 1 + table.insert(res,item) + end + end + + exec_cmd_in_sh("echo '' >> /tmp/wifi_cron_off.txt") + smart_wifi_info = io.input("/tmp/wifi_cron_off.txt") + + for line in smart_wifi_info:lines() do + idx,_ = string.find(line, "up_timer") + if line and idx then + time_idx_begin = string.find(line, "#") + 1 + time_idx_end = string.find(lind, "~") - 1 + local item = {} + item["close_min"] = string.sub(line, time_idx_begin, time_idx_begin + 1) + item["close_hour"] = string.sub(line, time_idx_begin + 2, time_idx_begin + 3) + item["open_min"] = string.sub(line, time_idx_begin + 4, time_idx_begin + 5) + item["open_hour"] = string.sub(line, time_idx_begin + 6, time_idx_begin + 7) + item["repeat"] = string.sub(line, time_idx_begin + 8, time_idx_end) + item["switch"] = 0 + table.insert(res,item) + end + end + + require "MZLog".log(3, res) + smart_wifi_info:close() + luci.http.write_json(res) +end + + +--function: 定时wifi开关shell形式 +--author: rh_Jameson +function set_smart_wifi_updown() + --get para + --wnet = luci.http.formvalue("wnet") + --close_hour = luci.http.formvalue("close_hour") + --close_min = luci.http.formvalue("close_min") + --open_hour = luci.http.formvalue("open_hour") + --open_min = luci.http.formvalue("open_min") + --repeat_var_from_http = luci.http.formvalue("repeat_var") + --func = luci.http.formvalue("func") + --flag = luci.http.formvalue("flag") + + --test normal + local wnet = 'mt7628' + local close_hour = "08" + local close_min = "11" + local open_hour = "08" + local open_min = "12" + local repeat_var_from_http = "1111111" + local func = luci.http.formvalue("func") + local time_flag = luci.http.formvalue("flag") + + --para err manage + if string.len(repeat_var_from_http) ~= 7 then + luci.http.write_json("len error") + return + end + --get repeat + local week_repeat = "" + for i = 0, #repeat_var_from_http do + tmp = string.sub(repeat_var_from_http, i, i) + if tmp == "1" then + if i == #repeat_var_from_http then + week_repeat = week_repeat..0 + break + end + week_repeat = week_repeat..tostring(i).."," + end + end + if week_repeat == "" then + week_repeat = "non-repeat" + end + + --exec + if func == "set" then + fork_smart_wifi_updown(wnet, close_hour, close_min, open_hour, open_min, week_repeat, time_flag) + elseif func == "stop" or func == "update" then + set_smart_wifi_stop(time_flag) + if func == "update" then + local new_flag = luci.http.formvalue("new_flag") + posix.sleep(1) + fork_smart_wifi_updown(wnet, close_hour, close_min, open_hour, open_min, week_repeat, new_flag) + end + elseif func == "on" then + set_smart_wifi_on(time_flag) + elseif func == "off" then + set_smart_wifi_off(time_flag) + end +end + + + +-----------------------------wifi第三版 无多余tmp文件------------------------------ +--getsmartwifiinfo +for line in smart_wifi_info:lines() do + idx,_ = string.find(line, "up_timer") + if line and idx then + time_idx_begin = string.find(line, "#") + 1 + time_idx_end = string.find(line, "~") - 1 + local item = {} + item["close_min"] = string.sub(line, time_idx_begin, time_idx_begin + 1) + item["close_hour"] = string.sub(line, time_idx_begin + 2, time_idx_begin + 3) + item["open_min"] = string.sub(line, time_idx_begin + 4, time_idx_begin + 5) + item["open_hour"] = string.sub(line, time_idx_begin + 6, time_idx_begin + 7) + item["repeat"] = string.sub(line, time_idx_begin + 8, time_idx_end) + item["switch"] = string.sub(line, 1, 1) == "#" and 0 or 1 + table.insert(res,item) + end +end + + +--off +function set_smart_wifi_off(update_flag) + --local update_flag = luci.http.formvalue("flag") + --local cmd = "sed -n '/"..update_flag.."/p' /etc/crontabs/root >> /tmp/wifi_cron_off.txt; sed -i '/" + + + + + + + + +---------------------------中继转dhcp----------------------------- +r, msg = pcall(function() db:rows(sqlStr) end) +if r == false then + require "MZLog".log(3, "r false") + return result +end + + + + + + + + + + + + + + + + + -- ..update_flag.."/d' /etc/crontabs/root; crontab /etc/crontabs/root" + + local cmd = "sed -i '/"..update_flag.."/s/^/#/' /etc/crontabs/root; crontab /etc/crontabs/root" + require "MZLog".log(3, cmd) + exec_cmd_in_sh(cmd) + luci.http.write_json(cmd) +end + +--on +function set_smart_wifi_on(update_flag) + --local update_flag = luci.http.formvalue("flag") + --local cmd = "sed -n '/"..update_flag.."/p' /tmp/wifi_cron_off.txt >> /etc/crontabs/root; sed -i '/" + -- ..update_flag.."/d' /tmp/wifi_cron_off.txt; crontab /etc/crontabs/root" + + local cmd = "sed -i '/"..update_flag.."/s/^#//' /etc/crontabs/root; crontab /etc/crontabs/root" + require "MZLog".log(3, cmd) + exec_cmd_in_sh(cmd) + luci.http.write_json(cmd) +end + + + + + + + + +-----------------------------------------smart wifi updown 4th single switch------------------------------------------------ +--set +function fork_smart_wifi_updown(wnet, close_hour, close_min, open_hour, open_min, week_repeat, time_flag, switch) + local wifi_status = (switch == tostring(0) and "#" or "") + if week_repeat == "non-repeat" then + week_repeat = "0,1,2,3,4,5,6" + cmd_close = "/usr/sbin/wifi_cron_switch "..close_min.." "..close_hour.." "..week_repeat.. " "..time_flag + .." "..wnet.." down" + cmd_open = "/usr/sbin/wifi_cron_switch "..open_min.." "..open_hour.." "..week_repeat.." "..time_flag + .." "..wnet.." up" + else + cmd_close = "echo '"..wifi_status..close_min.." "..close_hour.." * * "..week_repeat.." /sbin/wifi down "..wnet.."; #" + ..close_min..close_hour..open_min..open_hour..week_repeat.."~ down_"..time_flag + .."' >> /etc/crontabs/root; crontab /etc/crontabs/root;" + cmd_open = "echo '"..wifi_status..open_min.." "..open_hour.." * * "..week_repeat.." /sbin/wifi up "..wnet.."; #" + ..close_min..close_hour..open_min..open_hour..week_repeat.."~ up_"..time_flag + .."' >> /etc/crontabs/root; echo ''>>/etc/crontabs/root; crontab /etc/crontabs/root;" + end + require "MZLog".log(3, cmd_close) + require "MZLog".log(3, cmd_open) + exec_cmd_in_sh(cmd_close) + posix.sleep(1) + exec_cmd_in_sh(cmd_open) +end +--stop(del) +function set_smart_wifi_stop(update_flag) + local cmd = "sed -i '/"..update_flag.."/d' /etc/crontabs/root; crontab /etc/crontabs/root" + require "MZLog".log(3, cmd) + exec_cmd_in_sh(cmd) +end + +--off +function set_smart_wifi_off(update_flag) + local cmd = "sed -i '/"..update_flag.."/s/^/#/' /etc/crontabs/root; crontab /etc/crontabs/root" + require "MZLog".log(3, cmd) + exec_cmd_in_sh(cmd) +end + +--on +function set_smart_wifi_on(update_flag) + local cmd = "sed -i '/"..update_flag.."/s/^#//' /etc/crontabs/root; crontab /etc/crontabs/root" + require "MZLog".log(3, cmd) + exec_cmd_in_sh(cmd) +end + +--get +function get_smart_wifi_info() + local smart_wifi_info = io.open("/etc/crontabs/root", "r") + local res = {} + if smart_wifi_info == nil then + luci.http.write_json("false") + return + end + for line in smart_wifi_info:lines() do + idx,_ = string.find(line, "up_timer") + if line and idx then + time_idx_begin = string.find(line, "#", 2) + 1 + time_idx_end = string.find(line, "~") - 1 + local item = {} + item["close_min"] = string.sub(line, time_idx_begin, time_idx_begin + 1) + item["close_hour"] = string.sub(line, time_idx_begin + 2, time_idx_begin + 3) + item["open_min"] = string.sub(line, time_idx_begin + 4, time_idx_begin + 5) + item["open_hour"] = string.sub(line, time_idx_begin + 6, time_idx_begin + 7) + + local tmp_rep = string.sub(line, time_idx_begin + 8, time_idx_end) + local repeat_var = "0000000" + for i = 1, #tmp_rep, 2 do + idx = string.sub(tmp_rep, i, i) + if idx ~= tostring(0) then + repeat_var = string.sub(repeat_var, 0, idx - 1) .. tostring(1) .. string.sub(repeat_var, idx + 1, #repeat_var) + else + repeat_var = string.sub(repeat_var, 1, #repeat_var - 1) .. tostring(1) + end + require "MZLog".log(3, repeat_var) + end + + item["repeat"] = repeat_var + item["switch"] = string.sub(line, 1, 1) == "#" and 0 or 1 + table.insert(res,item) + end + end + + require "MZLog".log(3, res) + smart_wifi_info:close() + luci.http.write_json(res) +end + + +function set_smart_wifi_updown() + local info = {} + --get para + wnet = luci.http.formvalue("wnet") + close_hour = luci.http.formvalue("close_hour") + close_min = luci.http.formvalue("close_min") + open_hour = luci.http.formvalue("open_hour") + open_min = luci.http.formvalue("open_min") + repeat_var_from_http = luci.http.formvalue("repeat_var") + func = luci.http.formvalue("func") + time_flag = luci.http.formvalue("flag") + switch = luci.http.formvalue("switch") + + + --test normal +-- local wnet = 'mt7628' +-- local close_hour = "08" +-- local close_min = "11" +-- local open_hour = "08" +-- local open_min = "12" +-- local repeat_var_from_http = "1111111" +-- local func = luci.http.formvalue("func") +-- local time_flag = luci.http.formvalue("flag") +-- local switch = luci.http.formvalue("switch") + + --para err manage + if string.len(repeat_var_from_http) ~= 7 then + info["res"] = false + luci.http.write_json(info) + require "MZLog".log(3, "len error") + return + end + --get repeat + local week_repeat = "" + + require "MZLog".log(3, repeat_var_from_http) + for i = 0, #repeat_var_from_http do + tmp = string.sub(repeat_var_from_http, i, i) + if tmp == "1" then + if i == #repeat_var_from_http then + week_repeat = week_repeat..0 + break + end + week_repeat = week_repeat..tostring(i).."," + end + end + if week_repeat == "" then + week_repeat = "non-repeat" + end + + --exec + if func == "set" then + set_smart_wifi_stop(time_flag) + posix.sleep(1) + fork_smart_wifi_updown(wnet, close_hour, close_min, open_hour, open_min, week_repeat, time_flag, switch) + elseif func == "stop" or func == "update" then + set_smart_wifi_stop(time_flag) + if func == "update" then + local new_flag = luci.http.formvalue("new_flag") + posix.sleep(1) + fork_smart_wifi_updown(wnet, close_hour, close_min, open_hour, open_min, week_repeat, new_flag) + end + elseif func == "on" then +-- set_smart_wifi_on(time_flag) + set_smart_wifi_stop(time_flag) + posix.sleep(1) + fork_smart_wifi_updown(wnet, close_hour, close_min, open_hour, open_min, week_repeat, time_flag, switch) + elseif func == "off" then +-- set_smart_wifi_off(time_flag) + set_smart_wifi_stop(time_flag) + posix.sleep(1) + fork_smart_wifi_updown(wnet, close_hour, close_min, open_hour, open_min, week_repeat, time_flag, switch) + end + info["res"] = true + luci.http.write_json(info) +end + + + + + + + + + + + + + + + + diff --git a/week-report.txt~ b/week-report.txt~ new file mode 100644 index 0000000..66e2d9c --- /dev/null +++ b/week-report.txt~ @@ -0,0 +1,144 @@ +5.4-5.8 + +5.4上午 +组装电脑,安装ubuntu系统及各种开发环境 + +5.6下午 +办理入职,申请路由器R10和R13,并且对其固件进行,解决各种软硬件错误 + +5.7 +熟悉luci代码,学会通过串口跟ssh连入路由器,配置vim编辑器 +5.8 +安装虚拟机、win7,学习修改luci框架上的代码,测试其是否生效 + + +5.11-5.15 + +5.11 +将R13配置为中继器,询问android端定时开关的具体需求,查找小米相关功能的代码,并开始进行代码编写 + +5.12 +操作系统升级导致系统奔溃,重新配置xwindow,并在命令模式上重新升级 +定时wifi开关功能使用while死循环实现,但为了管理方便,决定重新用shell来写 + +5.13 +测试小米路由器,将定时WiFi开关重新实现,并且在R10上跑通,但R13无法启动wifi + +5.14 +调试R13,解决R13无法启动wifi,与阿里那边了解,对接参数 +熟悉sip push,熟悉相关代码 + +5.15 +实现启动、关闭、修改定时wifi开关等相关函数 +熟悉twisted httpserver的代码 + + + +5.16-5.22 + +5.16 + 调试watchcat代码,fix app 丢包问题 + 分析麦工提到的双开 重启WiFi开关 & 取消定时开关的情况 + 安装无线网卡linux版本驱动 + +5.18 + 调试sip push + grub坏掉,修复 + 添加定时WiFi开关的多种时间段的特殊处理代码 + 定时wifi对接,发现具体需求有出入,须改写代码 + +5.19 + 研究麦工提的crontab方案 + 熟悉crontab、at命令实现定时任务 + 研究luci如何将UTC时间字符串解析为时间戳 + +5.20 + watchcat代码调试 + 调试r13中继netgear路由器,中级成功后,app界面没搜到魅族路由器bug + +5.21 + 自己写一个socket客户端测试watchcat,调试bug + 解决r13中继netgear路由器,中级成功后,app界面没搜到魅族路由器bug +测试watchcat代码 + +5.22 + 与占勇联调r13中继netgear路由器,bug fix +整理代码,梳理一周学到的代码 / 命令 + + + + + + +6.1-6.5 + +6.1 + 测试 & fix upgrade bug + +6.2 + 改进优化smart wifi,使之无tmp文件 + shell脚本学习,试着将lua函数中用到的shell命令封装进.sh中 + sed命令学习 + +6.3 + 了解squid/nginx代理服务器模块 + 测试阿里小智相关api + +6.4 + 阅读squid/nginx相关书籍,了解实现原理 + 查找/阅读nginx http模块的proxy代码 + +6.5 + 网上搜索tiny server/proxy server实现代码 + + + + + + + +6.8-6.12 + +6.9 + 了解/调试mongoose代码结构及内部实现api + +6.10 + github上看相关http download的代码,了解其实现原理 + 试着整合http proxy & http download + +6.11 + 整合/调试http proxy & http download,调试成功 + +6.12 + 意识到用c代码实现周期太长,试着用python实现原型 + 回顾python基础语法 + 调试api getdeviceList + 弄懂twisted connect proxy代码(该代码在twisted的基础上加多了对http connect方法的处理) + 了解libcurl,运行http demo + get到linux terminal的常用快捷键 + +6.13 + 整理一周所学 + 异步非阻塞I/O模型:twisted & node.js,对比,区别,与多线程模型的对比 + git回顾 + +6.14 + 整理一周所学(nginx笔记) + twisted factory ==》抽象工厂与工厂模式的对比 ==》 抽象工厂是否过度设计了呢? + get python细节语法(pass / raise / with ... as ...) + + +6.15-6.20 + +6.15 + 联调api set/get bandwidth + 了解luci外网访问代码流程 + 阅读twisted dave api详解dave@http://krondo.com/?p=1209 + + + + + + + + diff --git a/week-report2.txt b/week-report2.txt index 5fdf729..90d6844 100644 --- a/week-report2.txt +++ b/week-report2.txt @@ -64,9 +64,12 @@ 阅读twisted dave api详解dave@http://krondo.com/?p=1209 4-5 总结twisted常用的类及相关回调函数 - - - +6.17 + getsmartwifiinfo挂了,调试 + 发现传入smart wifi参数格式有问题,亟待与app端沟通 + 调整smart wifi utc时间 + python实现urllib/urllib2 range/文件IO + 调试中继转dhcp bug diff --git a/week-report2.txt~ b/week-report2.txt~ index 5fdf729..90d6844 100644 --- a/week-report2.txt~ +++ b/week-report2.txt~ @@ -64,9 +64,12 @@ 阅读twisted dave api详解dave@http://krondo.com/?p=1209 4-5 总结twisted常用的类及相关回调函数 - - - +6.17 + getsmartwifiinfo挂了,调试 + 发现传入smart wifi参数格式有问题,亟待与app端沟通 + 调整smart wifi utc时间 + python实现urllib/urllib2 range/文件IO + 调试中继转dhcp bug