好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

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

初识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 主要分为三类: call , template 和 cbi 。

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/  
本文版权归作者和博客园共有,欢迎转载,但未经作者同意请保留此段声明,且在文章页面明显位置给出原文链接。 

作者: Leo_wl

    

出处: http://www.cnblogs.com/Leo_wl/

    

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

版权信息

查看更多关于初识Lua——OpenWrt路由界面配置的详细内容...

  阅读:51次