feat: add user deactivation and vendor BLE protocol

这个提交包含在:
Guoguo
2026-05-11 21:14:41 +08:00
父节点 c95d47e0c1
当前提交 583fcb7e3d
修改 16 个文件,包含 698 行新增52 行删除
+56
查看文件
@@ -61,6 +61,32 @@ function writeCommand(type, payload) {
})
}
function writeRawCommand(bytes) {
return new Promise(function (resolve, reject) {
if (!connection.isConnected()) {
reject({ error_code: 0x0C, error_msg: 'ERR_BLE_DISCONNECTED' })
return
}
var chars = connection.getChars()
if (!chars.command) {
reject({ error_code: 0xFF, error_msg: 'command characteristic not found' })
return
}
wx.writeBLECharacteristicValue({
deviceId: connection.getDeviceId(),
serviceId: chars.command.serviceId,
characteristicId: chars.command.uuid,
value: protocol.bytesToBuffer(bytes),
success: function () {
resolve({ message: 'success', bytes: bytes })
},
fail: function () {
reject({ error_code: 0x0C, error_msg: 'ERR_BLE_DISCONNECTED' })
}
})
})
}
function writeCommandWithRetry(type, payload, maxRetries) {
if (maxRetries === undefined) maxRetries = 3
var attempt = 0
@@ -122,6 +148,10 @@ function readDeviceInfo() {
}
function setParams(options) {
if (protocol.PROTOCOL_MODE === 'vendor_33') {
return writeRawCommand(protocol.buildVendorCommand33(options || {}))
}
var regionMask = options.region_mask || protocol.REGION.FULL_FACE
var wavelength = options.wavelength || protocol.WAVELENGTH.R
var brightness = options.brightness || 200
@@ -142,19 +172,41 @@ function setParams(options) {
}
function startTreatment(regionMask) {
if (protocol.PROTOCOL_MODE === 'vendor_33') {
return Promise.resolve({ message: 'vendor_33 uses setParams write as start', region_mask: regionMask })
}
var mask = regionMask || protocol.REGION.FULL_FACE
return writeCommandWithRetry(protocol.CMD.START, [mask])
}
function stopTreatment() {
if (protocol.PROTOCOL_MODE === 'vendor_33') {
return writeRawCommand(protocol.buildVendorCommand33({
region_mask: 0,
wavelength: protocol.WAVELENGTH.R,
brightness: 0,
current_gain: 0,
hold_time: 0
}))
}
return writeCommandWithRetry(protocol.CMD.STOP, [])
}
function queryStatus() {
if (protocol.PROTOCOL_MODE === 'vendor_33') {
return Promise.resolve({ message: 'query status not defined by vendor_33 protocol' })
}
return writeCommandWithRetry(protocol.CMD.QUERY_STATUS, [])
}
function bindDevice(userId, bindToken) {
if (protocol.PROTOCOL_MODE === 'vendor_33') {
setTimeout(function () {
connection.emit('bind_result', { success: true, skipped_ble_bind: true })
}, 0)
return Promise.resolve({ message: 'BLE bind not defined by vendor_33 protocol', user_id: userId, bind_token: bindToken })
}
var userBytes = protocol.uint32ToBytes(parseInt(userId, 10))
var tokenBytes = protocol.hexToBytes(bindToken)
var ts = Math.floor(Date.now() / 1000)
@@ -165,6 +217,9 @@ function bindDevice(userId, bindToken) {
}
function unbindDevice(userId) {
if (protocol.PROTOCOL_MODE === 'vendor_33') {
return Promise.resolve({ message: 'BLE unbind not defined by vendor_33 protocol', user_id: userId })
}
var userBytes = protocol.uint32ToBytes(parseInt(userId, 10))
var payload = [0x02].concat(userBytes)
return writeCommandWithRetry(protocol.CMD.UNBIND, payload)
@@ -175,6 +230,7 @@ module.exports = {
clearPendingAcks: clearPendingAcks,
writeCommand: writeCommand,
writeRawCommand: writeRawCommand,
writeCommandWithRetry: writeCommandWithRetry,
readDeviceInfo: readDeviceInfo,