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/src/service/configService.js |  273 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 273 insertions(+), 0 deletions(-)

diff --git a/vf205_access/src/service/configService.js b/vf205_access/src/service/configService.js
new file mode 100644
index 0000000..77c0387
--- /dev/null
+++ b/vf205_access/src/service/configService.js
@@ -0,0 +1,273 @@
+/**
+ * 閰嶇疆鏈嶅姟妯″潡
+ * 璐熻矗閰嶇疆鐨勬牎楠屻�佷繚瀛樺拰搴旂敤锛屽寘鎷悇绉嶉厤缃」鐨勮鍒欓獙璇佸拰鍥炶皟澶勭悊
+ */
+import utils from '../common/utils/utils.js'
+import config from '../../dxmodules/dxConfig.js'
+import mqtt from '../../dxmodules/dxMqtt.js'
+import std from '../../dxmodules/dxStd.js'
+import ntp from '../../dxmodules/dxNtp.js'
+import common from '../../dxmodules/dxCommon.js'
+import driver from '../driver.js'
+import bus from '../../dxmodules/dxEventBus.js'
+import mqttService from './mqttService.js'
+import logger from '../../dxmodules/dxLogger.js'
+const configService = {}
+
+/**
+ * IP鍦板潃鏍¢獙姝e垯
+ * 鍖归厤浠ョ偣鍒嗗崄杩涘埗褰㈠紡琛ㄧず鐨� IP 鍦板潃锛屼緥濡傦細192.168.0.1
+ */
+const ipCheck = v => /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(v)
+
+/**
+ * IP鍦板潃鎴栧煙鍚嶆牎楠屾鍒欙紙甯︾鍙o級
+ * 鍖归厤IP鍦板潃鎴栧煙鍚嶏紝鍙�夋嫨甯︽湁绔彛鍙�
+ */
+const ipOrDomainCheckWithPort = v => /^(?:(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)|(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,})(:\d{1,5})?$/.test(v);
+
+/**
+ * 姝f暣鏁版牎楠屾鍒�
+ */
+const regpCheck = v => /^[1-9]\d*$/.test(v)
+
+/**
+ * 闈炶礋鏁存暟鏍¢獙姝e垯
+ */
+const regnCheck = v => /^([1-9]\d*|0{1})$/.test(v)
+
+/**
+ * 鎵�鏈夋敮鎸佺殑閰嶇疆椤圭殑妫�楠岃鍒欎互鍙婅缃垚鍔熷悗鐨勫洖璋�
+ * rule锛氭牎楠岃鍒欙紝杩斿洖true鏍¢獙鎴愬姛锛宖alse鏍¢獙澶辫触
+ * callback锛氶厤缃慨鏀硅Е鍙戝洖璋�
+ */
+const supported = {
+    // 浜鸿劯閰嶇疆
+    face: {
+        similarity: { rule: v => typeof v == 'number' && v >= 0 && v <= 1, callback: v => driver.face.faceUpdateConfig({ score: v }) },
+        livenessOff: { rule: v => [0, 1].includes(v), callback: v => driver.face.faceUpdateConfig({ livingCheckEnable: v }) },
+        livenessVal: { rule: v => typeof v == 'number' && v >= 0 && v <= 1, callback: v => driver.face.faceUpdateConfig({ livingScore: v }) },
+        showNir: { rule: v => [0, 1].includes(v), callback: v => driver.capturer.showNir(v) },
+        detectMask: { rule: v => [0, 1].includes(v), callback: v => driver.face.faceUpdateConfig({ detectMaskEnable: v }) },
+        stranger: { rule: v => [0, 1, 2].includes(v) },
+        voiceMode: { rule: v => [0, 1, 2].includes(v) },
+        voiceModeDate: { rule: v => typeof v == 'string' },
+    },
+    // MQTT閰嶇疆
+    mqtt: {
+        addr: { rule: ipOrDomainCheckWithPort },
+        clientId: { rule: v => typeof v == 'string' },
+        username: { rule: v => typeof v == 'string' },
+        password: { rule: v => typeof v == 'string' },
+        qos: { rule: v => [0, 1, 2].includes(v) },
+        prefix: { rule: v => typeof v == 'string' },
+        willtopic: { rule: v => typeof v == 'string' },
+        onlinecheck: { rule: v => [0, 1, 2].includes(v) },
+        timeout: { rule: regpCheck },
+    },
+    // 缃戠粶閰嶇疆
+    net: {
+        type: { rule: v => [0, 1, 2, 4].includes(v) }, // 缃戠粶绫诲瀷
+        dhcp: { rule: v => [1, 2, 3].includes(v) }, // DHCP璁剧疆
+        ip: { rule: ipCheck }, // IP鍦板潃
+        gateway: { rule: ipCheck }, // 缃戝叧鍦板潃
+        dns: { rule: v => !v.split(",").some(ip => !ipCheck(ip)) }, // DNS鏈嶅姟鍣�
+        mask: { rule: ipCheck }, // 瀛愮綉鎺╃爜
+        mac: { rule: v => typeof v == 'string' }, // MAC鍦板潃
+        ssid: { rule: v => typeof v == 'string' }, // WiFi SSID
+        psk: { rule: v => typeof v == 'string' }, // WiFi瀵嗙爜
+    },
+    // NTP鏃堕棿鍚屾閰嶇疆
+    ntp: {
+        ntp: { rule: v => [0, 1].includes(v) }, // NTP寮�鍏�
+        server: { rule: ipCheck }, // NTP鏈嶅姟鍣�
+        gmt: { rule: v => typeof v == 'number' && v >= 0 && v <= 24, callback: v => ntp.updateGmt(v) }, // 鏃跺尯璁剧疆
+    },
+    // 绯荤粺閰嶇疆
+    sys: {
+        uuid: { rule: v => typeof v == 'string' }, // 璁惧UUID
+        model: { rule: v => typeof v == 'string' }, // 璁惧鍨嬪彿
+        mode: { rule: v => typeof v == 'string', callback: v => setMode(v) }, // 璁惧妯″紡
+        sn: { rule: v => typeof v == 'string' }, // 璁惧搴忓垪鍙�
+        version: { rule: v => typeof v == 'string' }, // 绯荤粺鐗堟湰
+        releaseDate: { rule: v => typeof v == 'string' }, // 鍙戝竷鏃ユ湡
+        nfc: { rule: v => [0, 1].includes(v) }, // NFC寮�鍏�
+        nfcIdentityCardEnable: { rule: v => [1, 3].includes(v) }, // 浜戣瘉寮�鍏�
+        pwd: { rule: v => [0, 1].includes(v) }, // 瀵嗙爜寮�闂ㄥ紑鍏�
+        strangerImage: { rule: v => [0, 1].includes(v) }, // 闄岀敓浜轰繚瀛樺浘鐗囧紑鍏�
+        accessImageType: { rule: v => [0, 1].includes(v) }, // 閫氳鍥剧墖绫诲瀷
+        interval: { rule: regnCheck }, // 绯荤粺闂撮殧璁剧疆
+    },
+    // 闂ㄧ閰嶇疆
+    access: {
+        relayTime: { rule: regpCheck }, // 缁х數鍣ㄥ姩浣滄椂闂�
+        offlineAccessNum: { rule: regpCheck }, // 绂荤嚎寮�闂ㄦ鏁�
+        tamperAlarm: { rule: v => [0, 1].includes(v) }, // 闃叉媶鎶ヨ寮�鍏�
+    },
+    // 鍩虹閰嶇疆
+    base: {
+        firstLogin: { rule: v => [0, 1].includes(v) }, // 棣栨鐧诲綍鏍囧織
+        brightness: { rule: regnCheck, callback: v => driver.face.setDisplayBacklight(v) }, // 灞忓箷浜害
+        brightnessAuto: { rule: v => [0, 1].includes(v) }, // 鑷姩浜害寮�鍏�
+        showIp: { rule: v => [0, 1].includes(v), callback: v => driver.screen.hideIp(v) }, // 鏄惁鏄剧ずIP鍦板潃
+        showSn: { rule: v => [0, 1].includes(v), callback: v => driver.screen.hideSn(v) }, // 鏄惁鏄剧ず搴忓垪鍙�
+        appMode: { rule: v => [0, 1].includes(v), callback: v => driver.screen.appMode(v) }, // 搴旂敤妯″紡
+        screenOff: { rule: regnCheck, callback: v => bus.fire("screenManagerRefresh") }, // 鐔勫睆鏃堕棿
+        screensaver: { rule: regnCheck, callback: v => bus.fire("screenManagerRefresh") }, // 灞忓箷淇濇姢
+        volume: { rule: regnCheck, callback: v => driver.alsa.volume(v) }, // 闊抽噺璁剧疆
+        password: { rule: v => typeof v == 'string' && v.length >= 8 }, // 绠$悊鍛樺瘑鐮�
+        language: { rule: v => ["EN", "CN"].includes(v), callback: v => driver.screen.changeLanguage() }, // 璇█璁剧疆
+        showProgramCode: { rule: v => [0, 1].includes(v) }, // 鏄惁鏄剧ず绋嬪簭浠g爜
+        showIdentityCard: { rule: v => [0, 1].includes(v) }, // 鏄惁鏄剧ず韬唤璇佷俊鎭�
+        luminanceWhite: { rule: v => typeof v == 'number' && v >= 0 && v <= 100, callback: v => driver.pwm.luminanceWhite(v) }, // 鐧藉厜浜害
+        luminanceNir: { rule: v => typeof v == 'number' && v >= 0 && v <= 100, callback: v => driver.pwm.luminanceNir(v) }, // 绾㈠鍏変寒搴�
+    },
+    // 瀵嗙爜閫氳閰嶇疆
+    passwordAccess: {
+        passwordAccess: { rule: v => [0, 1].includes(v) }, // 瀵嗙爜閫氳寮�鍏�
+        emergencyPassword: { rule: v => typeof v == 'string' && v.length >= 8 }, // 搴旀�ュ紑浠撳瘑鐮�
+    },
+    // HTTP閰嶇疆
+    http: {
+        gas: { rule: v => typeof v == 'string' }, // HTTP_姘斾綋
+        status: { rule: v => typeof v == 'string' }, // HTTP_鐘舵��
+        safeInputAccess: { rule: v => typeof v == 'string' } // HTTP鎺ュ彛璺緞
+    },
+    // 浠撳粧鍚嶇О
+    houseName: {},
+    // 搴撳尯鍚嶇О
+    GranaryName: {},
+    // 鏇存柊閰嶇疆
+    update: {
+        gasTime: { rule: regpCheck }, // 姘斾綋娴撳害鏇存柊鏃堕棿锛堢锛�
+        statusTime: { rule: regpCheck } // 鐘舵�佷俊鎭洿鏂版椂闂达紙绉掞級
+    }
+}
+
+/**
+ * 闇�瑕侀噸鍚殑閰嶇疆椤�
+ */
+const needReboot = ["sys.nfc", "sys.nfcIdentityCardEnable", "ntp"]
+configService.needReboot = needReboot
+
+/**
+ * 淇敼璁惧妯″紡
+ * @param {string} params - 妯″紡鍙傛暟
+ */
+function setMode(params) {
+    common.systemWithRes(`echo 'app' > /etc/.app_v1`, 2)
+    common.setMode(params)
+}
+
+/**
+ * 閰嶇疆json鏍¢獙骞朵繚瀛�
+ * @param {object} data - 閰嶇疆json瀵硅薄
+ * @returns {boolean|string} true琛ㄧず鏍¢獙骞朵繚瀛樻垚鍔燂紝string琛ㄧず閿欒淇℃伅
+ */
+configService.configVerifyAndSave = function (data) {
+    let netFlag = false // 缃戠粶閰嶇疆鏍囧織
+    let mqttFlag = false // MQTT閰嶇疆鏍囧織
+    let isReboot = false // 鏄惁闇�瑕侀噸鍚�
+    
+    for (const key in data) {
+        if (key == 'net') {
+            netFlag = true
+        }
+        if (key == 'mqtt') {
+            mqttFlag = true
+        }
+        
+        // 妫�鏌ラ厤缃粍鏄惁鏀寔
+        if (!supported[key]) {
+            return key + " not supported"
+        }
+        
+        const item = data[key];
+        if (typeof item != 'object') {
+            // 椤跺眰閰嶇疆椤癸紝鐩存帴淇濆瓨
+            config.set(key, item)
+            continue
+        }
+        
+        // 妫�鏌ユ槸鍚﹂渶瑕侀噸鍚�
+        if (needReboot.includes(key)) {
+            isReboot = true
+        }
+        
+        for (const subKey in item) {
+            let option = supported[key][subKey]
+            if (isEmpty(option)) {
+                return subKey + " not supported"
+            }
+            
+            const value = item[subKey];
+            
+            // 妫�鏌ユ槸鍚﹂渶瑕侀噸鍚�
+            if (needReboot.includes(key + "." + subKey)) {
+                isReboot = true
+            }
+            
+            // 鏍¢獙閰嶇疆鍊�
+            if (!option.rule || option.rule(value)) {
+                // 娌℃湁鏍¢獙瑙勫垯榛樿鏍¢獙閫氳繃
+                config.set(key + "." + subKey, value)
+                if (option.callback) {
+                    // 鎵ц閰嶇疆璁剧疆鍥炶皟
+                    option.callback(value)
+                }
+            } else {
+                return value + " check failure"
+            }
+        }
+    }
+    
+    // 淇濆瓨閰嶇疆
+    config.save()
+    
+    // 妫�鏌ラ渶瑕侀噸鍚殑閰嶇疆锛�3绉掑悗閲嶅惎
+    if (isReboot) {
+        driver.screen.upgrade({ title: "confirm.restartDevice", content: "confirm.restartDeviceDis" })
+        common.asyncReboot(3)
+    }
+    
+    // 澶勭悊缃戠粶閰嶇疆鍙樻洿
+    if (netFlag) {
+        // 绛夊緟 1 绉� 鍥犱负闇�瑕佽繑鍥� mqtt
+        std.setTimeout(() => {
+            bus.fire("switchNetworkType", config.get("net.type"))
+        }, 1000);
+    }
+    
+    // 澶勭悊MQTT閰嶇疆鍙樻洿
+    if (mqttFlag) {
+        let option = { 
+            mqttAddr: config.get("mqtt.addr"), 
+            clientId: config.get('mqtt.clientId') + std.genRandomStr(3), 
+            subs: mqttService.getTopics(), 
+            username: config.get("mqtt.username"), 
+            password: config.get("mqtt.password"), 
+            qos: config.get("mqtt.qos"), 
+            willTopic: config.get("mqtt.willTopic"), 
+            willMessage: JSON.stringify({ "uuid": config.get("sys.uuid") }) 
+        }
+        logger.info("閲嶅惎mqtt", JSON.stringify(option))
+        // 閿�姣� mqtt 閲嶆柊 init
+        bus.fire(mqtt.RECONNECT, option)
+    }
+    
+    // 瑙﹀彂閰嶇疆鏇存柊浜嬩欢锛岄�氱煡鍏朵粬妯″潡閰嶇疆宸叉洿鏂�
+    bus.fire('configUpdated')
+    
+    return true
+}
+
+/**
+ * 鍒ょ┖鍑芥暟
+ * @param {*} value - 瑕佸垽鏂殑鍊�
+ * @returns {boolean} true琛ㄧず鍊间负绌猴紝false琛ㄧず鍊间笉涓虹┖
+ */
+function isEmpty(value) {
+    return value === undefined || value === null
+}
+
+export default configService
\ No newline at end of file

--
Gitblit v1.9.3