Campus Network &openwrt (15) Luci Module

Source: Internet
Author: User
Tags lua

When the application is successfully developed as a system service, it is really a good thing to start or stop the service freely at the console restart. But the problem is, if we can not only use the application in the router console, we can also use the OPENWRT Web management interface to start or stop the application and get the status of the application (such as to get the campus network balance and current usage flow, etc.), it is not more convenient and more intuitive. So later I decided to develop a module on the Luci so that we could set the parameters of the application in Luci, restart or stop the application.
Here is a record of the development of the Luci module some of the main points, the specific operation of course, preferably on Luci wiki to learn.
the significance of several important catalogues in the Luci directory (/USR/LIB/LUA/LUCI)
1, Controller,luci on the various tabs corresponding to the module defined in this directory;
2. The CBI files of some modules in Model/cbi,luci, which define the mapping of the Luci module to the UCI configuration file, so that the Luci of the config file in the Web interface is rendered as a selection box, an input box, and so on, so that the click ' Application ' config file will be changed accordingly, see below;
3. The HTML templates in View,luci are here, and the Lua script can also be embedded in the template.

second, the new Luci module.
New Luci module is not difficult, assuming we put the application at the top level, then in the Controller Directory Admin directory under the new application name of the Lua script, following the previous article on the assumption: Cnipclient. Then create a new Cnipclient.lua file and add the following code to the file:

Module ("Luci.controller.admin.cnclient", Package.seeall)

function index ()

end

The first line is the Luci used to ensure that the module's file path is correct and that the module's scope is generated. The index function, however, defines the behavior of clicking on the module.

three, several methods of Luci module's actions
In the above mentioned index function, you need to add the entry function, which defines the URL and action of the top-level options and sub options in the Luci tab, and the following is the prototype:

Entry (path, target, Title=nil, Order=nil)

Path defines the URL, where we fill in {"admin", "cnclient"}, and then in the URL it appears as .../cgi-bin/luci/admin/cnclient,target defines the action that clicks on this option. Title defines this tab, and order indicates the arrangement of the option at the same level, the smaller the number, the higher the priority.
There are four kinds of action, take our application for example, file Controller/cnipclient as follows (the property of the entry in code indicates that the option is a child option) when the leaf is set to true:

module ("Luci.controller.admin.cnclient", Package.seeall) function index () entry ({"Admin",
        "Cnclient"}, alias ("admin", "cnclient", "help"), "Cnclient", "entry" ({"admin", "cnclient", "Help"},
        Template ("Cnclient/help"), "cnclient instructions". Leaf = True Entry ({"admin", "cnclient", "setting"},
        CBI ("cnclient/setting"), "set cnclient parameters". Leaf = True Entry ({"admin", "cnclient", "Restart"}, Call ("Restart_cnclient"), "Restart Cnclient service". Leaf = True End Function restart_cnclient () luci.util.exec (" /etc/init.d/cnclient Restart & ") Luci.http.redirect (Luci.dispatcher.build_url (" admin "," cnclient ")) End 

1, Aliases: Alias function, this is used on the parent option, these parent options have sub options, when we click on the parent option, it is equivalent to click on the path defined by it, here is .../cgi-bin/luci/admin/cnclient/help;
2, Render Template: Template function, parameter is template file path, function will go to find Luci/view (template directory) under the template file, and then render it, here we use to render our Application Help page template;
3, CBI model: CBI function, parameter is CBI file path, The function will go to the LUCI/MODEL/CBI directory to look for the CBI file, and then render the Web page based on the mapping class instance map that is returned by the CBI file (Lua script) associated with the UCI configuration file, and the operation will affect the UCI configuration file changes. Here we associate the CBI model with our application's configuration file (/etc/config/cnipclient) to modify the configuration file on the web;
4, calling function: The call function, which calls functions in the current scope. Here we use to invoke our Restart application function.

Among them, templates have custom template syntax, you can embed Lua scripts, and the CBI has several classes that you need to be familiar with, and Luci's GitHub documents are fairly well written.
  
Display the status information of the application.
We can view the source code of the index.htm template under the Admin_status directory under the View directory. At first there was a <%%> tag that contained a LUA script that Luci to get router information in the background and send that information to the client in JSON format, followed by the JS script, which is the JSON data from the client's receiving server, Use Xhr.poll to constantly update the data on the page, allowing the client to obtain real-time router data. The next step is the HTML code. Of course, it's better to go to the router to view the file than I would in Doha.
Here, we need to write in the Lua script area to get our application status information (login success, balance, etc.), add the div we want to display in the HTML area, and write the code in the JS script area to modify the display of our div using data from the background. So our real-time data can be displayed on our Luci status page. Of course, you can also separate it out of our application's parent option, just add a entry to the index function in Controller/cnipclient.lua.

With the above knowledge, I finally wrote out the Luci interface of our application. To this, my openwrt toss the record to sue a paragraph, later have the time I also can mend a little to perfect. This application took me a lot of time, but I also benefited from it, toss to learn things. Finally, attach the GitHub address of the application: Lua PPPoE version, Python version

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.