From e491cdb48129752324c4e3764f99bd9203c56dec Mon Sep 17 00:00:00 2001
From: lgq <1015864684@qq.com>
Date: 星期二, 31 三月 2026 09:48:44 +0800
Subject: [PATCH] 1.新增VF205门禁机代码
---
vf205_access/dxmodules/dxOta.js | 256 +++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 256 insertions(+), 0 deletions(-)
diff --git a/vf205_access/dxmodules/dxOta.js b/vf205_access/dxmodules/dxOta.js
new file mode 100644
index 0000000..a0061ee
--- /dev/null
+++ b/vf205_access/dxmodules/dxOta.js
@@ -0,0 +1,256 @@
+//build:20240724
+// 璐熻矗鍥轰欢鐨勫崌绾�
+import log from './dxLogger.js'
+import com from './dxCommon.js'
+import http from './dxHttp.js'
+import * as os from 'os';
+
+const ota = {}
+//鑾峰彇褰撳墠纾佺洏鍓╀綑澶у皬锛坘锛夊彲鑳戒笉鍚岀殑鎿嶄綔绯荤粺鎸囦护涓嶄竴鏍�
+ota.DF_CMD = `df -k / | awk 'NR==2 {print $4}'`
+ota.OTA_ROOT = '/ota'
+ota.OTA_RUN = ota.OTA_ROOT + '/run.sh'
+
+
+/**
+ * HTTP鍗囩骇锛氱綉缁滀笅杞藉崌绾у寘鍗囩骇
+ * @param {string} url 蹇呭~锛屼笅杞藉崌绾у寘鐨刪ttp url鍦板潃
+ * @param {string} md5 蹇呭~锛屽崌绾у寘鐨刴d5鏍囪瘑锛屼笅杞藉畬閫氳繃md5鏉ュ垽鏂槸鍚﹀畬鏁淬��32闀垮害鐨勫叏灏忓啓16杩涘埗瀛楃涓�
+ * @param {number} size 闈炲繀濉紝鍗囩骇鍖呯殑澶ф澶у皬锛屽崟浣嶆槸k锛屽鏋滄枃浠跺お澶ц�屽墿浣欑鐩樹笉澶燂紝浼氭彁鍓嶆姤閿欒锛屼笉浼氬紑濮嬪惎鍔ㄤ笅杞�
+ * @param {number} timeout 闈炲繀濉紝灏濊瘯閾炬帴涓嬭浇鍦板潃鐨勮秴鏃舵椂闂达紙涓嶆槸涓嬭浇瀹屾垚鐨勬椂闂达級锛岀己鐪佹槸3绉�
+ * @param {string} password 闈炲繀濉紝涓嬭浇瀹夎鍖呯殑瀵嗙爜锛屽彲閫夛紝濡傛灉璁剧疆锛屼笅杞芥椂闇�瑕佽緭鍏ュ瘑鐮�
+ */
+ota.updateHttp = function (url, md5, timeout = 3, password, size) {
+ if (!url || !md5) {
+ throw new Error("The 'url' and 'md5' param should not be null")
+ }
+ if (size && (typeof size != 'number')) {
+ throw new Error("The 'size' param should be a number")
+ }
+ //1. 鏌ョ湅纾佺洏杩樺墿浣欑殑澶у皬
+ let df = parseInt(com.systemWithRes(ota.DF_CMD, 1000))
+ if (size) {
+ if (df < (3 * size)) {//澶ф鏈湴蹇呴』鏈夊畨瑁呭寘3鍊嶅ぇ灏忕殑绌洪棿
+ throw new Error('The upgrade package is too large, and not be enough space on the disk to download it')
+ }
+ }
+ //2. 涓嬭浇鏂囦欢鍒颁复鏃剁洰褰�
+ const firmware = '/upgrades.zip'
+ const temp = '/upgrades.temp'
+ com.systemBrief(`rm -rf ${firmware} && rm -rf ${temp} `) //鍏堝垹闄ta鏍圭洰褰�
+
+ let downloadRet = http.download(url + (password ? "&password=" + password : '') , temp, null, timeout*1000)
+ let fileExist = (os.stat(temp)[1] === 0)
+ if (!fileExist) {
+ log.error("download result" + downloadRet)
+ com.systemBrief(`rm -rf ${firmware} && rm -rf ${temp} `)
+ throw new Error('Download failed, please check the url:' + url)
+ }
+ //3. 璁$畻骞舵瘮杈僲d5鏄惁涓�鏍�
+ let md5Hash = com.md5HashFile(temp)
+ md5Hash = md5Hash.map(v => v.toString(16).padStart(2, 0)).join('')
+ if (md5Hash != md5) {
+ com.systemBrief(`rm -rf ${firmware} && rm -rf ${temp} `)
+ throw new Error('Download failed with wrong md5 value')
+ }
+ //4. md5鏍¢獙閫氳繃锛屽皢鍗囩骇鍖呮斁鍒板崌绾х洰褰曚笅锛岀瓑寰呴噸鍚崌绾�
+ com.systemBrief(`mv ${temp} ${firmware} `)
+}
+
+
+/**
+ * 鏂囦欢鍗囩骇锛氶�氳繃鍏朵粬鏂瑰紡灏嗗崌绾у寘鏀惧埌鐢ㄦ埛鐩綍涓嬶紝璋冪敤姝ゆ柟娉曞崌绾�
+ * @param {string} path 蹇呭~锛屼笅杞藉崌绾у寘鐨刪ttp url鍦板潃
+ * @param {string} md5 蹇呭~锛屽崌绾у寘鐨刴d5鏍囪瘑锛屼笅杞藉畬閫氳繃md5鏉ュ垽鏂槸鍚﹀畬鏁淬��32闀垮害鐨勫叏灏忓啓16杩涘埗瀛楃涓�
+ * @param {number} size 闈炲繀濉紝鍗囩骇鍖呯殑澶ф澶у皬锛屽崟浣嶆槸k锛屽鏋滄枃浠跺お澶ц�屽墿浣欑鐩樹笉澶燂紝浼氭彁鍓嶆姤閿欒锛屼笉浼氬紑濮嬪惎鍔ㄤ笅杞�
+ */
+ota.updateFile = function (path, md5, size) {
+ if (!path || !md5) {
+ throw new Error("The 'path' and 'md5' param should not be null")
+ }
+ if (size && (typeof size != 'number')) {
+ throw new Error("The 'size' param should be a number")
+ }
+
+ //1. 鏌ョ湅纾佺洏杩樺墿浣欑殑澶у皬
+ let df = parseInt(com.systemWithRes(ota.DF_CMD, 1000))
+ if (size) {
+ if (df < (3 * size)) {//澶ф鏈湴蹇呴』鏈夊畨瑁呭寘3鍊嶅ぇ灏忕殑绌洪棿
+ throw new Error('The upgrade package is too large, and not be enough space on the disk to download it')
+ }
+ }
+
+ //2. 璁$畻骞舵瘮杈僲d5鏄惁涓�鏍�
+ let md5Hash = com.md5HashFile(path)
+ md5Hash = md5Hash.map(v => v.toString(16).padStart(2, 0)).join('')
+ if (md5Hash != md5) {
+ throw new Error('With wrong md5 value')
+ }
+
+ //3. md5鏍¢獙閫氳繃锛屽皢鍗囩骇鍖呮斁鍒板崌绾х洰褰曚笅锛岀瓑寰呴噸鍚崌绾�
+ const firmware = '/upgrades.zip'
+ com.systemBrief(`mv ${path} ${firmware} `)
+}
+
+
+/**
+ * 娉ㄦ剰锛氭鏂规硶鍗冲皢杩囨湡锛岀敤浜庡吋瀹规棫鐗堟湰锛屾柊鐗堟湰涓嶆帹鑽愪娇鐢�
+ * 鍗囩骇鍒嗕簩澶ф楠わ紝绗竴姝ユ槸鍦ㄥ簲鐢ㄧ涓嬭浇鍗囩骇鍖咃紙zip锛夛紝瑙e帇鍗囩骇鍖�
+ * 绗簩姝ュ寘鎷噸鍚澶囷紝鍒╃敤鑴氭湰澶嶅埗鐩綍鍜屾枃浠舵垨棰濆涓�浜涙搷浣�
+ * 濡傛灉鍦ㄥ崌绾у寘鏍圭洰褰曚笅鏀句竴涓猚ustom_update.sh,灏变細鍏堟墽琛岃繖涓猻hell鏂囦欢锛屾垜浠彲浠ュ湪杩欎釜鏂囦欢閲屾斁涓�浜涜嚜瀹氫箟鐨勫崌绾у姩浣�
+ * @param {string} url 蹇呭~锛屼笅杞藉崌绾у寘鐨刪ttp url鍦板潃
+ * @param {string} md5 蹇呭~锛屽崌绾у寘鐨刴d5鏍囪瘑锛屼笅杞藉畬閫氳繃md5鏉ュ垽鏂槸鍚﹀畬鏁淬��32闀垮害鐨勫叏灏忓啓16杩涘埗瀛楃涓�
+ * @param {number} size 闈炲繀濉紝鍗囩骇鍖呯殑澶ф澶у皬锛屽崟浣嶆槸k锛屽鏋滄枃浠跺お澶ц�屽墿浣欑鐩樹笉澶燂紝浼氭彁鍓嶆姤閿欒锛屼笉浼氬紑濮嬪惎鍔ㄤ笅杞�
+ * @param {string} shell 闈炲繀濉紝閲嶅惎璁惧鍚庣殑鍗囩骇鑴氭湰鍐呭锛岃В鍘嬪悗鐨勬枃浠跺す缂虹渷鏄� /ota/temp,鍗囩骇浼氱己鐪佹妸/ota/temp涓嬫墍鏈夋枃浠舵嫹璐濆鍒跺埌/app/code/涓�
+ * @param {number} timeout 闈炲繀濉紝灏濊瘯閾炬帴涓嬭浇鍦板潃鐨勮秴鏃舵椂闂达紙涓嶆槸涓嬭浇瀹屾垚鐨勬椂闂达級锛岀己鐪佹槸3绉�
+ */
+ota.update = function (url, md5, size, shell, timeout = 3) {
+ if (!url || !md5) {
+ throw new Error("The 'url' and 'md5' param should not be null")
+ }
+ if (size && (typeof size != 'number')) {
+ throw new Error("The 'size' param should be a number")
+ }
+ //1. 鏌ョ湅纾佺洏杩樺墿浣欑殑澶у皬
+ let df = parseInt(com.systemWithRes(ota.DF_CMD, 1000))
+ if (size) {
+ if (df < (3 * size)) {//澶ф鏈湴蹇呴』鏈夊畨瑁呭寘3鍊嶅ぇ灏忕殑绌洪棿
+ throw new Error('The upgrade package is too large, and not be enough space on the disk to download it')
+ }
+ }
+ //2. 涓嬭浇鏂囦欢鍒扮壒瀹氱洰褰�
+ const firmware = ota.OTA_ROOT + '/download.zip'
+ const temp = ota.OTA_ROOT + '/temp'
+ com.systemBrief(`rm -rf ${ota.OTA_ROOT} && mkdir ${ota.OTA_ROOT} `) //鍏堝垹闄ta鏍圭洰褰�
+ let download = `wget --no-check-certificate --timeout=${timeout} -c "${url}" -O ${firmware} 2>&1`
+ com.systemBrief(download, 1000)
+ let fileExist = (os.stat(firmware)[1] === 0)
+ let downloadRet
+ if (!fileExist) {
+ downloadRet = http.download(url, firmware, null, timeout*1000)
+ }
+ fileExist = (os.stat(firmware)[1] === 0)
+ if (!fileExist) {
+ log.error("download result" + downloadRet)
+ throw new Error('Download failed, please check the url:' + url)
+ }
+ //3. 璁$畻骞舵瘮杈僲d5鏄惁涓�鏍�
+ let md5Hash = com.md5HashFile(firmware)
+ md5Hash = md5Hash.map(v => v.toString(16).padStart(2, 0)).join('')
+ if (md5Hash != md5) {
+ log.error("download result" + downloadRet)
+ throw new Error('Download failed with wrong md5 value')
+ }
+ //4. 瑙e帇
+ com.systemBrief(`mkdir ${temp} && unzip -o ${firmware} -d ${temp}`)
+ //5. 鎵ц鑷畾涔夌殑鍗囩骇鑴氭湰
+ const custom_update = temp+'/custom_update.sh'
+ if(os.stat(custom_update)[1] === 0){
+ com.systemBrief(`chmod +x ${custom_update}`)
+ com.systemWithRes(`${custom_update}`)
+ }
+ //6. 鏋勫缓鑴氭湰鏂囦欢
+ if (!shell) {
+ //缂虹渷鍙槸鎷疯礉鐩綍骞跺垹闄ta鏍圭洰褰�
+ shell = `cp -r ${temp}/* /app/code \n rm -rf ${ota.OTA_ROOT}`
+ }
+
+ com.systemBrief(`echo "${shell}" > ${ota.OTA_RUN} && chmod +x ${ota.OTA_RUN}`)
+ fileExist = (os.stat(ota.OTA_RUN)[1] === 0)
+ if (!fileExist) {
+ throw new Error('Build shell file failed')
+ }
+ com.systemWithRes(`${ota.OTA_RUN}`)
+}
+
+/**
+ * 娉ㄦ剰锛氭鏂规硶鍗冲皢杩囨湡锛岀敤浜庡吋瀹规棫鐗堟湰锛屾柊鐗堟湰涓嶆帹鑽愪娇鐢�
+ * 鐗规畩锛氬吋瀹规棫鐨勫崌绾ф牸寮忥紝蹇呴』鏄痶ar.xz鏍煎紡锛屼笖鍙敤鏉ュ崌绾ц祫婧愭枃浠�
+ * 鍗囩骇鍒嗕簩澶ф楠わ紝绗竴姝ユ槸鍦ㄥ簲鐢ㄧ涓嬭浇鍗囩骇鍖咃紙zip锛夛紝瑙e帇鍗囩骇鍖�
+ * 绗簩姝ュ寘鎷噸鍚澶囷紝鍒╃敤鑴氭湰澶嶅埗鐩綍鍜屾枃浠舵垨棰濆涓�浜涙搷浣�
+ * 濡傛灉鍦ㄥ崌绾у寘鏍圭洰褰曚笅鏀句竴涓猚ustom_update.sh,灏变細鍏堟墽琛岃繖涓猻hell鏂囦欢锛屾垜浠彲浠ュ湪杩欎釜鏂囦欢閲屾斁涓�浜涜嚜瀹氫箟鐨勫崌绾у姩浣�
+ * @param {string} url 蹇呭~锛屼笅杞藉崌绾у寘鐨刪ttp url鍦板潃
+ * @param {string} md5 蹇呭~锛屽崌绾у寘鐨刴d5鏍囪瘑锛屼笅杞藉畬閫氳繃md5鏉ュ垽鏂槸鍚﹀畬鏁淬��32闀垮害鐨勫叏灏忓啓16杩涘埗瀛楃涓�
+ * @param {number} size 闈炲繀濉紝鍗囩骇鍖呯殑澶ф澶у皬锛屽崟浣嶆槸k锛屽鏋滄枃浠跺お澶ц�屽墿浣欑鐩樹笉澶燂紝浼氭彁鍓嶆姤閿欒锛屼笉浼氬紑濮嬪惎鍔ㄤ笅杞�
+ * @param {string} shell 闈炲繀濉紝閲嶅惎璁惧鍚庣殑鍗囩骇鑴氭湰鍐呭锛岃В鍘嬪悗鐨勬枃浠跺す缂虹渷鏄� /ota/temp,鍗囩骇浼氱己鐪佹妸/ota/temp涓嬫墍鏈夋枃浠舵嫹璐濆鍒跺埌/app/code/涓�
+ * @param {number} timeout 闈炲繀濉紝灏濊瘯閾炬帴涓嬭浇鍦板潃鐨勮秴鏃舵椂闂达紙涓嶆槸涓嬭浇瀹屾垚鐨勬椂闂达級锛岀己鐪佹槸3绉�
+ */
+ota.updateResource = function (url, md5, size, shell, timeout = 3) {
+ if (!url || !md5) {
+ throw new Error("The 'url' and 'md5' param should not be null")
+ }
+ if (size && (typeof size != 'number')) {
+ throw new Error("The 'size' param should be a number")
+ }
+ //1. 鏌ョ湅纾佺洏杩樺墿浣欑殑澶у皬
+ let df = parseInt(com.systemWithRes(ota.DF_CMD, 1000))
+ if (size) {
+ if (df < (3 * size)) {//澶ф鏈湴蹇呴』鏈夊畨瑁呭寘3鍊嶅ぇ灏忕殑绌洪棿
+ throw new Error('The upgrade package is too large, and not be enough space on the disk to download it')
+ }
+ }
+ //2. 涓嬭浇鏂囦欢鍒扮壒瀹氱洰褰�
+ const firmware = ota.OTA_ROOT + '/download.tar.xz'
+ const temp = ota.OTA_ROOT + '/temp'
+ com.systemBrief(`rm -rf ${ota.OTA_ROOT} && mkdir ${ota.OTA_ROOT} `) //鍏堝垹闄ta鏍圭洰褰�
+ let download = `wget --no-check-certificate --timeout=${timeout} -c "${url}" -O ${firmware} 2>&1`
+ com.systemBrief(download, 1000)
+ let fileExist = (os.stat(firmware)[1] === 0)
+ if (!fileExist) {
+ http.download(url, firmware, null, timeout*1000)
+ }
+ fileExist = (os.stat(firmware)[1] === 0)
+ if (!fileExist) {
+ throw new Error('Download failed, please check the url:' + url)
+ }
+
+ //3. 璁$畻骞舵瘮杈僲d5鏄惁涓�鏍�
+ let md5Hash = com.md5HashFile(firmware)
+ md5Hash = md5Hash.map(v => v.toString(16).padStart(2, 0)).join('')
+ if (md5Hash != md5) {
+ throw new Error('Download failed with wrong md5 value')
+ }
+ //4. 瑙e帇
+ //tar -xJvf test.tar.xz -C /path/
+ com.systemBrief(`mkdir ${temp} && tar -xJvf ${firmware} -C ${temp}`)
+ //5. 鏋勫缓鑴氭湰鏂囦欢
+ if (!shell) {
+ shell = `
+ source=${temp}/vgapp/res/image/bk.png
+ target=/app/code/resource/image/bg.png
+ if test -e "\\$source"; then
+ cp "\\$source" "\\$target"
+ fi
+ source=${temp}/vgapp/res/image/bk_90.png
+ target=/app/code/resource/image/bg_90.png
+ if test -e "\\$source"; then
+ cp "\\$source" "\\$target"
+ fi
+ source=${temp}/vgapp/res/font/AlibabaPuHuiTi-2-65-Medium.ttf
+ target=/app/code/resource/font.ttf
+ if test -e "\\$source"; then
+ cp "\\$source" "\\$target"
+ fi
+ source=${temp}/vgapp/wav/*.wav
+ target=/app/code/resource/wav/
+ cp "\\$source" "\\$target"
+ rm -rf ${ota.OTA_ROOT}
+ `
+ }
+
+ com.systemBrief(`echo "${shell}" > ${ota.OTA_RUN} && chmod +x ${ota.OTA_RUN}`)
+ fileExist = (os.stat(ota.OTA_RUN)[1] === 0)
+ if (!fileExist) {
+ throw new Error('Build shell file failed')
+ }
+ com.systemWithRes(`${ota.OTA_RUN}`)
+}
+/**
+ * 鐢辫皟鐢ㄨ�呮潵鍚姩閲嶅惎锛屼竴鑸槸update鍑芥暟娌℃湁閿欒锛岃繍琛屽畬鎴愬苟鍚戝寳鍚戞眹鎶ョ粨鏋滃悗鍐嶈皟鐢ㄩ噸鍚�
+ */
+ota.reboot = function () {
+ com.asyncReboot(2)
+}
+//-------------------------private-------------------
+
+export default ota
\ No newline at end of file
--
Gitblit v1.9.3