Openwrt Luci界面開(kāi)發(fā)
總結(jié):
make menuconfig 選擇的的luci:
Luci--- >Collections--->luci
源碼位置::
\192.168.1.223\share\openwrt\openwrt_1\feeds\luci\modules\luci-mod-admin-full\luasrc
Openwrt已經(jīng)提供了一個(gè)很強(qiáng)大的web管理界面Luci李请,可以方便的管理路由器砚尽。我們?cè)陂_(kāi)發(fā)智能路由器時(shí)蚀浆,一般就需要在OpenWrt的WEB界面增加內(nèi)容。
1.Luci簡(jiǎn)介
LuCI是OpenWrt上的Web管理界面转唉,LuCI采用了MVC三層架構(gòu),使用Lua腳本開(kāi)發(fā),所以開(kāi)發(fā)LuCI的配置界面不需要編輯任何的Html代碼控妻,除非想自己?jiǎn)为?dú)去創(chuàng)建網(wǎng)頁(yè)(View層),否則我們基本上只需要修改Model層就可以了揭绑。
2. 添加選項(xiàng)Test
接下來(lái)介紹如何在“System”添加Test選項(xiàng)卡弓候。
在文件系統(tǒng)目錄,源碼路徑(\feeds\luci\modules\luci-mod-admin-full\luasrc\view\admin_mqtt)
“/usr/lib/lua/luci/controller/admin”下創(chuàng)建test.lua文件他匪,文件內(nèi)容如下:
module("luci.controller.admin.test", package.seeall)
function index()
entry({"admin", "test"}, alias("admin", "test", "test"), _("Test1"), 30).index = true
entry({"admin", "test", "control"}, cbi("admin_test/control"), _("ControlTest"), 1)
end
/etc/init.d/uhttpd restart 重啟http服務(wù)之后菇存,刷新界面之后(有時(shí)候因?yàn)榫彺妫缑鏇](méi)有及時(shí)變化邦蜜,rm -rf /tmp/luci-* 刪除緩存就可以了)依鸥,界面變成
test.lua中 entry表示添加一個(gè)新的模塊入口,entry的定義如下悼沈,其中后兩項(xiàng)都是可以為空:
entry(path, target, title=nil, order=nil)
“path”是訪問(wèn)的路徑贱迟,路徑是按字符串?dāng)?shù)組給定的,比如路徑按如下方式寫(xiě)“{"admin", "test", "control"}”絮供,那么就可以在瀏覽器里訪問(wèn)“http://192.168.1.1/cgi-bin/luci/admin/test/control”來(lái)訪問(wèn)這個(gè)腳本衣吠。其中的“admin”表示為管理員添加腳本,“test”即為一級(jí)菜單名壤靶,“control”為菜單項(xiàng)名缚俏。系統(tǒng)會(huì)自動(dòng)在對(duì)應(yīng)的菜單中生成菜單項(xiàng)。比如想在“System”菜單下創(chuàng)建一個(gè)菜單項(xiàng)萍肆,那么一級(jí)菜單名可以寫(xiě)為“system”袍榆。
“target”為調(diào)用目標(biāo),調(diào)用目標(biāo)分為三種塘揣,分別是執(zhí)行指定方法(Action)包雀、訪問(wèn)指定頁(yè)面(Views)以及調(diào)用CBI Module。
第一種可以直接調(diào)用指定的函數(shù)亲铡,比如點(diǎn)擊菜單項(xiàng)就直接重啟路由器等等才写,比如寫(xiě)為“call("function_name")”葡兑,然后在該lua文件下編寫(xiě)名為function_name的函數(shù)就可以調(diào)用了。
第二種可以訪問(wèn)指定的頁(yè)面赞草,比如寫(xiě)為“template("myapp/mymodule")”就可/usr/lib/lua/luci/model/cbi以調(diào)用/usr/lib/lua/luci/view/myapp/mymodule.htm文件了讹堤。
第三種主要應(yīng)用在配置界面,比如寫(xiě)為“cbi("myapp/mymodule")”就可以調(diào)用/usr/lib/lua/luci/model/cbi/myapp/mymodule.lua文件了厨疙。
title和order是針對(duì)管理員菜單的洲守,其中的title即是顯示在網(wǎng)頁(yè)上的內(nèi)容。這里我們創(chuàng)建“/usr/lib/lua/luci/controller/admin/test.lua”文件沾凄,定義我們的入口為“test”梗醇。
3添加cbi腳本
由test.lua中cbi指示的目錄,在“/usr/lib/lua/luci/model/cbi/admin_test”目錄下有control.lua腳本撒蟀。
1.在/usr/lib/lua/luci/model/cbi在新建admin_test目錄
2.在admin_test中新建control.lua文件叙谨,添加內(nèi)容
require("luci.sys")
require("luci.sys.zoneinfo")
require("luci.tools.webadmin")
require("luci.fs")
require("luci.config")
local m, s, o
m = Map("test", translate("Test"), translate("This is simple test."))
m:chain("luci")
s = m:section(TypedSection, "controlboard", translate("Control Board"))
s.anonymous = true
s.addremove = false
s:tab("led", translate("Control LED"))
s:tab("beep", translate("Control Beep"))
--s:tab("adc", translate("Control Adc"))
--
-- LED
--
o = s:taboption("led", ListValue, "lednum", translate("LED NUM:"))
o.default = 0
o.datatype = "uinteger"
o:value(0, translate("LED0"))
o:value(1, translate("LED1"))
o:value(2, translate("LED2"))
o = s:taboption("led", ListValue, "ledstatus", translate("LED STATUS:"))
o.default = 1 --off status
o.datatype = "uinteger"
o:value(0, translate("LED ON"))
o:value(1, translate("LED OFF"))
--
-- BEEP
--
o = s:taboption("beep", ListValue, "beepstatus", translate("BEEP STATUS:"))
o.default = 1 --off status
o.datatype = "uinteger"
o:value(0, translate("ON"))
o:value(1, translate("OFF"))
o = s:taboption("beep", Value, "beepfreq", translate("BEEP FREQ:"))
o.datatype = "uinteger"
該腳本表示讀取/etc/config下的test文件,因此我們需要在/etc/config/中添加test文件保屯。并在文件中添加:config controlboard
重啟uhttpd服務(wù)后手负,刷新后界面為: