Openwrt LuCI CBI(二)

2024-02-05 02:20
文章标签 openwrt luci cbi

本文主要是介绍Openwrt LuCI CBI(二),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

1 、CBI控件类型汇总

2 、CBI常用控件用法详解

2.1 class Map (config, title, description)

2.2 class NamedSection(name, type, title, description)

2.3 class TypedSection (type, title, description)

2.4 class Value (option, title, description)

2.5 class ListValue (option, title, description)

2.6 class Flag (option, title, description)

2.7 class MultiValue (option, title, description)

2.8 class StaticList (option, title, description)

2.9 class DynamicList (option, title, description)

2.10 class DummyValue (option, title, description)

2.11 class TextValue (option, title, description)

2.12 class Button (option, title, description)

2.13 class tab(tabname, title)

3 、实例操作

3.1 示例

3.2 网上实例参考

4 、结尾

4.1 总结&反思

4.2 资料参考


1 、CBI控件类型汇总

  • CBI模型是描述UCI配置文件结构的Lua文件,并且CBI解析器将lua文件转为HTML呈现给用户 。
  • 所有 CBI 模型文件必须返回类型为 luci.cbi.Map 的对象。
  • CBI 模型文件的范围由 luci.cbi 模块的内容和 luci.i18n 的转换函数自动扩展。
名称描述继承自模板
NamedSectionA fixed configuration section defined by its nameNamedSection = class(AbstractSection)cbi/nsection
TypedSectionA (set of) configuration section(s) defined by the typeTypedSection = class(AbstractSection)cbi/tsection
NodeNode pseudo abstract classNode = class()cbi/node
TemplateA simple template elementTemplate = class(Node)
MapA map describing a configuration fileMap = class(Node)cbi/map
CompoundContainerCompound = class(Node)cbi/compound
DelegatorNode controllerDelegator = class(Node)cbi/delegator
SimpleFormA Simple non-UCI formSimpleForm = class(Node)cbi/simpleform
FormForm = class(SimpleForm)
AbstractSectionAbstractSection = class(Node)
SimpleSectionSimpleSection = class(AbstractSection)cbi/nullsection
TableTable = class(AbstractSection)cbi/tblsection
AbstractValueAn abstract Value TypeAbstractValue = class(Node)
ValueA one-line valueValue = class(AbstractValue)cbi/value
DummyValueThis does nothing except being thereDummyValue = class(AbstractValue)cbi/dvalue
FlagA flag being enabled or disabledFlag = class(AbstractValue)cbi/fvalue
ListValueA one-line value predefined in a listListValue = class(AbstractValue)cbi/lvalue
MultiValueMultiple delimited valuesMultiValue = class(AbstractValue)cbi/mvalue
StaticListStaticList = class(MultiValue)
DynamicListDynamicList = class(AbstractValue)cbi/dynlist
TextValueA multi-line valueTextValue = class(AbstractValue)cbi/tvalue
ButtonButton = class(AbstractValue)cbi/button
FileUploadFileUpload = class(AbstractValue)cbi/upload
FileBrowserFileBrowser = class(AbstractValue)cbi/browser
PageA simple nodePage = class(Node)

2 、CBI常用控件用法详解

2.1 class Map (config, title, description)
  • This is the root object of the model.。
  • 模型的根对象

参数说明:

  • config: configuration filename to be mapped, see UCI documentation and the files in /etc/config
  • UCI详解 见 Openwrt:LuCI之UCI(三)
  • title: 页面显示名称
  • description: 页面显示详细描述

方法说明:

  • function :section(sectionclass, …)
  • sectionclass: a class object of the section。// section的类对象
  • additional parameters passed to the constructor of the section class。//传递给section类的构造函数的其他参数
  • 补充

section(TypedSection, type, title, description) section(NamedSection, name, type, title, description) section 对象有一些属性如下:

template: html 模板, 默认为"cbi/tsection" addremove: 是否可以增加和删除, 默认为 false anonymous: 是否为匿名 section, 默认为 false

  • 网页图例
