OpenWrt_Luci_Lua/smart_wifi_code/updateWifi.lua

577 lines
18 KiB
Lua
Raw Normal View History

===========================================================
--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 finishedcan 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====================================================
2015-05-25 09:59:40 +00:00
-------------------------./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;
2015-05-25 09:59:40 +00:00
------------------------------------------------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
2015-05-25 09:59:40 +00:00
-----------------------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