第 3 節

Plugin development

0瀏覽次數0訪問次數--跳出率--平均停留

The source code of a plugin is composed of files. OpenWrt plugins generally use an "MVC" structure:

  1. Configuration file Located at /etc/config/*:
config server
option username ''
option password ''

Two pending input configurations have been generated in the sample file. A form page is now needed to manage them.

  1. CBI file Generally located in the directory /usr/lib/lua/luci/model/cbi/*.lua:
require("luci.sys")

-- 页面标题和描述
m = Map("bargo", translate("Bargo Client"), translate("Configure Bargo client, Powered By Sinchie."))

-- 读取配置文件
s = m:section(TypedSection, "server", "")
s.addremove = false
s.anonymous = true

-- 是否启用的选择框
enable = s:option(Flag, "enable", translate("Enable"))
-- 映射我们的配置到输入框
username = s:option(Value, "username", translate("Username"))
pass = s:option(Value, "password", translate("Password"))
pass.password = true

-- 如果点击了保存按钮
local apply = luci.http.formvalue("cbi.apply")
if apply then
    -- 这里是调用我们自己的程序脚本,后面会讲怎么来写这个脚本
    io.popen("/etc/init.d/bargo restart > /dev/null &")
end

return m
  1. controller file Generally located in the system's /usr/lib/lua/luci/controller/*.lua, after creating the controller file, you can create a menu entry in the web interface. The script structure is as follows:
-- module 名称
module("luci.controller.bargo", package.seeall)

function index()
    -- 4 个参数介绍
    -- 1.后台访问路径 admin/services/bargo 
    -- 2.target 动作(call, template, cbi)call 是调用自定义函数,template 调用 html 模板,cbi 调用 openwrt 的公共表单页面
    -- 3.菜单名称 
    -- 4.排序
    entry({"admin", "services", "bargo"}, cbi("bargo"), _("Bargo Client"), 1)
end
Create the build file Makefile

To create an OpenWRT compilation Makefile, follow these steps:

  1. Install the OpenWRT SDK and set up environment variables.
  2. Create a new folder in any directory to store your application code.
  3. In this directory, create the Makefile file and fill in the corresponding content according to the following structure:

# 这是注释,可以忽略或者修改

include $(TOPDIR)/rules.mk # 引入OpenWRT编译规则
PKG_NAME:=myapp # 应用程序的名称,建议使用小写字母和数字组合
PKG_VERSION:=1.0 # 应用程序的版本号
PKG_RELEASE:=1 # 应用程序的发行版本号

include $(INCLUDE_DIR)/package.mk # 引入OpenWRT中提供的软件包模板
define Package/myapp # 描述应用程序的信息
  SECTION:=net
  CATEGORY:=Network
  TITLE:=My Application
  DEPENDS:=+libopenssl +libcurl +libjson-c 
endef

define Package/myapp/description # 描述应用程序功能和用途
 My Application is a simple program that does something useful.
endef

define Build/Prepare # 准备构建应用程序所需的源码和资源

# 此处可以添加自定义命令,用于准备应用程序的源码和资源
endef

define Build/Configure # 配置应用程序的编译选项

# 此处可以添加自定义命令,用于配置应用程序的编译选项
endef

define Build/Compile # 编译应用程序的源码

# 此处可以添加自定义命令,用于编译应用程序的源码
endef

define Package/myapp/install # 安装应用程序所需的文件和目录
$(INSTALL_DIR) $(1)/usr/bin # 创建安装目录
$(INSTALL_BIN) $(PKG_BUILD_DIR)/myapp $(1)/usr/bin/ # 安装二进制文件
endef

$(eval $(call BuildPackage,myapp)) # 构建软件包并注册到OpenWRT中
音乐页