2.2 class NamedSection(name, type, title, description)
  • An object describing an UCI section selected by the name. //根据type来解析一个UCI section
  • To instantiate use: Map:section(NamedSection, "name", "type", "title", "description")

参数说明:

  • name: UCI section名字, config type section
  • type: UCI section类型, config type section
  • type可选:Value、 DynamicList、 Flag、 ListValue、TextValue、MultiValue、DummyValue、StaticList、Button …
  • title: 页面显示名称
  • description: 页面显示详细描述

对象属性:

  • property.addremove = false
  • Allows the user to remove and recreate the configuration section. //允许用户删除并重新创建配置section。
  • property.anonymous = true
  • true 页面不显示section名字
  • false 页面显示section名字
  • property.dynamic = false
  • ture 将section 标记为动态
  • Dynamic sections can contain an undefinded number of completely userdefined options. //将此部分标记为动态。动态section可以包含未找到的完全用户定义的选项数。
  • property.optional = true
  • 解析可选的options

方法说明:

  • function :option(optionclass, …)
  • Creates a new option //创建新选项
  • ptionclass: a class object of the section //section的类对象
  • additional parameters passed to the constructor of the option class //传递给选项类的构造函数的其他参数
  • 补充

option(type, name, title, description) option对象有一些属性如下:

rmempty:如果为空值则删除该选项,默认为 true default: 默认值, 如果为空值,则设置为该默认值 datatype: 限制输入类型。 例如"and(uinteger,min(1))“限制输入无符号整形而且大于 0, “ip4addr"限制 输入 IPv4 地址, “port"限制输入类型为端口,更多参考/usr/lib/lua/luci/cbi/datatypes.lua placeholder: 占位符( html5 才支持)

2.3 class TypedSection (type, title, description)
  • An object describing a group of UCI sections selected by their type. // 通过类型type选择来描述一组 UCI section的对象。
  • To instantiate use: Map:section(TypedSection, "type", "title", "description")

参数说明:

  • type: UCI section类型, config type section
  • type可选:Value、 DynamicList、 Flag、 ListValue、TextValue、MultiValue、DummyValue、StaticList、Button …
  • title: 页面显示名称
  • description: 页面显示详细描述

对象属性:

  • property.addremove = false 同NameSection
  • property.anonymous = true 同NameSection
  • property.template = “cbi/template” 设置此页面的模块

方法说明:

  • function :depends(key, value)
  • Only show this option field if another option key is set to value in the same section. If you call this function several times the dependencies will be linked with “or”// 仅当另一个选项键在同一section中设置为值时,才显示此选项字段。如果多次调用此函数,则依赖项将链接为 [或]
  • function .filter(self, section) -abstract-
  • You can override this function to filter certain sections that will not be parsed. The filter function will be called for every section that should be parsed and returns nil for sections that should be filtered. For all other sections it should return the section name as given in the second parameter. //您可以重写此函数以筛选某些不会解析的部分。对于应分析的每个section,将调用筛选器函数,对于应筛选的section,将返回 nil。对于所有其他section,它应返回第二个参数中给出的section名称。
  • function :option(optionclass, …)
  • 网页图例
2.4 class Value (option, title, description)
  • An object describing an option in a section of a UCI File. Creates a standard text field in the formular.//描述 UCI 文件部分中的选项的对象。在公式中创建标准文本框。(单行文本框)
  • To instantiate use: NamedSection:option(Value, "option", "title", "description") or TypedSection:option(Value, "option", "title", "description")

参数说明:

  • option: UCI option name
  • title: 页面显示名称
  • description: 页面显示详细描述

