Luci页面配置

来源:互联网 发布:免费装修软件app 编辑:程序博客网 时间:2024/06/16 02:41

   整理一波openwrt页面设置的方法,如有不恰当之处,随后再逐步完善。2016/8/8

----------------------------------------------------------------我是分割线~~~~~-------------------------------------------------

    Luci是用来做openwrt页面的,采用lua脚本语言开发。采用MVC架构,只需要写一些lua脚本,剩下的部分openwrt可以自动完成。

  │   │   └── etc  │   │   │    ├── config  │   │   │    │  └── 配置文件名称  │   │   │    ├── init.d  │   │   │        └──执行初始化的脚本  │   │   └── usr  │   │     └── lib  │   │       └── lua  │   │         └── luci  │   │           ├── controller  │   │           │ └── lua脚本名称  │   │           ├──  model  │   │           │ └── cbi  │   │           │   └── lua脚本名称  │   │           ├──view  │   │             └── htm文件名称

       如上图所示,需要添加的几个文件已经采用红色标示出来,根据controller调用的处理方法不同及定位的文件路径不同,model和view下的文件需要作相应的调整及删减。具体步骤如下:

第1步:

     在\usr\lib\lua\luci\controller\下建立一个lua脚本,脚本内容示例如下:

--[[ 模块入口 ]] -- 程序模块名 /usr/lib/lua/luci/controller/njitclient.luamodule("luci.controller.njitclient", package.seeall) function index()-- 添加一个新的模块(Page)入口-- entry(路径, 调用目标, _("显示名称"), 显示顺序)-- 路径 以字符串数组给定 {"admin", "network", "njitclient"} -> http://ip/cgi-bin/luci/admin/network/njitclient-- 调用目标:-- 执行指定方法(Action):call("function_name")-- 访问指定页面(Views):template("myapp/mymodule") 访问/usr/lib/lua/luci/view/myapp/mymodule.htm-- 调用CBI(Configuration Binding Interface) Module:cbi("myapp/mymodule") 调用/usr/lib/lua/luci/model/cbi/myapp/mymodule.lua-- 重定向:alias("admin", "status", "overview")<span style="white-space:pre"></span>entry({"admin", "network", "njitclient"}, cbi("njitclient"), _("NJIT Client"), 100)end


第2步:

     UCI是openwrt的配置管理机制,将配置统一放到/etc/config文件夹下。示例如下:

config loginoption username ''option password ''option ifname 'eth0'option domain ''


Section开始语法:config ‘类型’ ‘名字’

参数定义语法: option ‘键’ ‘值’

列表定义语法:list ‘集合名字’‘值’

第3步:

      在controller里采用cbi调用Model文件夹里的lua脚本,所以此步需要在\usr\lib\lua\luci\model\cbi文件夹下建立如下lua脚本

--[[ 配置模块实际的代码 ]] require("luci.sys")-- 映射与存储文件的关系-- m = Map("配置文件文件名", "配置页面标题", "配置页面说明")-- 配置文件文件名:配置文件存储的文件名,不包含路径 /etc/config/xxxm = Map("njitclient", translate("NJIT Client"), translate("Configure NJIT 802.11x client."))-- 配置文件中对应的sections = m:section(TypedSection, "login", "")-- 不允许增加或删除s.addremove = false-- 不显示Section的名称s.anonymous = true-- 配置文件中section的交互(创建Option)-- Value(文本框)-- ListValue(下拉框)-- Flag(选择框)enable = s:option(Flag, "enable", translate("Enable"))name = s:option(Value, "username", translate("Username"))pass = s:option(Value, "password", translate("Password"))pass.password = truedomain = s:option(Value, "domain", translate("Domain"))ifname = s:option(ListValue, "ifname", translate("Interfaces"))for k, v in ipairs(luci.sys.net.devices()) doif v ~= "lo" thenifname:value(v)endend-- 判断是否点击了“应用”按钮local apply = luci.http.formvalue("cbi.apply")if apply thenio.popen("/etc/init.d/njitclient restart")endreturn m


第4步:

        读写配置完成之后,如何根据配置进行操作?编辑/etc/init.d/下相应的文件,并添加执行权限

#!/bin/sh /etc/rc.commonSTART=50run_njit(){local enable# 获取变量值# config_get_bool 变量名 Section名 Section参数名config_get_bool enable $1 enableif [ $enable ]; thenlocal usernamelocal passwordlocal domainlocal ifname# 获取变量值# config_get 变量名 Section名 Section参数名config_get username $1 usernameconfig_get password $1 passwordconfig_get domain $1 domainconfig_get ifname $1 ifnameif [ "$domain" != "" ]; then<span style="white-space:pre"></span>njit-client $username@$domain $password $ifname &else<span style="white-space:pre"></span>njit-client $username $password $ifname &fiecho "NJIT Client has started."fi}start(){# 读取配置文件config_load njitclient# 遍历配置文件中的Section# config_foreach 遍历函数名 Section类型config_foreach run_njit login} stop(){killall njit-clientkillall udhcpcecho "NJIT Client has stoped."}


第5步:

      此部分可选,在\usr\lib\lua\luci\view页面下添加相应的htm文件。由于示例程序在controller文件下的lua脚本中并没有使用template访问相应的htm脚本,故此部分可以略去。


     以上就是全部的luci添加页面的操作。各步骤不分先后,但每部分之间参数及调用必须保持一致。

参考文献:

[1]http://jphome.github.io/blog/2014/08/03/luci_add_page.html

[2]http://www.cnblogs.com/chengyi818/p/5094443.html

[3]http://www.leiphone.com/news/201406/diy-a-smart-router-topic-system-configuration.html

[4]https://htmlpreview.github.io/?https://github.com/openwrt/luci/blob/master/documentation/api/index.html






0 0
原创粉丝点击