fix(iot): 修复事件上报处理逻辑,支持 button_event 事件
1. CleanRuleProcessorManager 新增 processEventData() 方法 - 区分处理属性上报和事件上报 - 事件上报从 identifier 字段提取事件类型 2. ButtonEventRuleProcessor.parseButtonId() 支持事件上报格式 - 属性上报:value 直接是按键ID - 事件上报:value 是 Map,包含 keyId 和 keyState 字段 Co-Authored-By: Claude (MiniMax-M2.1) <noreply@anthropic.com>
This commit is contained in:
@@ -62,10 +62,46 @@ public class CleanRuleProcessorManager {
|
|||||||
log.debug("[processMessage][设备({}) 处理{}上报,数据数量: {}]",
|
log.debug("[processMessage][设备({}) 处理{}上报,数据数量: {}]",
|
||||||
deviceId, "thing.event.post".equals(method) ? "事件" : "属性", data.size());
|
deviceId, "thing.event.post".equals(method) ? "事件" : "属性", data.size());
|
||||||
|
|
||||||
// 3. 顺序处理各数据项(与场景规则处理器保持一致)
|
// 3. 判断是属性上报还是事件上报
|
||||||
|
if (IotDeviceMessageMethodEnum.EVENT_POST.getMethod().equals(method)) {
|
||||||
|
// 事件上报:params 结构为 {identifier: "...", eventTime: ..., params: {...}}
|
||||||
|
processEventData(deviceId, data);
|
||||||
|
} else {
|
||||||
|
// 属性上报:直接遍历 key-value
|
||||||
data.forEach((identifier, value) ->
|
data.forEach((identifier, value) ->
|
||||||
processDataSafely(deviceId, identifier, value));
|
processDataSafely(deviceId, identifier, value));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 处理事件上报数据
|
||||||
|
* <p>
|
||||||
|
* 事件上报的 params 结构:
|
||||||
|
* {
|
||||||
|
* "identifier": "button_event",
|
||||||
|
* "eventTime": 1234567890,
|
||||||
|
* "params": { keyId: 1, keyState: 1 }
|
||||||
|
* }
|
||||||
|
*/
|
||||||
|
private void processEventData(Long deviceId, Map<String, Object> data) {
|
||||||
|
String identifier = (String) data.get("identifier");
|
||||||
|
if (identifier == null) {
|
||||||
|
log.warn("[processEventData][设备({}) 事件缺少 identifier]", deviceId);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Object params = data.get("params");
|
||||||
|
log.debug("[processEventData][设备({}) 事件: {}, params: {}]", deviceId, identifier, params);
|
||||||
|
|
||||||
|
// 路由到对应处理器
|
||||||
|
switch (identifier) {
|
||||||
|
case "button_event" ->
|
||||||
|
buttonEventRuleProcessor.processPropertyChange(deviceId, identifier, params);
|
||||||
|
default -> {
|
||||||
|
// 其他事件忽略
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 安全处理单个数据项
|
* 安全处理单个数据项
|
||||||
|
|||||||
@@ -222,12 +222,28 @@ public class ButtonEventRuleProcessor {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 解析按键ID
|
* 解析按键ID
|
||||||
|
* <p>
|
||||||
|
* 支持两种格式:
|
||||||
|
* 1. 属性上报:value 直接是按键ID(如 1)
|
||||||
|
* 2. 事件上报:value 是 Map,包含 keyId 字段(如 {keyId: 1, keyState: 1})
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
private Integer parseButtonId(Object value) {
|
private Integer parseButtonId(Object value) {
|
||||||
if (value == null) {
|
if (value == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 事件上报格式:value 是 Map,包含 keyId 字段
|
||||||
|
if (value instanceof Map) {
|
||||||
|
Map<String, Object> map = (Map<String, Object>) value;
|
||||||
|
Object keyId = map.get("keyId");
|
||||||
|
if (keyId instanceof Number) {
|
||||||
|
return ((Number) keyId).intValue();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 属性上报格式:value 直接是按键ID
|
||||||
if (value instanceof Number) {
|
if (value instanceof Number) {
|
||||||
return ((Number) value).intValue();
|
return ((Number) value).intValue();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user