对象属性:

  • property .default = nil 文本框中缺省的值
  • property .maxlength = nil
  • The maximum input length (of chars) of the value //值的最大输入长度(字符)
  • property .optional = false
  • Marks this option as optional, implies .rmempty = true //将此选项标记为可选, .rmempty = true
  • property .rmempty = true
  • Removes this option from the configuration file when the user enters an empty value //当用户输入空值时,从配置文件中删除此选项
  • property .size = nil
  • The maximum number of chars displayed by form field //窗体字段显示的最大字符数
  • property .password = false

密码输入框

方法说明:

  • function :depends(key, value)
  • Only show this option field if another option key is set to value in the same section. If you call this function several times the dependencies will be linked with “or”
  • function :value(key, value)
  • Convert this text field into a combobox if possible and add a selection option. //如果可能,将此文本字段转换为组合框,并添加选择选项。
  • 网页图例​
2.5 class ListValue (option, title, description)
  • An object describing an option in a section of a UCI File.Creates a list box or list of radio (for selecting one of many choices) in the formular. //描述 UCI File 部分中选项的对象。在公式中创建一个列表框或单选列表(用于选择多种选项之一)(即下拉列表)。
  • To instantiate use: NamedSection:option(ListValue, "option", "title", "description") or TypedSection:option(ListValue, "option", "title", "description")

参数说明:

  • option: UCI option name
  • title: 页面显示名称
  • description: 页面显示详细描述

对象属性:

  • property .default = nil 文本框中缺省的值
  • property .widget = “select”
  • select shows a selection list, radio shows a list of radio buttons inside form //select 显示选择列表,radio 显示表单内单选按钮的列表
  • property .optional = false
  • property .rmempty = true
  • property .size = nil
  • The maximum number of chars displayed by form field

方法说明:

  • function :depends(key, value)
  • function :value(key, value)
  • Adds an entry to the selection list.//将条目添加到选择列表中。
  • 网页图例

2.6 class Flag (option, title, description)
  • An object describing an option with two possible values in a section of a UCI File. Creates a checkbox field in the formular. //描述在 UCI 文件部分中具有两个可能值的选项的对象。在公式中创建复选框。
  • To instantiate use: NamedSection:option(Flag, "option", "title", "description")or TypedSection:option(Flag, "option", "title", "description")

参数说明:

  • option: UCI option name
  • title: 页面显示名称
  • description: 页面显示详细描述

对象属性:

  • property .default = nil The default value
  • property .disabled = 0
  • the value that should be set if the checkbox is unchecked //取消选中复选框时 应设置的值
  • property .enabled = 1
  • the value that should be set if the checkbox is checked //选中复选框时 应设置的值
  • property .optional = false
  • property .rmempty = true

方法说明:

  • function :depends (key, value)
  • 网页图例

2.7 class MultiValue (option, title, description)
  • An object describing an option in a section of a UCI File.Creates a list of checkboxed or a multiselectable list as form fields. //描述 UCI 文件部分中的选项的对象。创建复选框列表或多选列表(非下拉列表)。
  • To instantiate use: NamedSection:option(MultiValue, “option”, “title”, “description”) or TypedSection:option(MultiValue, “option”, “title”, “description”)

参数说明:

  • option: UCI option name
  • title: 页面显示名称
  • description: 页面显示详细描述

对象属性:

  • property .widget = “checkbox”
  • select shows a selection list, checkbox shows a list of checkboxes inside form //选中显示选择列表,复选框显示表单内的复选框列表
  • property .delimiter = " "

-The string which will be used to delimit the values inside stored option //用于分隔存储选项内的值的字符串

  • property .default = nil
  • property .optional = false
  • property .rmempty = true
  • property .size = nil

-The size of the form field (only used if property .widget = “select”) //窗体字段的大小(仅当属性 .widget = “选择"时才使用)

方法说明:

  • function :depends (key, value)
  • function :value(key, value)
  • Adds an entry to the list
  • 网页图例

