lgq
2026-03-31 e491cdb48129752324c4e3764f99bd9203c56dec
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/**
 * 二维码/条形码处理服务模块
 * 处理各类二维码/条形码的解析和处理,包括配置码、云证激活码和通行码
 */
import common from '../../dxmodules/dxCommon.js';
import log from '../../dxmodules/dxLogger.js'
import qrRule from '../../dxmodules/dxQrRule.js'
import std from '../../dxmodules/dxStd.js'
import config from '../../dxmodules/dxConfig.js'
import base64 from '../../dxmodules/dxBase64.js'
import dxMap from '../../dxmodules/dxMap.js'
import ota from "../../dxmodules/dxOta.js";
import bus from "../../dxmodules/dxEventBus.js"
import sqliteService from "./sqliteService.js";
import driver from '../driver.js';
import utils from '../common/utils/utils.js';
import configConst from '../common/consts/configConst.js';
import configService from './configService.js';
import accessService from './accessService.js';
import logger from '../../dxmodules/dxLogger.js';
const codeService = {}
 
/**
 * 接收消息并处理
 * @param {ArrayBuffer} data - 接收到的原始数据
 */
codeService.receiveMsg = function (data) {
    log.info('[codeService] receiveMsg :' + JSON.stringify(data))
    // 将ArrayBuffer转换为十六进制字符串,再转换为UTF8字符串
    let str = common.utf8HexToStr(common.arrayBufferToHexString(data))
    // 调用code方法处理字符串
    this.code(str)
}
 
/**
 * 比较两个字符串的前N个字符是否相等
 * @param {string} str1 - 第一个字符串
 * @param {string} str2 - 第二个字符串
 * @param {number} N - 比较的字符数
 * @returns {boolean} 前N个字符是否相等
 */
function comparePrefix (str1, str2, N) {
    let substring1 = str1.substring(0, N);
    let substring2 = str2.substring(0, N);
    return substring1 === substring2;
}
 
/**
 * 处理二维码/条形码数据
 * @param {string} data - 二维码/条形码的原始数据
 */
codeService.code = function (data) {
    log.info('[codeService] code :' + data)
    // 格式化二维码数据
    data = qrRule.formatCode(data, sqliteService)
    
    // 判断二维码类型
    if (data.type == 'config' || comparePrefix(data.code, "___VF102_CONFIG_V1.1.0___", "___VF102_CONFIG_V1.1.0___".length)) {
        // 配置码处理
        configCode(data.code)
    } else if (comparePrefix(data.code, "___VBAR_ID_ACTIVE_V", "___VBAR_ID_ACTIVE_V".length)) {
        // 云证激活码处理
        let activeResute = driver.eid.active(config.get("sys.sn"), config.get("sys.appVersion"), config.get("sys.mac"), data.code);
        log.info("[codeService] code: activeResute " + activeResute)
        if (activeResute === 0) {
            log.info("[codeService] code: 云证激活成功")
            driver.screen.upgrade({ title: "confirm.cloudCertActive", content: "confirm.cloudCertActiveSuccess" })
        } else {
            log.info("[codeService] code: 云证激活失败")
            driver.screen.upgrade({ title: "confirm.cloudCertActive", content: "confirm.cloudCertActiveFail" })
        }
    } else {
        // 通行码处理
        log.info("解析通行码:", JSON.stringify(data))
        bus.fire("access", { data })
    }
}
 
/**
 * 配置码处理
 * @param {string} code - 配置码数据
 * @param {boolean} configType - 配置类型标志
 */
function configCode (code, configType) {
    // 配置码校验暂时注释
    // if (!checkConfigCode(code)) {
    //     log.error("配置码校验失败")
    //     return
    // }
    
    // 解析配置码为JSON对象
    let json = utils.parseString(code)
    if (Object.keys(json).length <= 0) {
        try {
            json = JSON.parse(code.slice(code.indexOf("{"), code.lastIndexOf("}") + 1))
        } catch (error) {
            log.error(error)
        }
    }
    log.info("解析配置码:", JSON.stringify(json))
    
    // 模式切换处理
    if (!utils.isEmpty(json.w_model)) {
        try {
            common.setMode(json.w_model)
            common.asyncReboot(1)
        } catch (error) {
            log.error(error, error.stack)
            log.info('切换失败不做任何处理');
        }
        return
    }
    
    let map = dxMap.get("UPDATE")
    // 扫码升级处理
    if (json.update_flag === 1) {
        if (!driver.net.getStatus()) {
            driver.alsa.play(`/app/code/resource/${config.get("base.language") == "CN" ? "CN" : "EN"}/wav/network.wav`)
            return
        }
        if (map.get("updateFlag")) {
            return
        }
        map.put("updateFlag", true)
        try {
            driver.screen.upgrade({ title: "confirm.upgrade", content: "confirm.upgrading" })
            ota.updateHttp(json.update_addr, json.update_md5, 300)
            driver.screen.upgrade({ title: "confirm.upgrade", content: "confirm.upgradeSuccess" })
        } catch (error) {
            logger.info(error.message)
            driver.screen.upgrade({ title: "confirm.upgrade", content: "confirm.upgradeFail" })
        } finally {
            map.del("updateFlag")
        }
        common.asyncReboot(3)
        return
    }
    
    // 设备配置处理
    let configData = {}
    for (let key in json) {
        let transKey
        if (configType == true) {
            transKey = key
        } else {
            transKey = configConst.getValueByKey(key)
        }
        if (transKey == undefined) {
            continue
        }
        let keys = transKey.split(".")
        if (utils.isEmpty(configData[keys[0]])) {
            configData[keys[0]] = {}
        }
        configData[keys[0]][keys[1]] = json[key]
    }
    
    let res = false
    if (Object.keys(configData).length > 0) {
        res = configService.configVerifyAndSave(configData)
    }
    
    if (typeof res != 'boolean') {
        log.error(res)
        return
    }
    
    if (res) {
        log.info("配置成功")
    } else {
        log.error("配置失败")
    }
    
    // 配置后重启
    if (json.reboot === 1) {
        driver.screen.warning({ msg: config.get("sysInfo.language") == 1 ? "Rebooting" : "重启中", beep: false })
        common.asyncReboot(1)
    }
}
 
/**
 * 校验配置码
 * @param {string} code - 配置码数据
 * @returns {boolean} 校验是否成功
 */
function checkConfigCode (code) {
    let password = config.get('sysInfo.com_passwd') || '1234567887654321'
    let lastIndex = code.lastIndexOf("--");
    if (lastIndex < 0) {
        lastIndex = code.lastIndexOf("__");
    }
    let firstPart = code.substring(0, lastIndex);
    let secondPart = code.substring(lastIndex + 2);
    let res
    try {
        res = base64.fromHexString(common.arrayBufferToHexString(common.hmac(firstPart, password)))
    } catch (error) {
        log.error(error)
        return false
    }
 
    return res == secondPart;
}
 
export default codeService