89 lines
2.3 KiB
JavaScript
89 lines
2.3 KiB
JavaScript
import { parseRadarData } from '../parsers'
|
|
|
|
export function createRadarModule(ble) {
|
|
function requireAuthorized() {
|
|
const state = ble.getState()
|
|
|
|
// 协议要求:获取雷达校准数据前必须先完成密钥匹配。
|
|
if (!state.auth || state.auth.keyMatched !== true) {
|
|
throw new Error('请先完成密钥匹配,再执行雷达命令')
|
|
}
|
|
}
|
|
|
|
async function sendCommand(commandByte) {
|
|
const { cmd } = ble.getState().characteristics
|
|
|
|
if (!cmd || !cmd.uuid) {
|
|
throw new Error('未发现 UUID_CMD 特征值')
|
|
}
|
|
|
|
await ble.writeCharacteristic(cmd.uuid, new Uint8Array([commandByte]))
|
|
}
|
|
|
|
async function startRadarStream() {
|
|
const profile = ble.getProfile()
|
|
|
|
// 协议1.2.2:先密钥匹配,再向 UUID_CMD 写 0xA1。
|
|
requireAuthorized()
|
|
await sendCommand(profile.radar.startCommand)
|
|
return true
|
|
}
|
|
|
|
async function stopRadarStream() {
|
|
const profile = ble.getProfile()
|
|
|
|
// 停止同样要求处于鉴权通过态,避免误发到未鉴权连接。
|
|
requireAuthorized()
|
|
await sendCommand(profile.radar.stopCommand)
|
|
return true
|
|
}
|
|
|
|
async function setNarrowMode(enabled) {
|
|
const profile = ble.getProfile()
|
|
|
|
if (!profile.cmd.supportsNarrowMode) {
|
|
throw new Error('当前设备不支持窄床模式设置')
|
|
}
|
|
|
|
requireAuthorized()
|
|
await sendCommand(enabled ? 0x7D : 0x7C)
|
|
}
|
|
|
|
async function writeFallParams(rawParams) {
|
|
const profile = ble.getProfile()
|
|
|
|
if (!profile.cmd.supportsFallParam67) {
|
|
throw new Error('当前设备不支持 0x67 跌倒参数写入')
|
|
}
|
|
|
|
requireAuthorized()
|
|
|
|
const payload = rawParams instanceof Uint8Array ? rawParams : new Uint8Array(rawParams)
|
|
const packet = new Uint8Array(1 + payload.length)
|
|
packet[0] = 0x67
|
|
packet.set(payload, 1)
|
|
|
|
const { cmd } = ble.getState().characteristics
|
|
if (!cmd || !cmd.uuid) {
|
|
throw new Error('未发现 UUID_CMD 特征值')
|
|
}
|
|
|
|
await ble.writeCharacteristic(cmd.uuid, packet)
|
|
}
|
|
|
|
function parseNotifyPayload(payload) {
|
|
const profile = ble.getProfile()
|
|
return parseRadarData(payload, profile.id)
|
|
}
|
|
|
|
return {
|
|
sendCommand,
|
|
startRadarStream,
|
|
stopRadarStream,
|
|
setNarrowMode,
|
|
writeFallParams,
|
|
parseNotifyPayload,
|
|
requireAuthorized
|
|
}
|
|
}
|