栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

OpenWRT 添加 WEB 配置界面实战记录

OpenWRT 添加 WEB 配置界面实战记录

本篇是记录在 Openwrt 镜像中添加 自定义的 web 配置界面过程,编译进 openwrt 的系统镜像中。

第一步 建立项目文件目录
mkdir -p feeds/luci/applications/luci-app-Gateway
mkdir -p feeds/luci/applications/luci-app-Gateway/config 
mkdir -p feeds/luci/applications/luci-app-Gateway/luci 
mkdir -p feeds/luci/applications/luci-app-Gateway/module
第二步 编写 controller 的 lua 源码
wooya@hi-wooya:~/openwrt-hiwooya/feeds/luci/applications/luci-app-Gateway$ cat luasrc/controller/gateway.lua 
module("luci.controller.gateway", package.seeall) 
function index()
    entry({"admin", "services", "Gateway"}, cbi("gateway/gateway_conf"), translate("Gateway"), nil)
    entry({"admin", "services", "Service"}, template("gateway/helloworld"), _("online help"), nil)
end
第三步 编写 model 模块内容
wooya@hi-wooya:~/openwrt-hiwooya/feeds/luci/applications/luci-app-Gateway$ cat luasrc/model/cbi/gateway/gateway_conf.lua 
m = Map("gatewayConf", translate("Gateway")) -- cbi_file is the config file in /etc/config

g = m:section(TypedSection, "gateway")  -- info is the section called info in cbi_file

sn = g:option(Value, "sn", translate("Gateway SN"),"BCD code Eg:abcdef202202110001000101");

addr = g:option(Value,"IPv4addr",translate("Server IPv4:Port"),"eg:172.168.10.211:1813");

heart= g:option(Value, "HeartBeat", translate("MQTT HeartBeat"), "d:second");

name = g:option(Value, "UserName", translate("MQTT UserName"));

pwd = g:option(Value, "Password", translate("MQTT Password"));

return m
第四步 编码在线帮助web 网页
wooya@hi-wooya:~/openwrt-hiwooya/feeds/luci/applications/luci-app-Gateway$ cat luasrc/view/gateway/helloworld.htm 
<%+header%>

<%: HelloWorld %>

<%+footer%>
第五步 创建配置参数确实值
wooya@hi-wooya:~/openwrt-hiwooya/feeds/luci/applications/luci-app-Gateway/root/etc/config$ cat gatewayConf
config gateway 'parament'
        option HeartBeat '60'
        option sn 'abcdef202202110001000101'
        option UserName 'admin'
        option Password '123456'
        option IPv4addr '81.70.211.95:1883'

此文件镜像打包后的系统路径: etc/config 网页中显示和配置内容。

第六步 验证文件路径
wooya@hi-wooya:~/openwrt-hiwooya/feeds/luci/applications/luci-app-Gateway$ tree -L 5
.
├── luasrc
│   ├── controller
│   │   └── gateway.lua
│   ├── model
│   │   └── cbi
│   │       └── gateway
│   │           └── gateway_conf.lua
│   └── view
│       └── gateway
│           └── helloworld.htm
├── Makefile
└── root
    └── etc
        └── config
            └── gatewayConf

至此我们把 luci 相关源码准备好,开始编译验证

第七步 编译 luci app 模块

安装 luci_app_gateway 模板

wooya@hi-wooya:~/openwrt-hiwooya$ ./scripts/feeds install -a -p luci
Installing all packages from feed luci.
Installing package 'luci-app-Gateway'

# make menuconfig 

Symbol: DEFAULT_luci-app-Gateway [=DEFAULT_luci-app-Gateway]                                                                     
  │ Type  : unknown                                                                                                                  
  │                                                                                                                                 
  │                                                                                                                                 
  │ Symbol: PACKAGE_luci-app-Gateway [=y]                                                                                            
  │ Type  : tristate                                                                                                                 
  │ prompt: luci-app-Gateway........................... LuCI Support for Gateway-menu                                                 
  │   Location:                                                                                                                     
  │     -> LuCI                                                                                                                      
  │ (1)   -> 3. Applications                                                                                                         
  │   Defined at tmp/.config-package.in:27354                                                                                       
  │   Selects: PACKAGE_librt [=y] && PACKAGE_libc [=y] && PACKAGE_libpthread [=y] && PACKAGE_libssp [=n]   

烧写镜像后,可以在 services 菜单中,看到 Gateway 和 online help 子菜单内容。下一篇将分享如果在 C++ 程序通过 libuci.so 库
方式,获取web设置内容。

如果您对 Luci 相关语法有些陌生,可以移步至 openwrt.org 网站中wiki部分检索。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/761840.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号