2.8 class StaticList (option, title, description)
  • Similar to the MultiValue, but stores selected Values into a UCI list instead of a character-separated option. //与MultiValue类似,但将所选值存储到 UCI list中,而不是字符分隔option中。
  • (这里可能会有疑问,请看下面操作,当在网页对StaticList和MultiValue 选中四个选项时,保存并应用,查看UCI 中配置文件就可以明白。MultiValue 对应的值存储在option,而StaticList对应的值分别存储在list中)​
  • 网页图例
2.9 class DynamicList (option, title, description)
  • A extensible list of user-defined values. Stores Values into a UCI list //用户定义值的可扩展列表。将值存储到 UCI list中
  • 网页图例​
2.10 class DummyValue (option, title, description)
  • Creates a readonly text in the form. It writes no data to UCI!//在窗体中创建只读文本。它不写入任何数据到UCI!
  • To instantiate use: NamedSection:option(DummyValue, "option", "title", "description")or TypedSection:option(DummyValue, "option", "title", "description")

参数说明:

  • option: UCI option name
  • title: 页面显示名称
  • description: 页面显示详细描述

方法说明:

  • function :depends (key, value)
  • 网页图例

2.11 class TextValue (option, title, description)
  • An object describing a multi-line textbox in a section in a non-UCI form.// 描述非 UCI 窗体中节中多行文本框的对象。
  • 注:与Value的区别就是TextValue可以多行显示,而Value只能单行显示。
  • 网页图例
2.12 class Button (option, title, description)
  • An object describing a Button in a section in a non-UCI form.//描述非 UCI 窗体中节中的按钮的对象。

属性说明:同Value

  • .inputstyle = nil 按钮样式 apply, reset

方法说明:同Value

  • 网页图例

2.13 class tab(tabname, title)
  • 调用 section 的 tab 函数创建一个名称为 tabname,标题为 title 的 Tab 标签。
  • 对应的 option 则使用 taboption s:taboption(tabname, type, name, title) 在指定的 tabname 下创建一个 option。
  • s为section 对象,与之前的控件不同,一个section里面如果有tab控件,会将其他(如Value)控件覆盖掉,所以,要想创建tab选项,必须另外再创建section。
b = m:section(TypedSection, "typedsection2_title", "typedsection2 description")
b:tab("tab1", "Tab1");
b:tab("tab2", "Tab2");
b1=b:taboption("tab1", Value, "tab1_option1", "tab1 Option1");
b2=b:taboption("tab2", Value, "tab2_option2", "tab2 Option2");
  • 网页图例​

3 、实例操作

3.1 示例
  • (1)网页整体图例
  • (2) lua 文件中CBI控件代码
m = Map("test_file","map title" ,translate("map descrption")) -- cbi_file is the config file in /etc/configa = m:section(TypedSection, "typedsection1_title", "typedsection1 description")  -- info is the section called info in cbi_filea1 = a:option(Value, "Value_option_name","Value option title", translate("Value option description"));a2 = a:option(TextValue, "TextValue_option_name","TextValue option title", "TextValue option description");a3 = a:option(MultiValue , "MultiValue_option_name","MultiValue  option title", "MultiValue  option description");
a3:value("0", translate("value0"));
a3:value("1", translate("value1"));
a3:value("2", translate("value2"));
a3:value("3", translate("value3"));a4 = a:option(DummyValue , "DummyValue_option_name","DummyValue option title", "DummyValue option description");a5 = a:option(ListValue, "Listvalue_option_name","ListValue option title", "ListValue option description");
a5:value("0", translate("value0"));
a5:value("1", translate("value1"));
a5:value("2", translate("value2"));
a5:value("3", translate("value3"));a6 = a:option(DynamicList, "DynamicList_option_name","Dynamiclist option title", "DynamicList option description");a7 = a:option(StaticList, "StaticList_option_name","StaticList option title", "StaticList option description");
a7:value("0", translate("value0"));
a7:value("1", translate("value1"));
a7:value("2", translate("value2"));
a7:value("3", translate("value3"));a8 = a:option(Flag, "Flag_option_name","Flag option title", "Flag option description");a9 = a:option(Button, "Button_option_name","Button option title", "Button option description");b = m:section(TypedSection, "typedsection2_title", "typedsection2 description")
b:tab("tab1", "Tab1");
b:tab("tab2", "Tab2");
b1=b:taboption("tab1", Value, "tab1_option1", "tab1 Option1");
b2=b:taboption("tab2", Value, "tab2_option2", "tab2 Option2");return m	
  • (3)UCI 文件中配置信息
