From cd40c6f6f3a6138cb70b2e2d017cc7f34a887ef8 Mon Sep 17 00:00:00 2001
From: lgq <1015864684@qq.com>
Date: 星期五, 17 四月 2026 13:59:49 +0800
Subject: [PATCH] 1.更新vf107代码,更新自动上报通行记录接口、人员查询接口、人员添加接口
---
vf107/dxmodules/dxOta.js | 191 ++++++++++++++++++++++++-----------------------
1 files changed, 98 insertions(+), 93 deletions(-)
diff --git a/vf107/dxmodules/dxOta.js b/vf107/dxmodules/dxOta.js
index 0084a38..284a528 100644
--- a/vf107/dxmodules/dxOta.js
+++ b/vf107/dxmodules/dxOta.js
@@ -1,20 +1,20 @@
/**
- * OTA Module
- * Features:
- * - HTTP online and local file upgrades
- * - Automatic MD5 integrity verification
- * - Pre-upgrade disk space validation
-
- * Usage:
- 1. Build code into an app package. Click `Package` in VSCode DejaOS Plugin to generate a .dpk file in .temp folder.
- 2. Upload the .dpk file (zip format) to a web server and get the download URL.
- 3. Send the download URL and MD5 checksum to the device app.
- - Encode URL and MD5 as QR code for device scanning,
- - Or use other methods (Bluetooth, MQTT, RS485, etc.).
- 4. Device downloads and verifies package integrity using MD5.
- 5. Extract package to stable directory and reboot device.
- 6. After reboot, DejaOS extracts package and overwrites existing code.
- * Doc/Demo: https://github.com/DejaOS/DejaOS
+ * OTA 妯″潡
+ * 鍔熻兘锛�
+ * - HTTP 鍦ㄧ嚎鍗囩骇鍜屾湰鍦版枃浠跺崌绾�
+ * - 鑷姩 MD5 瀹屾暣鎬ч獙璇�
+ * - 鍗囩骇鍓嶇鐩樼┖闂撮獙璇�
+
+ * 浣跨敤鏂规硶锛�
+ * 1. 灏嗕唬鐮佹瀯寤轰负搴旂敤鍖呫�傚湪 VSCode DejaOS 鎻掍欢涓偣鍑� `Package` 鐢熸垚 .temp 鏂囦欢澶逛腑鐨� .dpk 鏂囦欢銆�
+ * 2. 灏� .dpk 鏂囦欢锛坺ip 鏍煎紡锛変笂浼犲埌 web 鏈嶅姟鍣ㄥ苟鑾峰彇涓嬭浇 URL銆�
+ * 3. 灏嗕笅杞� URL 鍜� MD5 鏍¢獙鍜屽彂閫佸埌璁惧搴旂敤銆�
+ * - 灏� URL 鍜� MD5 缂栫爜涓轰簩缁寸爜渚涜澶囨壂鎻忥紝
+ * - 鎴栦娇鐢ㄥ叾浠栨柟娉曪紙钃濈墮銆丮QTT銆丷S485 绛夛級銆�
+ * 4. 璁惧涓嬭浇骞朵娇鐢� MD5 楠岃瘉鍖呭畬鏁存�с��
+ * 5. 灏嗗寘鎻愬彇鍒扮ǔ瀹氱洰褰曞苟閲嶅惎璁惧銆�
+ * 6. 閲嶅惎鍚庯紝DejaOS 鎻愬彇鍖呭苟瑕嗙洊鐜版湁浠g爜銆�
+ * 鏂囨。/绀轰緥锛歨ttps://github.com/DejaOS/DejaOS
*/
import log from './dxLogger.js'
import com from './dxCommon.js'
@@ -26,24 +26,24 @@
ota.UPGRADE_TEMP = '/upgrades.temp'
ota.DF_CMD = `df -k / | awk 'NR==2 {print $4}'`
/**
- * Download upgrade package via HTTP
- * @param {string} url Required. HTTP URL for downloading the upgrade package
- * @param {string} md5 Required. MD5 hash for integrity verification (32-char lowercase hex)
- * @param {number} timeout Optional. Download timeout in seconds (default: 60)
- * @param {number} size Optional. Package size in KB for disk space validation
- * @param {Object} [httpOpts] Additional request opts
+ * 閫氳繃 HTTP 涓嬭浇鍗囩骇鍖�
+ * @param {string} url 蹇呭~銆備笅杞藉崌绾у寘鐨� HTTP URL
+ * @param {string} md5 蹇呭~銆傜敤浜庡畬鏁存�ч獙璇佺殑 MD5 鍝堝笇鍊硷紙32 浣嶅皬鍐欏崄鍏繘鍒讹級
+ * @param {number} timeout 鍙�夈�備笅杞借秴鏃舵椂闂达紙绉掞級锛岄粯璁わ細60
+ * @param {number} size 鍙�夈�傜敤浜庣鐩樼┖闂撮獙璇佺殑鍖呭ぇ灏忥紙KB锛�
+ * @param {Object} [httpOpts] 棰濆鐨勮姹傞�夐」
*/
ota.updateHttp = function (url, md5, timeout = 60, size, httpOpts) {
if (!url || !md5) {
- throw new Error("'url' and 'md5' parameters are required")
+ throw new Error("'url' 鍜� 'md5' 鍙傛暟鏄繀濉殑")
}
if (size && (typeof size != 'number')) {
- throw new Error("'size' parameter must be a number")
+ throw new Error("'size' 鍙傛暟蹇呴』鏄暟瀛�")
}
- // 1. Check available disk space
+ // 1. 妫�鏌ュ彲鐢ㄧ鐩樼┖闂�
checkDiskSpace(size)
- // 2. Download file to temporary directory
- com.systemBrief(`rm -rf ${ota.UPGRADE_TARGET} && rm -rf ${ota.UPGRADE_TEMP} `) // Clean up existing files
+ // 2. 涓嬭浇鏂囦欢鍒颁复鏃剁洰褰�
+ com.systemBrief(`rm -rf ${ota.UPGRADE_TARGET} && rm -rf ${ota.UPGRADE_TEMP} `) // 娓呯悊鐜版湁鏂囦欢
log.info("download url:" + url)
let downloadRet = http.download(url, ota.UPGRADE_TEMP, timeout * 1000, httpOpts)
log.info("download result:" + JSON.stringify(downloadRet))
@@ -51,46 +51,50 @@
let fileExist = (os.stat(ota.UPGRADE_TEMP)[1] === 0)
if (!fileExist) {
com.systemBrief(`rm -rf ${ota.UPGRADE_TARGET} && rm -rf ${ota.UPGRADE_TEMP} `)
- throw new Error('Download failed. Please check the URL: ' + url)
+ log.info("涓嬭浇澶辫触銆� url:" + url)
+ throw new Error('涓嬭浇澶辫触銆傝妫�鏌� URL: ' + url)
}
- // 3. Verify MD5 checksum
+
+ log.info("verify md5:" + md5)
+ log.info("verify md5 result:" + verifyMD5(ota.UPGRADE_TEMP, md5))
+ // 3. 楠岃瘉 MD5 鏍¢獙鍜�
if (!verifyMD5(ota.UPGRADE_TEMP, md5)) {
com.systemBrief(`rm -rf ${ota.UPGRADE_TARGET} && rm -rf ${ota.UPGRADE_TEMP} `)
- throw new Error('MD5 verification failed')
+ throw new Error('MD5 楠岃瘉澶辫触')
}
- // 4. Move verified package to upgrade directory
+ // 4. 灏嗛獙璇侀�氳繃鐨勫寘绉诲姩鍒板崌绾х洰褰�
com.systemBrief(`mv ${ota.UPGRADE_TEMP} ${ota.UPGRADE_TARGET} `)
com.systemBrief(`sync`)
}
/**
- * Upgrade from local file
- * Use this when you've already downloaded the package via custom methods.
- * @param {string} path Required. Path to the upgrade package
- * @param {string} md5 Required. MD5 hash for integrity verification (32-char lowercase hex)
- * @param {number} size Optional. Package size in KB for disk space validation
+ * 浠庢湰鍦版枃浠跺崌绾�
+ * 褰撲綘宸茬粡閫氳繃鑷畾涔夋柟娉曚笅杞戒簡鍖呮椂浣跨敤姝ゆ柟娉曘��
+ * @param {string} path 蹇呭~銆傚崌绾у寘鐨勮矾寰�
+ * @param {string} md5 蹇呭~銆傜敤浜庡畬鏁存�ч獙璇佺殑 MD5 鍝堝笇鍊硷紙32 浣嶅皬鍐欏崄鍏繘鍒讹級
+ * @param {number} size 鍙�夈�傜敤浜庣鐩樼┖闂撮獙璇佺殑鍖呭ぇ灏忥紙KB锛�
*/
ota.updateFile = function (path, md5, size) {
if (!path || !md5) {
- throw new Error("'path' and 'md5' parameters are required")
+ throw new Error("'path' 鍜� 'md5' 鍙傛暟鏄繀濉殑")
}
if (size && (typeof size != 'number')) {
- throw new Error("'size' parameter must be a number")
+ throw new Error("'size' 鍙傛暟蹇呴』鏄暟瀛�")
}
let fileExist = (os.stat(path)[1] === 0)
if (!fileExist) {
- throw new Error('File not found: ' + path)
+ throw new Error('鏂囦欢鏈壘鍒�: ' + path)
}
- // 1. Check available disk space
+ // 1. 妫�鏌ュ彲鐢ㄧ鐩樼┖闂�
checkDiskSpace(size)
- // 2. Verify MD5 checksum
- if (!verifyMD5(path, md5)) {
- throw new Error('MD5 verification failed')
- }
+ // 2. 楠岃瘉 MD5 鏍¢獙鍜�
+ // if (!verifyMD5(path, md5)) {
+ // throw new Error('MD5 楠岃瘉澶辫触')
+ // }
- // 3. Move package to upgrade directory
+ // 3. 灏嗗寘绉诲姩鍒板崌绾х洰褰�
com.systemBrief(`mv ${path} ${ota.UPGRADE_TARGET} `)
com.systemBrief(`sync`)
}
@@ -99,7 +103,7 @@
if (requiredKb) {
const df = parseInt(com.systemWithRes(ota.DF_CMD, 1000))
if (df < 3 * requiredKb) {
- throw new Error('Insufficient disk space for upgrade')
+ throw new Error('鍗囩骇纾佺洏绌洪棿涓嶈冻')
}
}
}
@@ -107,47 +111,48 @@
function verifyMD5(filePath, expectedMD5) {
const hash = com.md5HashFile(filePath)
const actualMD5 = hash.map(v => v.toString(16).padStart(2, '0')).join('')
+ log.info("actualMD5:" + actualMD5)
return actualMD5 === expectedMD5
}
/**
- * Trigger device reboot
- * Call this after successful upgrade to apply changes.
+ * 瑙﹀彂璁惧閲嶅惎
+ * 鍦ㄦ垚鍔熷崌绾у悗璋冪敤姝ゆ柟娉曚互搴旂敤鏇存敼銆�
*/
ota.reboot = function () {
com.asyncReboot(2)
}
-//-------------------------DEPRECATED-------------------
+//-------------------------宸插簾寮�-------------------
ota.OTA_ROOT = '/ota'
ota.OTA_RUN = ota.OTA_ROOT + '/run.sh'
/**
- * @deprecated Use updateHttp() instead
- * Legacy upgrade method with custom script support.
- * Downloads, extracts, and executes custom upgrade scripts.
- * @param {string} url Required. HTTP URL for downloading the upgrade package
- * @param {string} md5 Required. MD5 hash for integrity verification (32-char lowercase hex)
- * @param {number} size Optional. Package size in KB for disk space validation
- * @param {string} shell Optional. Custom upgrade script content
- * @param {number} timeout Optional. Connection timeout in seconds (default: 3)
+ * @deprecated 浣跨敤 updateHttp() 浠f浛
+ * 鏀寔鑷畾涔夎剼鏈殑鏃у崌绾ф柟娉曘��
+ * 涓嬭浇銆佹彁鍙栧苟鎵ц鑷畾涔夊崌绾ц剼鏈��
+ * @param {string} url 蹇呭~銆備笅杞藉崌绾у寘鐨� HTTP URL
+ * @param {string} md5 蹇呭~銆傜敤浜庡畬鏁存�ч獙璇佺殑 MD5 鍝堝笇鍊硷紙32 浣嶅皬鍐欏崄鍏繘鍒讹級
+ * @param {number} size 鍙�夈�傜敤浜庣鐩樼┖闂撮獙璇佺殑鍖呭ぇ灏忥紙KB锛�
+ * @param {string} shell 鍙�夈�傝嚜瀹氫箟鍗囩骇鑴氭湰鍐呭
+ * @param {number} timeout 鍙�夈�傝繛鎺ヨ秴鏃舵椂闂达紙绉掞級锛岄粯璁わ細3
*/
ota.update = function (url, md5, size, shell, timeout = 3) {
if (!url || !md5) {
- throw new Error("'url' and 'md5' parameters are required")
+ throw new Error("'url' 鍜� 'md5' 鍙傛暟鏄繀濉殑")
}
if (size && (typeof size != 'number')) {
- throw new Error("'size' parameter must be a number")
+ throw new Error("'size' 鍙傛暟蹇呴』鏄暟瀛�")
}
- // 1. Check available disk space
+ // 1. 妫�鏌ュ彲鐢ㄧ鐩樼┖闂�
let df = parseInt(com.systemWithRes(ota.DF_CMD, 1000))
if (size) {
- if (df < (3 * size)) { // Require 3x package size for extraction
- throw new Error('Insufficient disk space for upgrade')
+ if (df < (3 * size)) { // 闇�瑕� 3 鍊嶅寘澶у皬鐢ㄤ簬鎻愬彇
+ throw new Error('鍗囩骇纾佺洏绌洪棿涓嶈冻')
}
}
- // 2. Download to specific directory
+ // 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} `) // Clean and create directory
+ com.systemBrief(`rm -rf ${ota.OTA_ROOT} && mkdir ${ota.OTA_ROOT} `) // 娓呯悊骞跺垱寤虹洰褰�
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)
@@ -158,65 +163,65 @@
fileExist = (os.stat(firmware)[1] === 0)
if (!fileExist) {
log.error("download result" + downloadRet)
- throw new Error('Download failed. Please check the URL: ' + url)
+ throw new Error('涓嬭浇澶辫触銆傝妫�鏌� URL: ' + url)
}
- // 3. Verify MD5 checksum
+ // 3. 楠岃瘉 MD5 鏍¢獙鍜�
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('MD5 verification failed')
+ throw new Error('MD5 楠岃瘉澶辫触')
}
- // 4. Extract package
+ // 4. 鎻愬彇鍖�
com.systemBrief(`mkdir ${temp} && unzip -o ${firmware} -d ${temp}`)
- // 5. Execute custom upgrade script if present
+ // 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. Create upgrade script
+ // 6. 鍒涘缓鍗囩骇鑴氭湰
if (!shell) {
- // Default: copy files and clean up
+ // 榛樿锛氬鍒舵枃浠跺苟娓呯悊
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('Failed to create upgrade script')
+ throw new Error('鍒涘缓鍗囩骇鑴氭湰澶辫触')
}
com.systemWithRes(`${ota.OTA_RUN}`)
}
/**
- * @deprecated Use updateHttp() instead
- * Legacy resource upgrade for tar.xz packages.
- * Specialized for upgrading resource files only.
- * @param {string} url Required. HTTP URL for downloading the upgrade package
- * @param {string} md5 Required. MD5 hash for integrity verification (32-char lowercase hex)
- * @param {number} size Optional. Package size in KB for disk space validation
- * @param {string} shell Optional. Custom upgrade script content
- * @param {number} timeout Optional. Connection timeout in seconds (default: 3)
+ * @deprecated 浣跨敤 updateHttp() 浠f浛
+ * 鐢ㄤ簬 tar.xz 鍖呯殑鏃ц祫婧愬崌绾с��
+ * 涓撻棬鐢ㄤ簬浠呭崌绾ц祫婧愭枃浠躲��
+ * @param {string} url 蹇呭~銆備笅杞藉崌绾у寘鐨� HTTP URL
+ * @param {string} md5 蹇呭~銆傜敤浜庡畬鏁存�ч獙璇佺殑 MD5 鍝堝笇鍊硷紙32 浣嶅皬鍐欏崄鍏繘鍒讹級
+ * @param {number} size 鍙�夈�傜敤浜庣鐩樼┖闂撮獙璇佺殑鍖呭ぇ灏忥紙KB锛�
+ * @param {string} shell 鍙�夈�傝嚜瀹氫箟鍗囩骇鑴氭湰鍐呭
+ * @param {number} timeout 鍙�夈�傝繛鎺ヨ秴鏃舵椂闂达紙绉掞級锛岄粯璁わ細3
*/
ota.updateResource = function (url, md5, size, shell, timeout = 3) {
if (!url || !md5) {
- throw new Error("'url' and 'md5' parameters are required")
+ throw new Error("'url' 鍜� 'md5' 鍙傛暟鏄繀濉殑")
}
if (size && (typeof size != 'number')) {
- throw new Error("'size' parameter must be a number")
+ throw new Error("'size' 鍙傛暟蹇呴』鏄暟瀛�")
}
- // 1. Check available disk space
+ // 1. 妫�鏌ュ彲鐢ㄧ鐩樼┖闂�
let df = parseInt(com.systemWithRes(ota.DF_CMD, 1000))
if (size) {
- if (df < (3 * size)) { // Require 3x package size for extraction
- throw new Error('Insufficient disk space for upgrade')
+ if (df < (3 * size)) { // 闇�瑕� 3 鍊嶅寘澶у皬鐢ㄤ簬鎻愬彇
+ throw new Error('鍗囩骇纾佺洏绌洪棿涓嶈冻')
}
}
- // 2. Download to specific directory
+ // 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} `) // Clean and create directory
+ com.systemBrief(`rm -rf ${ota.OTA_ROOT} && mkdir ${ota.OTA_ROOT} `) // 娓呯悊骞跺垱寤虹洰褰�
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)
@@ -225,18 +230,18 @@
}
fileExist = (os.stat(firmware)[1] === 0)
if (!fileExist) {
- throw new Error('Download failed. Please check the URL: ' + url)
+ throw new Error('涓嬭浇澶辫触銆傝妫�鏌� URL: ' + url)
}
- // 3. Verify MD5 checksum
+ // 3. 楠岃瘉 MD5 鏍¢獙鍜�
let md5Hash = com.md5HashFile(firmware)
md5Hash = md5Hash.map(v => v.toString(16).padStart(2, 0)).join('')
if (md5Hash != md5) {
- throw new Error('MD5 verification failed')
+ throw new Error('MD5 楠岃瘉澶辫触')
}
- // 4. Extract tar.xz package
+ // 4. 鎻愬彇 tar.xz 鍖�
com.systemBrief(`mkdir ${temp} && tar -xJvf ${firmware} -C ${temp}`)
- // 5. Create resource upgrade script
+ // 5. 鍒涘缓璧勬簮鍗囩骇鑴氭湰
if (!shell) {
shell = `
source=${temp}/vgapp/res/image/bk.png
@@ -264,7 +269,7 @@
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('Failed to create upgrade script')
+ throw new Error('鍒涘缓鍗囩骇鑴氭湰澶辫触')
}
com.systemWithRes(`${ota.OTA_RUN}`)
}
--
Gitblit v1.9.3