• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

初识Lua——OpenWrt路由界面配置

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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欢迎大神们的指导,希望能共同促进! 

 

 

 

http://www.cnblogs.com/dwayne/ 
本文版权归作者和博客园共有,欢迎转载,但未经作者同意请保留此段声明,且在文章页面明显位置给出原文链接。 

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欢迎大神们的指导,希望能共同促进! 

 

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
lua表排序发布时间:2022-07-22
下一篇:
lua日期与时间操作发布时间:2022-07-22
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap