Openwrt-Luci 開發介紹(三)

2022年12月24日12:05:20 科技 1910

這篇重點講如何在源碼中Web開發

1. 創建目錄及文件(以下重點講RSSI和NicRate這兩部分功能)


進入 openwrt源碼/qsdk/qca/feeds/luci/applications
,添加下圖中目錄文件結構:

Openwrt-Luci 開發介紹(三) - 天天要聞

2. 上圖luci-app-myapplication/Makefile文件中添加以下代碼:

include $(TOPDIR)/rules.mk




LUCI_TITLE:=LuCI Support for Test

LUCI_DEPENDS:=




include ../../luci.mk

# call BuildPackage - OpenWrt buildroot signature

3. 在luasrc/controller/myapp/mobi.lua添加以下代碼:

function index()

entry({"admin", "MOBI"}, firstchild(),"MOBI", 30).dependent=false

entry({"admin", "MOBI", "RSSI"}, template("rssi_test"), _("信號強度"), 1)

entry({"admin", "MOBI", "NicRate"}, cbi("mymodule/gateway"), translate("網卡速率"), 2)

end

4. 在/luasrc/view/myview/rssi_test中添加以下代碼:

<%

local fs = require "nixio.fs"

local util = require "luci.util"

local log = require "luci.log"

require "luci.model.uci"

if luci.http.formvalue("status") == "1" then

local file = io.open('/sys/class/net/wlan0/device/prs_attrs/rx_rssi','r')

local ret

if file then

ret = file:read('*all')

file:close()

luci.http.prepare_content("application/json")

luci.http.write_json(ret)

return

else

ret = "Error"

log.print("ret="..ret)

luci.http.prepare_content("application/json")

luci.http.write_json(ret)

return

end

end

-%>

<%+header%>

<script type="text/javascript" src="<%=resource%>/cbi.js"></script>

<script type="text/javaScript">

XHR.poll(2,'<%=REQUEST_URI%>',{status:1},function(x,info) {

var ret = info;

document.getElementById("contxt").innerText = info;

}

);

</script>

<h1><a id="content" name="content"><%:Signal Strength:%></a></h1>

<h2><b id="contxt" name="content"><%%></b></h2>

<%+footer%>

5.在/luasrc/model/cbi/mymodule/gateway.lua中添加以下代碼:

local fs = require "nixio.fs"

m = Map("test_file","Nic Rate" ,translate("Nic Rate")) -- cbi_file is the config file in /etc/config

s = m:section(TypedSection,"arguments","")

s.addremove=false

s.anonymous=true

o = s:option(ListValue,"collection","NicRate")

o:value("first item","10G")

o:value("second item","2.5G")

local apply = luci.http.formvalue("cbi.apply")

if apply then

io.popen("/etc/init.d/NicTestSH.sh start")

end

return m

6. 在openwrt源碼/qsdk/package/base-files/files/etc/init.d/NisTestSH.sh添加以下代碼:

#!/bin/sh etc/rc.common

START=50

run_mobi()

{

local enable

config_get_bool enable $1 enable

local collection

config_get collection $1 collection

if [ "$collection" == "first item" ]; then

ethtool -s eth1 speed 2500 duplex full

else

ethtool -s eth1 speed 10000 duplex full

fi

}

start()

{

config_load test_file

config_foreach run_mobi arguments

}



7. 執行Luci更新:

回退到openwrt源碼目錄,以此執行以下命令:

./scripts/feeds update luci

./scripts/feeds install -a -p luci

8.make menuconfig中勾選:

LuCI --->
	Collections --->
			<*> luci
			<*> luci-ssl
	Translations--->
			<*> Chinese(zh-cn)   //支持中文
			<*> English (en)     支持英文
LuCI --->
	Applications --->
	      <*> luci-app-myapplication.............. LuCI Support for Test 

Openwrt-Luci 開發介紹(三) - 天天要聞

Openwrt-Luci 開發介紹(三) - 天天要聞

9. 然後執行 make V=s 進行編譯

10. 燒錄固件,查看web界面

Openwrt-Luci 開發介紹(三) - 天天要聞

Openwrt-Luci 開發介紹(三) - 天天要聞

屬於自己的第一個頁面就完成了呢,是不是很激動

,接下來繼續挖掘研究吧!

科技分類資訊推薦

引領科技豪華MPV新風尚 第二代騰勢D9西安車展亮相 - 天天要聞

引領科技豪華MPV新風尚 第二代騰勢D9西安車展亮相

兼具宜商氣度與家用溫情的科技豪華旗艦MPV,第二代騰勢D9迎來西安地區正式亮相。新車依託全球新能源MPV冠軍底蘊,以第二代刀片電池、雙閥雲輦-C、天神之眼5.0智駕等核心技術全面升級,兼顧商務體面與家庭舒適,為西北高端用戶帶來一站式全能出行解決方案。
採購禁入!科華數據材料造假被拒門外 - 天天要聞

採購禁入!科華數據材料造假被拒門外

本報(chinatimes.net.cn)記者胡雅文 北京報道這家趕上AI算力風口的公司,因投標材料造假,被相關採購方列入禁入名單兩年,其此前提出的複議申請也被正式駁回。相關採購平台近日發布公告,明確駁回科華數據股份有限公司(下稱「科華數據」,002335.SZ)此前提交的複議申請。早在一年前,科華數據已被認定在「信息通信樞紐...
快評樂道L80:15萬元級買大五座,這波值得沖? - 天天要聞

快評樂道L80:15萬元級買大五座,這波值得沖?

日前,樂道L80正式發布並開啟預售,其整車購買預售價為24.58萬元起,租電購買預售價則低至15.98萬元起。面對大型SUV市場「細分再細分」之競爭趨勢,這款樂道年度重磅新車都有哪些優勢?又能否成為「大五座SUV革新之作」?下面,圈哥就帶大家全方位感受。
成都直擊凱威德:純電全尺寸SUV的張揚與大氣 - 天天要聞

成都直擊凱威德:純電全尺寸SUV的張揚與大氣

4月22日,凱迪拉克以奧斯卡級盛典規格,將上海保利大劇院點亮為璀璨舞台,在品牌代言人倪妮與全場嘉賓的共同見證下,凱迪拉克全尺寸純電公路旗艦——凱威德耀然上市。新車共推出長續航四驅Pro、高性能四驅Ultra兩款配置,官方售價區間為46.88萬-50.88萬元。