BLE Mesh 状态上报格式 (ble_out)
网关上报的设备状态同样为 hex 格式,hapi 解析流程:
状态解码 (decodeStatus)
decodeStatus(type, subtype, message) 逐段解析 MSG_TYPE,每段格式:<MSG_TYPE><data>,data 长度由 messageLength 表决定。
MSG_TYPE 与字段映射
| MSG_TYPE | 字段名 | 数据长度 | 解码方式 | 取值 |
|---|---|---|---|---|
02 | on_off / sub_on_off | 1 字节 | bit 位解析 | on / off |
03 | brightness | 1 字节 | parseInt | 0-100 |
04 | colour_temperature | 1 字节 | parseInt | 0-100 |
05 | curtain_run | 1 字节 | messageMap | open / close / stop |
06 | position | 1 字节 | parseInt | 0-100 |
0a | door_sensor | 1 字节 | messageMap | close / open |
0c | human_body | 1 字节 | messageMap | false / true |
0e | plug_card | 1 字节 | 直接判断 | true(有卡) / false(无卡) |
15 | passthrough | 剩余全部 | 原始 hex | 透传数据 |
16 | sensor_temperature | 2 字节 | readInt16LE / 100 | -40~85(℃) |
1b | ac_temperature | 1 字节 | parseInt | 16-30 |
1c | ac_wind_speed | 1 字节 | messageMap | high / middle / low / auto |
1d | ac_mode | 1 字节 | messageMap | cold / warm / fan / auto |
45 | sub_on_off(8 路) | 1 字节 | bit 位解析 | on / off |
9c | sub_brightness | 2 字节 | index + value | 子灯亮度数组 |
messageMap 值映射
| 字段 | 0 | 1 | 2 | 3 | 4 |
|---|---|---|---|---|---|
curtain_run | — | open | close | stop | — |
door_sensor | close | open | — | — | — |
human_body | false | true | — | — | — |
ac_wind_speed | — | high | middle | low | auto |
ac_mode | — | cold | warm | fan | auto |
开关状态解码 (MSG_TYPE=02/45)
单路开关(subtype=0):
data = 0b10 → on
data = 0b01 → off多路开关(subtype>0):每路 2 bit,10=on, 01=off, 00=不变
bit 7 6 5 4 3 2 1 0
[4][3][2][1] 4 路(MSG_TYPE=02)8 路开关使用 MSG_TYPE=45,第二个字节编码 5-8 路。
插卡取电解码 (MSG_TYPE=0e)
| data 值 | 含义 | 解码结果 |
|---|---|---|
00 | 无卡 | plug_card = false |
01 | 有卡 | plug_card = true |
02 | 上电插卡虚拟事件 | plug_card = true |
03 | 无卡取电拔卡 | plug_card = false |
温度传感器解码 (MSG_TYPE=16)
2 字节小端序有符号整数,除以 100 得到实际温度:
javascript
const buf = Buffer.from(data, 'hex');
const temperature = buf.readInt16LE(0);
ret.sensor_temperature = temperature / 100;子灯亮度解码 (MSG_TYPE=9c)
<index><brightness>| 字段 | 长度 | 说明 |
|---|---|---|
| index | 1 字节 | 子灯索引 |
| brightness | 1 字节 | 亮度值(0-100),子灯关闭时为 0 |
messageLength 表
部分 MSG_TYPE 的数据长度(用于分段解析):
| MSG_TYPE | 数据长度(字节) |
|---|---|
0f | 3 |
12 | 4 |
13 | 2 |
16 | 2 |
19 | 2 |
1a | 3 |
22 | 4 |
23 | 2 |
27 | 3 |
28 | 10 |
29 | 8 |
2e | 4 |
38 | 2 |
39 | 8 |
43 | 13 |
未在表中的 MSG_TYPE 默认数据长度为 1 字节。
状态缓存
解码后的状态写入 Redis Hash,key 格式:SH:ST:{mq_id},field 为设备 naddr,value 为完整状态 hex 字符串。
