加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-14 22:07:58 所属栏目:大数据 来源:网络整理
导读:OpenWrt路由的界面配置使用LuCI系统管理。 在此,对其中的目录结构进行介绍: - 目录结构 以 status 模块为例进行说明,模块入口文件 status.lua 在目录 lualucicontrolleradmin 下。 function ?index () ??? entry ({ "admin" , ? "status" }, ?alias (

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


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

-目录结构

status模块为例进行说明,模块入口文件status.lua在目录lualucicontrolleradmin下。

function?index()

??? entry({"admin",?"status"},?alias("admin",?"status",?"overview"),?_("Status"),?20).index?=?true

??? entry({"admin",?"overview"},?template("admin_status/index"),?_("Overview"),0)">1)

??? entry({"admin",?"iptables"},?call("action_iptables"),?_("Firewall"),0)">2).leaf?=?true

??? ……

??? entry({"admin",?"processes"},?cbi("admin_status/processes"),?_("Processes"),0)">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",0)">2)

Firewall模块调用了action_iptables函数:

function?action_iptables()

????if?luci.http.formvalue("zero")?then

??? ??? ……

????end

end

?

template用来调用已有的htm模版,模版目录在lualuciview目录下。

即语句entry({"admin",0)">1)

调用了lualuciviewadmin_statusindex.htm文件来显示。

?

cbi语句使用cbi模块,这是使用非常频繁也非常方便的模块,在cbi模块中定义各种控件,Luci系统会自动执行大部分处理工作。其链接目录在lualucimodelcbi下。

显然语句entry({"admin",0)">6)

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读