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

摘要:
OpenWrt路由的接口配置使用LuCI系统管理。也就是说,语句entryFirewall模块调用action_ Iptables函数:functionaction_ Iptables()ifluci.http。Formvaluethen…endendtemplate用于调用现有的htm模板。模板目录位于lua-luci视图目录中。也就是说,语句条目调用lua-luci-view admin_status\index。Htm文件。其链接目录位于lua-luci模型cbi下。显然,语句条目调用lua-luci模型cbi-admin_status\processs。实现模块的lua——cbi模块包含一系列lua文件,以形成接口元素的组合。cbi模块中的所有控件都需要用lucic.cbi.Map编写。有关常用控件的详细说明,请参阅LuCID文档中的说明。

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

初识Lua——OpenWrt路由界面配置第1张

在此,对其中的目录结构进行介绍:

-目录结构

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文件中封装了所有的控件元素,例如复选框,下拉列表等。

 初识Lua——OpenWrt路由界面配置第2张

 常用控件的具体说明可以参照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——OpenWrt路由界面配置第3张

Lua语言也是这几天才开始接触,了解必然是有局限性的,wayne欢迎大神们的指导,希望能共同促进! 

 


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

免责声明:文章转载自《初识Lua——OpenWrt路由界面配置》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇openssh交叉编译一文梳理JS事件下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

关于 Lua 内存泄漏的检测

前一阵开始和同事一起优化内存,首先是优化 Lua 内存,因为发现每次战斗完后 Lua 内存非常大,从 3M 左右在经过了10次左右的战斗后,会暴增到近 100M,很明显是有内存泄漏。 然后我正式启动该工作,基本思路就是递归遍历内存中所有的数据,表,函数,协程,用户数据,查看未释放和笔误引起的全局变量泄漏;于是通过搜索我参考了以下资料: http://sha...

OpenWrt上搭建纯L2TP服务器[ZT]

转自:http://www.openwrt.pro/post-389.html 纯L2TP(l2tp + ppp,无IPSec) 首先安装xl2tpd软件包 opkg update opkg install xl2tpd 编辑/etc/xl2tpd/xl2tpd.conf,配置l2tp服务器端 [global] port = 1701 auth fi...

openwrt固件支持3G和4G上网卡

http://wiki.openwrt.org/doc/howtobuild/wireless-router-with-a-3g-dongle Building image with support for 3g/4g and usb tethering Preparing build environment First of all, you nee...

[openwrt] 使用ubus实现进程通信

摘自:https://blog.csdn.net/jasonchen_gbd/article/details/45627967 ubus为openwrt平台开发中的进程间通信提供了一个通用的框架。它让进程间通信的实现变得非常简单,并且ubus具有很强的可移植性,可以很方便的移植到其他linux平台上使用。本文描述了ubus的实现原理和整体框架。 ubus源...

Luarocks 安装艰难过程

1.最好新建一个你自己的目录,然后把lua + Luarocks 都安装在这个文件夹下面,方便以后维护 2.安装lua环境 $ wget http://www.lua.org/ftp/lua-5.3.0.tar.gz  $ tar zxvf lua-5.3.0.tar.gz  $ cd lua-5.3.0  $ vi Makefile INSTALL_TO...

openwrt下安装nohup

wget http://d.wrtnode.com/packages/coreutils_8.16-1_ramips_24kec. ipk opkg install coreutils_8.16-1_ramips_24kec.ipk wget http://d.wrtnode.com/packages/coreutils-nohup_8.16-1_ra...