Luci Page Configuration

Source: Internet
Author: User
Tags lua

Organize a wave of OpenWrt Page Setup method, if there is inappropriate, and then gradually improve. 2016/8/8

----------------------------------------------------------------I'm the split line ~~~~~------------------------------------- ------------

Luci is used to make openwrt pages, and is developed using the LUA scripting language. With the MVC architecture, just write some Lua scripts, and the rest of the openwrt can be done automatically.

	  │ │ └──etc │ │ │ ├──config │ │ │ │  └── profile name
	  │   │   │ ├──INIT.D │ │ │        └── execute init script
	  │   │   └──usr │ │     └──lib
	  │   │       └──lua
	  │   │         └──luci
	  │   │           ├──controller
	  │   │           │└── Lua script name
	  │   │           ├──  model
	  │   │           │└──cbi
	  │ │ │   └──lua script name
	  │   │           ├──view
	  │   │             └──htm file name

As shown in the figure above, several files that need to be added have been marked in red, depending on how the controller is called and the location of the file path, the files under model and view need to be adjusted and deleted accordingly. The steps are as follows:

1th Step:

Create a LUA script under \usr\lib\lua\luci\controller\, with examples of script content as follows:

--[[Module Entry]] 
--Program module name/usr/lib/lua/luci/controller/njitclient.lua module
	("Luci.controller.njitclient", Package.seeall) 
	function Index ()--
	Add a new Module (page) entry--
	entry (path, call target, _ ("Display Name"), display order)-
	-Path Given an array of strings {"admin", "Network", "Njitclient"}, http://ip/cgi-bin/luci/admin/network/njitclient
	--Call target:
	- -Execute the specified method (Action): Call ("Function_name")
	--access to the specified page (views): Template ("Myapp/mymodule") Access/usr/lib/lua/luci/view/ Myapp/mymodule.htm
	-Call the CBI (Configuration Binding Interface) module:cbi ("Myapp/mymodule") Call/usr/lib/lua/luci/ Model/cbi/myapp/mymodule.lua
	-redirect: Alias ("admin", "status", "Overview")
	<span style= "White-space:pre ">	</span>entry ({" admin "," Network "," Njitclient "}, CBI (" Njitclient "), _ (" NJIT Client "),"
	End "


2nd Step:

UCI is the OPENWRT configuration management mechanism that puts configuration into the/etc/config folder. Examples are as follows:

Config login
	option username '
	option password ' option
	ifname ' eth0 '
	option domain '


Section start Syntax: config ' type ' ' name '

Parameter definition syntax: option ' key ' value '

List definition Syntax: List ' collection name ' value '

3rd Step:

Using the CBI in the controller to invoke the Lua script in the Model folder, this step requires the following LUA script to be created under the \USR\LIB\LUA\LUCI\MODEL\CBI folder

--[[Configuration module actual code]] require ("Luci.sys")--mapping and storage of the relationship between the file-M = map ("Profile file name", "Configuration page title", "Configuration page description")--profile file name: Profile stored file name, not
	Contains the path/etc/config/xxx m = Map ("Njitclient", Translate ("NJIT client"), translate ("Configure NJIT 802.11x client.")) --the corresponding section s = m:section (typedsection, "Login", "") in the configuration file--does not allow adding or removing S.addremove = false--does not show the name of the section s.anonym ous = true--Interaction of section in configuration file (create option)--Value (text box)--ListValue (dropdown box)--Flag (selection box) enable = S:option (flag, "Enable" , Translate ("Enable")) name = S:option (value, "username", translate ("username")) pass = s:option (value, "Password", Tran Slate ("Password")) Pass.password = True domain = s:option (Value, "domain", translate ("domain")) ifname = S:option (Listv Alue, "ifname", Translate ("Interfaces")) for K, V in Ipairs (Luci.sys.net.devices ()) does if v ~= "Lo" then Ifname:value (v End end--determine if the "Apply" button is clicked local apply = Luci.http.formvalue ("cbi.apply") if Apply then Io.popen ("/etc/init.d/njitcl ient restart ") End return m


4th step:

After the read and write configuration is complete, how to operate according to the configuration. Edit the appropriate file under/etc/init.d/and add the Execute permission

	#!/bin/sh/etc/rc.common
	start=50
	Run_njit ()
	{
	Local enable
	# get variable value
	# config_get_bool variable name Section name section parameter name
	Config_get_bool enable enable
	
	if [$enable]; then
	local username
	local password
	local domain
	local ifname
	# get variable value
	# config_get variable Name section name section parameter name
	config_get username $ Username
	config_get Password $ password
	config_get domain domain
	config_get ifname $ ifname
	If ["$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 &
	fi
	echo "NJIT Client has started."
	Fi
	}
	start ()
	{
		# read config file
		config_load njitclient
		# traverse the section # Config_ in the configuration file
		foreach Traversal function name section type
		config_foreach run_njit login
	}
	 
	Stop ()
	{
		killall njit-client
		Killall udhcpc
		echo "NJIT Client has stoped."
	}


5th step:

This section is optional and adds the corresponding HTM file under the \usr\lib\lua\luci\view page. Because the sample program does not use the template to access the corresponding HTM script in the Lua script under the controller file, it can be omitted in part.


The above is all the Luci to add the page. The steps are not sequential, but the parameters and calls must be consistent between each part.

Reference documents:

[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




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.