config typedsection1_title 'typedsection1'option Value_option_name 'Value'option TextValue_option_name 'TextValue'option DummyValue_option_name 'DummyValue'option Listvalue_option_name '1'list DynamicList_option_name 'DynamicList'list DynamicList_option_name '123123'option Flag_option_name '1'option MultiValue_option_name '0 1 2 3'list StaticList_option_name '0'list StaticList_option_name '1'list StaticList_option_name '2'list StaticList_option_name '3'config typedsection2_title 'typedsection2'option tab1_option1 '1'option tab2_option2 '2'
3.2 网上实例参考

例程详情参考 ModulesHowTo · openwrt/luci Wiki · GitHub

 1 m = Map("network", "Network") -- We want to edit the uci config file /etc/config/network 2 s = m:section(TypedSection, "interface", "Interfaces") -- Especially the "interface"-sections3 s.addremove = true -- Allow the user to create and remove the interfaces4 function s:filter(value)  5      return value ~= "loopback" and value -- Don't touch loopback6 end s:depends("proto", "static") -- Only show thosewith"static"7 s:depends("proto", "dhcp") -- or "dhcp" as protocol and leave PPPoE and PPTP alone 8 9 p = s:option(ListValue, "proto", "Protocol") -- Creates an element list (select box)
10 p:value("static", "static") -- Key and value pairs
11 p:value("dhcp", "DHCP")
12 p.default = "static" 
13 
14 s:option(Value, "ifname", "interface", "the physical interface to be used") -- This will give a simple textbox 
15 
16 s:option(Value, "ipaddr", translate("IP Address")) -- Ja, das ist eine i18n-Funktion ;-) 
17 
18 s:option(Value, "netmask", "Netmask"):depends("proto", "static") -- You may remember this "depends" function from above 
19 
20 mtu = s:option(Value, "mtu", "MTU")
21 mtu.optional = true -- This one is very optional 
22 dns = s:option(Value, "dns", "DNS-Server")
23 dns:depends("proto", "static")
24 dns.optional = true
25 function dns:validate(value) -- Now, that's nifty, eh?
26     return value:match("[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+") -- Returns nil if it doesn't match otherwise returns match
27 end 
28 
29 gw = s:option(Value, "gateway", "Gateway")
30 gw:depends("proto", "static")
31 gw.rmempty = true -- Remove entry if it is empty 
32 
33 return m -- Returns the map
  • 更多 CBI 例程可以参考 openwrt源码/feeds/luci/modules/xxx模块/luasrc/model/cbi/ 目录下的相关文件,结合网页界面去阅读相关源码,学习提高。

4 、结尾

4.1 总结&反思
  • TypedSection与NamedSection的运用场景与区别?
  • Openwrt源码中提到的SimpleForm的效果是如何的?
  • 每个控件属性对每个控件的影响是怎么样,效果如何?哪些属性是所有控件共同的,哪些属性是独有?
  • 如何重构控件方法?
  • 控件未列出的属性或方法在哪些途径能获取到?
4.2 资料参考

文档参考: CBI · openwrt/luci Wiki · GitHub 博客参考:https://blog.csdn.net/qq_21949217/article/details/44340261

这篇关于Openwrt LuCI CBI(二)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/679497

相关文章

openwrt的旁路模式无法访问国内网站

防火墙: 常规设置-> 区域:  lan-> wan :编辑 IP 动态伪装:勾选

【OpenWrt(2)】编译OpenWrt 的SDK,以linksys e8450 的MT7622 CPU为例

