初識Lua-OpenWrt路由界面配置


OpenWrt路由的界面配置使用LuCI系統管理。

在此,對其中的目錄結構進行介紹:

-目錄結構

status模塊為例進行說明,模塊入口文件status.lua在目錄lua\luci\controller\admin下。

function index()

    entry({"admin", "status"}, alias("admin", "status", "overview"), _("Status"), 20).index = true

    entry({"admin", "status", "overview"}, template("admin_status/index"), _("Overview"), 1)

    entry({"admin", "status", "iptables"}, call("action_iptables"), _("Firewall"), 2).leaf = true

    ……

    entry({"admin", "status", "processes"}, cbi("admin_status/processes"), _("Processes"), 6)

……

end

 

index()函數中,使用entry函數來完成每個模塊函數的注冊,官方說明文檔如下:

 entry(path, target, title=nil, order=nil)

  • path is a table that describes the position in the dispatching tree: For example a path of {"foo", "bar", "baz"} would insert your node in foo.bar.baz.
  • target describes the action that will be taken when a user requests the node. There are several predefined ones of which the 3 most important (call, template, cbi) are described later on on this page
  • title defines the title that will be visible to the user in the menu (optional)
  • order is a number with which nodes on the same level will be sorted in the menu (optional)

 其中target主要分為三類:calltemplatecbi

call用來調用函數。

即語句entry({"admin", "status", "iptables"}, call("action_iptables"), _("Firewall"), 2)

Firewall模塊調用了action_iptables函數:

function action_iptables()

    if luci.http.formvalue("zero") then

        ……

    end

end

 

template用來調用已有的htm模版,模版目錄在lua\luci\view目錄下。

即語句entry({"admin", "status", "overview"}, template("admin_status/index"), _("Overview"), 1)

調用了lua\luci\view\admin_status\index.htm文件來顯示。

 

cbi語句使用cbi模塊,這是使用非常頻繁也非常方便的模塊,在cbi模塊中定義各種控件,Luci系統會自動執行大部分處理工作。其鏈接目錄在lua\luci\model\cbi下。

顯然語句entry({"admin", "status", "processes"}, cbi("admin_status/processes"), _("Processes"), 6)

調用lua\luci\model\cbi\admin_status\processes.lua來實現模塊。

 

這樣我們可以發現,cbi模塊可能是核心功能模塊了,我們看看這個模塊的使用。

-cbi模塊

cbi模塊包含的一系列lua文件構成界面元素的組合,所有cbi模塊中的控件都需要寫在luci.cbi.Map中。

cbi.lua文件中封裝了所有的控件元素,例如復選框,下拉列表等。

 

 常用控件的具體說明可以參照LuCI Documentation中的描述。

http://luci.subsignal.org/trac/wiki/Documentation/CBI

 

在此簡單地舉例Button來說明其應用

button = s:option(Button, "_button", "Button")

button.inputtitle = translate("exec")

button.inputstyle = "apply"

 

function button.write(self, section, value)

        AbstractValue.write(self, section, value)

        local listvalue = luci.fs.readfile("/etc/saveValue")

        os.execute("touch /etc/testfile%s" %{listvalue})

        self.inputtitle = translate("haha")

end

 

按鈕的響應過程為:從saveValue文件中獲取內容,然后以獲取到的字符串命名創建新文件。

截一張學習測試界面時的圖:

 

Lua語言也是這幾天才開始接觸,了解必然是有局限性的,wayne歡迎大神們的指導,希望能共同促進! 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM