zoukankan      html  css  js  c++  java
  • 初识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欢迎大神们的指导,希望能共同促进! 

     


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

  • 相关阅读:
    大三进度条01
    阅读笔记01
    问题账户需求分析
    《人月神话》阅读笔记02
    《人月神话》阅读笔记01
    每周总结(第五周)
    《构建之法》阅读笔记06
    《构建之法》阅读笔记05
    《构建之法》阅读笔记04
    《构建之法》阅读笔记03
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/2455882.html
Copyright © 2011-2022 走看看