Luci

Control

代碼庫路徑: feeds/lluci/applications/luci-myapp/luasrc/controller/myapp.lua
設備上路徑: /usr/lib/lua/luci/controller/myapp.luahtml

格式:web

module("luci.controller.myapp", package.seeall)

function index()

end

註冊URL:服務器

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

target分別有:app

  • actions
    使用函數直接輸出內容函數

    call("my_fun")ui

  • views
    使用htmllua

    template("mymodule/myview")url

  • cbispa

    cbi("mymodule/mycbi")

例如

function index()
    entry({"admin", "services", "mymodule", "mycbi"}, 
            cbi("mymodule/mycbi"),
            _("CBI")).leaf = true

    entry({"admin", "services", "mymodule", "myview"},
            template("mymodule/myview"),
            _("View")).leaf = true

    entry({"admin", "services", "mymodule", "myfun"},
            call("my_fun"),
            _("FUN")).leaf = true
end

CBI

代碼庫路徑: feeds/luci/applications/luci-myapp/luasrc/model/cbi/myapp.lua
設備上路徑: /usr/lib/lua/luci/model/cbi/myapp.lua

/etc/config/ipsec配置文件以下

config policy tunnel
    option name 'test'
    option enable '1'

建立UCI配置文件ipsec對應的map對象

m = Map("ipsec", translate("IPSec"))

建立UCI type對象

s = m:section(TypedSection, "policy", translate("Polciy"))
s.template = "cbi/tblsection" -- 使用列表模板
s.anonymous = true -- 不顯示section名稱

建立UCI name對象

s = m:section(NamedSection, "tunnel", "policy", "translate("Policy"))

建立UCI option對象

name = s:option(DummyValue, "name", translate("Name"))

enable = s:option(Flag, "enable", translate("Enable))
enable.rmempty = false -- 值爲空時不刪除

CBI類說明

class Map(config, title, description)

參數說明:

  • config: /etc/config/目錄下的UCI文件名
  • title: 頁面顯示名稱
  • description: 頁面顯示詳細描述

方法說明:

:section(sectionclass, ...)


class NamedSection(name, type, title, description)

參數說明:

  • name: UCI section名字, config type section
  • type: UCI section類型, config type section
  • title: 頁面顯示名稱
  • description: 頁面顯示詳細描述

對象屬性:
.addremove = false

此section是否容許刪除或建立
爲true時,頁面會顯示刪除建立按鈕

.anonymous = true

頁面不顯示此secion名字

方法說明:

:option(optionclass, ...)


class TypedSection(type, title, description)

參數說明:

  • type: UCI section類型, config type section
  • title: 頁面顯示名稱
  • description: 頁面顯示詳細描述

對象屬性:

.addremove = false

此section是否容許刪除或建立
爲true時,頁面會顯示刪除建立按鈕

.anonymous = true

頁面不顯示此secion名字

.extedit = luci.dspatcher.build_url("url")

設置此section編輯頁面URL, 頁面顯示編輯按鈕

.template = "cbi/template"

設置此section頁面模板

方法說明:

:option(optionclass, ...)


class Value(option, title, description)

頁面將建立文件輸入框

參數說明:

  • option: UCI option名稱, option enable '1'
  • title: 頁面顯示名稱
  • description: 頁面顯示詳細描述

屬性說明:

.defalut = nil

缺省值

.maxlength = nil

option值最大長度

.rmempty = true

option值爲空時不寫入UCI文件

.size = nil

頁面表單對應位置大小

.optional = false

是否爲可選,爲true可強制頁面輸入

.datatype = nil

指定option值類型,用於輸入合法性檢查

.template = nil

頁面模板

.password = false

密碼輸入框

方法說明:

:depends(key, value)

當key值等於value時,頁面才顯示

function o.validate(self, value)
end

重構option合法性檢查方法,可本身控制option值

function o.formvalue(self, key)
end

重構option從表單獲取值方法

當表單值爲空時返回-,在合法性檢驗時判斷若是value等於-即表示頁面沒有輸入值

function o.formvalue(...)
    return Value.formvalue(...) or "-" 
end
function o.validate(self, value)
    if value == "-" then
        return nil, translate("required fields have no value!")
    end 
    return value
end

functin o.write(self, section, value)
end

重構option寫入UCI文件方法

在用戶輸入的值後追加內容s:

function o.write(self, section, value)
    Value.write(self, section, value .. "s")
end

function o.cfgvalue(self, section)
end

重構option值輸出到頁面方法

頁面只顯示UCI option值開始爲數字部份內容:

function o.cfgvalue(self, section)
    local v = Value.cfgvalue(self, section) 
    if v then
        return string.sub(v, string.find(v, "%d+"))
    else 
        return nil
    end
end

class ListValue(option, title, description)

頁面將建立列表框

參數說明:

  • option: UCI option名稱, option action 'drop'
  • title: 頁面顯示名稱
  • description: 頁面顯示詳細描述

屬性說明:
同class Value

方法說明:
同class Value

:value(ucivalue, showkey)

設置下拉列表顯示與值對應關係

o = s:option(ListValue, "enable", translate("Enable"))
o:value("1", translate("Enable"))
o:value("0", translate("Disable"))

class Flag(option, title, description)

頁面將建立單選框

參數說明:

  • option: UCI option名稱, option enable '0'
  • title: 頁面顯示名稱
  • description: 頁面顯示詳細描述

屬性說明:
同class Value

方法說明:
同class Value


class Button(option, title, description)

頁面將建立按鈕

參數說明:

  • option: UCI option名稱, option enable '0'
  • title: 頁面顯示名稱
  • description: 頁面顯示詳細描述

屬性說明:
同class Value

.inputstyle = nil

按鈕樣式, apply, reset,

方法說明:
同class Value

View

代碼庫路徑: feeds/luci/applications/luci-myapp/luasrc/view/myview.htm
設備上路徑: /usr/lib/lua/luci/view

定義本身的html模板

原有模板路徑: feeds/luci/modules/base/luasrc/view/cbi/目錄

Debug

當設備更新了control文件後,頁面不會顯示,需把/tmp/目錄下luci-indexcache luci-modulecache/ luci-sessions/刪除,web服務器從新創建索引後新頁面才能顯示

相關文章
相關標籤/搜索