Luci页面配置

   整理一波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.lua
	module("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")
		entry({"admin", "network", "njitclient"}, cbi("njitclient"), _("NJIT Client"), 100)
	end


第2步:

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

config login
	option 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/xxx
	m = Map("njitclient", translate("NJIT Client"), translate("Configure NJIT 802.11x client."))
	-- 配置文件中对应的section
	s = 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 = true
	domain = s:option(Value, "domain", translate("Domain"))
	ifname = s:option(ListValue, "ifname", translate("Interfaces"))
	for k, v in ipairs(luci.sys.net.devices()) do
	if v ~= "lo" then
	ifname:value(v)
	end
	end
	-- 判断是否点击了“应用”按钮
	local apply = luci.http.formvalue("cbi.apply")
	if apply then
	io.popen("/etc/init.d/njitclient restart")
	end
	return m



第4步:

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

	#!/bin/sh /etc/rc.common
	START=50
	run_njit()
	{
	local enable
	# 获取变量值
	# config_get_bool 变量名 Section名 Section参数名
	config_get_bool enable $1 enable
	
	if [ $enable ]; then
	local username
	local password
	local domain
	local ifname
	# 获取变量值
	# config_get 变量名 Section名 Section参数名
	config_get username $1 username
	config_get password $1 password
	config_get domain $1 domain
	config_get ifname $1 ifname
	if [ "$domain" != "" ]; then
		njit-client $username@$domain $password $ifname &
	else
		njit-client $username $password $ifname &
	fi
	echo "NJIT Client has started."
	fi
	}
	start()
	{
		# 读取配置文件
		config_load njitclient
		# 遍历配置文件中的Section
		# config_foreach 遍历函数名 Section类型
		config_foreach run_njit login
	}
	 
	stop()
	{
		killall njit-client
		killall udhcpc
		echo "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






你可能感兴趣的:(Openwrt)