Skip to content

BLE Mesh 状态上报格式 (ble_out)

网关上报的设备状态同样为 hex 格式,hapi 解析流程:

状态解码 (decodeStatus)

decodeStatus(type, subtype, message) 逐段解析 MSG_TYPE,每段格式:<MSG_TYPE><data>,data 长度由 messageLength 表决定。

MSG_TYPE 与字段映射

MSG_TYPE字段名数据长度解码方式取值
02on_off / sub_on_off1 字节bit 位解析on / off
03brightness1 字节parseInt0-100
04colour_temperature1 字节parseInt0-100
05curtain_run1 字节messageMapopen / close / stop
06position1 字节parseInt0-100
0adoor_sensor1 字节messageMapclose / open
0chuman_body1 字节messageMapfalse / true
0eplug_card1 字节直接判断true(有卡) / false(无卡)
15passthrough剩余全部原始 hex透传数据
16sensor_temperature2 字节readInt16LE / 100-40~85(℃)
1bac_temperature1 字节parseInt16-30
1cac_wind_speed1 字节messageMaphigh / middle / low / auto
1dac_mode1 字节messageMapcold / warm / fan / auto
45sub_on_off(8 路)1 字节bit 位解析on / off
9csub_brightness2 字节index + value子灯亮度数组

messageMap 值映射

字段01234
curtain_runopenclosestop
door_sensorcloseopen
human_bodyfalsetrue
ac_wind_speedhighmiddlelowauto
ac_modecoldwarmfanauto

开关状态解码 (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>
字段长度说明
index1 字节子灯索引
brightness1 字节亮度值(0-100),子灯关闭时为 0

messageLength 表

部分 MSG_TYPE 的数据长度(用于分段解析):

MSG_TYPE数据长度(字节)
0f3
124
132
162
192
1a3
224
232
273
2810
298
2e4
382
398
4313

未在表中的 MSG_TYPE 默认数据长度为 1 字节。

状态缓存

解码后的状态写入 Redis Hash,key 格式:SH:ST:{mq_id},field 为设备 naddr,value 为完整状态 hex 字符串。