资源 参考 https://downloads.openwrt.org/releases/ 文章目录 资源依赖下载 SDK查询 CPU 信号 解压使用 feed 编译后台运行 依赖 apt-get updateapt-get install subversion build-essential libncurses5-dev zlib1g-dev gawk git ccac

【OpenWrt(3)】内网搭建iperf3测速服务器

下载的iperf3 网站:https://iperf.fr/iperf-download.php Window地址:https://github.com/ar51an/iperf3-win-builds 安卓:https://gitee.com/hiyanyx/magic-i-perf 文章目录 下载的iperf3Windows 服务器启动安卓客户端启动参考 Windows 服

《OpenWrt开发笔记》第9章 开发板和Windows、Ubuntu互传文件

【注1】本文是基于Ubuntu12.04+Openwrt15.05开发的教程。 【注2】Openwrt17.01源码中未添加该功能,有兴趣的朋友可自行查找相关资料。 9.1添加SSH服务器 一说到开发板和Windows之间互传文件,大家肯定马上就会想到通过SSH,但是大家最终发现,无论我们通过什么软件,总是不能登录到开发板上。 那么,这会是什么原因呢?因为目前为止,我们开发板上面的系统还没有S

《OpenWrt开发笔记》第8章 无线AP的实现(Ubuntu12.04+Openwrt15.05)

【注】本文是基于Ubuntu12.04+Openwrt15.05开发的教程。 8.1在开发板上面使能Wi-Fi 简单的来讲,我们有时候担心手机流量用得太多,于是在笔记本上打开了 wifi 给手机连接,这时候,笔记本就充当了 Access Point 的角色,无线网络简单的可以看成是无线接入点,终端,无线介质组成,手机接入到笔记本 wifi 情景中,手机是终端,同样的道理,开发板也可以是无线接入

《OpenWrt开发笔记》第7章 添加Web界面

.【注】Openwrt17.01未集成LuCL,本文是基于Openwrt15.05开发的教程。 7.1LuCI介绍 LuCI作为“FFLuCI”诞生于2008年3月份,目的是为OpenWrt固件从Whiterussian到Kamikaze实现快速配置接口。Lua是一个小巧的脚本语言,很容易嵌入其它语言。轻量级LUA语言的官方版本只包括一个精简的核心和最基本的库。这使得LUA体积小、启动速度快,

《OpenWrt开发笔记》第6章 WAN和LAN灵活切换

6.1VLAN介绍 LAN: Local Area Network 的英文简称,即局域网 指在较小的地理范围内,将两台以上的计算机通过传输电缆连接起来,实现资源共享。局域网的传输速度通常在 10Mb/s~1000Mb/s 之间,主干 1000Mb/s、桌面 100Mb/s 是目前的主流技术。局域网的设计通常是针对于一座建筑物内,提高资源和信息的安全性,用于减少管理者的维护操作等。客户/服务系统(

【openwrt-21.02】T750 openwrt-21.02 Linux-5.4.238 input子系统----gpio-keys实现分析

input子系统           输入子系统是由设备驱动层(input driver)、输入核心层(input core)、输入事件处理层(input event handle)组成 input子系统架构图 gpio-keys         gpio-keys是基于input子系统实现的一个通用按键驱动,该驱动也符合linux驱动实现模型,即driver和device分离模型.一

UBUNTU环境下编译的openwrt

UBUNTU环境下编译的openwrt 请执行这个命令以安装编译依赖包:sudo apt-get update 然后再输这个: sudo apt-get install gcc g++ binutils patch bzip2 flex bison make autoconf gettext texinfo unzip sharutils subversion libncurses5-de

OpenWRT 之LuCI 入门开发

LuCI 基础篇 Controller 位于: /usr/lib/lua/luci/controller/ 下——定义模块的入口 Model 位于: /usr/lib/lua/luci/model/cbi/ 下——配置模块实际的代码 第一步:定义模块入口: eg: module("luci.controller.控制器名/路径", package.seeall)function i