当前位置:   article > 正文

【笔记】openwrt - luci开发(资料整理)

luci开发

在这里插入图片描述

# Demo:系统中添加luci界面

在这里插入图片描述
MVC模型,将涉及的三个文件夹列出来:

  • /usr/lib/lua/luci/controller/*
  • /usr/lib/lua/luci/view/*
  • /usr/lib/lua/luci/model/cbi/*

后面我们也将围绕这三个文件夹进行界面开发。

视图 /usr/lib/lua/luci/view/
root@OpenWrt_D2550:~# cat /usr/lib/lua//luci/view/myapp-mymodule/helloworld.htm
<%+header%>
<h1>Hello World</h1>
<%+footer%>
  • 1
  • 2
  • 3
  • 4

配合下面的controller可以实现下面效果
在这里插入图片描述

语法说明

  1. 包含Lua代码: <% code %>
  2. 输出变量和函数值:<% write(value) %> or <%=value%>
  3. 包含模板:
    <% include(templatesName) %>
    <%+templatesName%>
  4. 转换:
    <%= translate(“Text to translate”) %>
    <%:Text to translate%>
  5. 注释:
    <%# comment %>

其他语法跟html和JavaScript一样。

控制 /usr/lib/lua/luci/controller/
mkdir  /usr/lib/lua/luci/controller/myapp 
vim    /usr/lib/lua/luci/controller/myapp/new_tab.lua
  • 1
  • 2

在文件中输入如下内容

module("luci.controller.myapp.new_tab", package.seeall) 
function index()
    entry({"admin", "new_tab"}, firstchild(), translate("cfg"), 1).dependent=false
    entry({"admin", "new_tab", "sn"}, cbi("myapp-mymodule/gateway_sn"), translate("sn"), 2)
    entry({"admin", "new_tab", "hellworld"}, template("myapp-mymodule/helloworld"), _("HelloWorld"), 3)
end
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

api解释

  • module(“luci.controller.myapp.new_tab”, package.seeall)
    定义模块的入口

  • entry(path, target, title=nil, order=nil)
    entry表示添加一个新的选项入口

    • “path” 是访问的路径
      如:“{“admin”, “new_tab”, “hellworld”}”
      路径为:http://192.168.1.252/cgibin/luci/admin/new_tab/hellworld
    • “target”为调用目标
      调用目标分为三种,分别是执行指定方法(Action)、访问指定页面(Views)以及调用
      CBI Module
      1. Action
        可以直接调用指定的函数,比如点击菜单项就直接重启路由器等等,比如写“call(“function_name”)”,然后在该lua文件下编写名为function_name的函数就可以调用了。
      2. View
        可以访问指定的页面,比如写为“template(“myapp-mymodule/helloworld”)”就可以调用/usr/lib/lua/luci/view/myapp-mymodule/helloworld.htm文件了。
      3. CBI Module
        主要应用在配置界面,比如写为“cbi(“myapp/mymodule”)”就可以调用/usr/lib/lua/luci/model/cbi/myapp-mymodule/gateway_sn.lua文件了
      4. 其他一些如 lias 是等同于别的链接,form 和 cgi 对应到 model/cbi 相应的目录下面.
    • title即是显示在网页上的内容,即选项名
      可以用translate(“英文/中文”),也可以用_(“HelloWorld”)方式,还有一种就是利用.po 文件,将英文标识 与 翻译语言 一一对应,例如:
      msgid “Default gateway”
      msgstr “默认网关”
      
      • 1
      • 2
      当title参数为 _("Default gateway")时,如果路由设置为中文显示,则该选项自动显示为 默认网关。
    • order就是菜单项在界面中显示的顺序,由上至下,由左至右,依次递增,可以缺省。
模型 /usr/lib/lua/luci/model/
mkdir /usr/lib/lua/luci/model/cbi/myapp-mymodule
vim   /usr/lib/lua/luci/model/cbi/myapp-mymodule/gateway_sn.lua
  • 1
  • 2

文件中输入如下内容:

m = Map("sn_file", translate("产品序列号")) -- cbi_file is the config file in /etc/config
d = m:section(TypedSection, "gateway_sn")  -- info is the section called info in cbi_file
a = d:option(Value, "sn", translate("序列号"));
a.optional=false; 
a.rmempty = false;  -- name is the option in the cbi_file
return m
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

语法说明:
官方文档 http://luci.subsignal.org/trac/wiki/Documentation/CBI
或详情 见 Openwrt:LuCI之CBI(二)
https://blog.csdn.net/qq_28812525/article/details/103881723

在这里插入图片描述

类数据库 /etc/config/sn_file
vim /etc/config/sn_file
  • 1
config 'gateway_sn' 'sn'
    option 'sn' 'gw2019081300001'
  • 1
  • 2

语法说明:
详情 见 Openwrt:LuCI之UCI(三)
https://blog.csdn.net/qq_28812525/article/details/103902872

在这里插入图片描述


# Demo:在源码中添加界面

创建目录及文件
进入openwrt源码/feeds/luci/applications/,添加如下目录文件结构

lawsssscat@lawsssscat-virtual-machine:~/lede/feeds/luci/applications$ tree luci-app-myapplication/
luci-app-myapplication/
└── luasrc
    ├── controller
    │   └── myapp
    ├── model
    │   └── cbi
    │       └── myapp-mymoeule
    └── view
        └── myapp-mymodule
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
vim luci-app-myapplication/Makefile
  • 1
include $(TOPDIR)/rules.mk

LUCI_TITLE:=LuCI Support for Test
LUCI_DEPENDS:=
include ../../luci.mk
# call BuildPackage - OpenWrt buildroot signature
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

luci-app-myapplication/luasrc/controller/myapp/new_tab.lua文件中 ,添加内容如下(注:与上面系统添加界面方式的内容一致):

module("luci.controller.myapp.new_tab", package.seeall) 
function index()
    entry({"admin", "new_tab"}, firstchild(),translate("cfg"), 1).dependent=false
    entry({"admin", "new_tab", "sn"}, cbi("myapp-mymodule/gateway_sn"), translate("sn"), 2)
    entry({"admin", "new_tab", "hellworld"}, template("myapp-mymodule/helloworld"), _("HelloWorld"), 3)
end
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

在luci-app-myapplication/luasrc/model/cbi/myapp-mymodule/gateway_sn.lua文件中 ,添加内容如下(注:与上面系统添加界面方式的内容一致):

m = Map("sn_file", translate("产品序列号")) -- cbi_file is the config file in /etc/config
d = m:section(TypedSection, "gateway_sn")  -- info is the section called info in cbi_file
a = d:option(Value, "sn", translate("序列号"));
a.optional=false; 
a.rmempty = false;  -- name is the option in the cbi_file
return m
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

在luci-app-myapplication/luasrc/view/myapp-mymodule/helloworld.htm 文件中 ,添加内容如下(注:与上面系统添加界面方式的内容一致):

<%+header%>
<h1><%: HelloWorld %></h1>
<%+footer%>
  • 1
  • 2
  • 3

在openwrt源码目录 openwrt源码/package/base-files/files/etc/config/ 新建cbi_file文件

config 'gateway_sn' 'sn'
    option 'sn' 'gw2019081300001'
  • 1
  • 2

回退到Openwrt 源码顶层目录,依次执行以下命令:

./scripts/feeds update luci
./scripts/feeds install -a -p luci
make menuconfig
  • 1
  • 2
  • 3

进入

LuCI --->
	Applications --->
			 <*> luci-app-myapplication............................. LuCI Support for Test 
  • 1
  • 2
  • 3

选定,然后保存退出,编译 make V=s
烧录固件

# 缓存问题

注:凡是修改controller/文件夹中的配置,都需要重启板子或把/tmp/目录下luci-indexcache luci-modulecache/luci-sessions/删除 才能生效,其他几个文件夹修改可不用,刷新一下网页即可

在这里插入图片描述

root@openwrt_d2550:~# cat /etc/config/luci

config core 'main'
        option resourcebase '/luci-static/resources'
        option lang 'zh_cn'
        option mediaurlbase '/luci-static/bootstrap'

config extern 'flash_keep'
        option uci '/etc/config/'
        option dropbear '/etc/dropbear/'
        option openvpn '/etc/openvpn/'
        option passwd '/etc/passwd'
        option opkg '/etc/opkg.conf'
        option firewall '/etc/firewall.user'
        option uploads '/lib/uci/upload/'

config internal 'languages'
        option zh_cn '简体中文 (Simplified Chinese)'

config internal 'sauth'
        option sessionpath '/tmp/luci-sessions'
        option sessiontime '3600'

config internal 'ccache'
        option enable '1'

config internal 'themes'
        option Argon '/luci-static/argon'
        option ATMaterial '/luci-static/atmaterial'
        option ATMaterial_red '/luci-static/atmaterial_red'
        option ATMaterial_Brown '/luci-static/atmaterial_Brown'
        option Bootstrap '/luci-static/bootstrap'
        option Edge '/luci-static/edge'
        option Material '/luci-static/material'
        option Neobird '/luci-static/neobird'
        option Netgear '/luci-static/netgear'
        option tomato '/luci-static/tomato'
        option Argonne '/luci-static/argonne'
        option ifit '/luci-static/ifit'
        option mcat '/luci-static/mcat'

config internal 'diag'
        option dns 'openwrt.org'
        option ping 'openwrt.org'
        option route 'openwrt.org'
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

路径错了

No page is registered at '/myt'.
If this url belongs to an extension, make sure it is properly installed.
If the extension was recently installed, try removing the /tmp/luci-indexcache file.
  • 1
  • 2
  • 3
/usr/lib/lua/luci/dispatcher.lua:350: Access Violation
The page at 'mytest/sethostname/' has no parent node so the access to this location has been denied.
This is a software bug, please report this message at https://github.com/openwrt/luci/issues
stack traceback:
	[C]: in function 'assert'
	/usr/lib/lua/luci/dispatcher.lua:350: in function 'dispatch'
	/usr/lib/lua/luci/dispatcher.lua:126: in function </usr/lib/lua/luci/dispatcher.lua:125>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号