refactor(iot): 优化事件发布机制并修复状态值解析
Some checks failed
Java CI with Maven / build (11) (push) Has been cancelled
Java CI with Maven / build (17) (push) Has been cancelled
Java CI with Maven / build (8) (push) Has been cancelled

1. IntegrationEventPublisher 只保留设备状态变更事件发布
   - 注释掉 publishPropertyChanged 和 publishEventOccurred 接口
   - RocketMQIntegrationEventPublisher 对应实现改为注释

2. IotDevicePropertyServiceImpl 属性消息发布暂停
   - 注释掉 saveDeviceProperty 中的 publishPropertyMessage 调用
   - 注释掉 publishToIntegrationEventBus 中的实际发布逻辑

3. IotDeviceMessageServiceImpl 新增状态值解析兼容
   - 新增 parseStateValue 方法支持整数和字符串格式状态值
   - 支持 "online"/"offline" 字符串解析

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
lzh
2026-01-21 22:56:13 +08:00
parent 842b40596d
commit fa619710ef
4 changed files with 52 additions and 43 deletions

View File

@@ -26,13 +26,13 @@ public interface IntegrationEventPublisher {
*
* @param event 属性变更事件
*/
void publishPropertyChanged(DevicePropertyChangedEvent event);
// void publishPropertyChanged(DevicePropertyChangedEvent event);
/**
* 发布设备事件上报事件
*
* @param event 设备事件
*/
void publishEventOccurred(DeviceEventOccurredEvent event);
// void publishEventOccurred(DeviceEventOccurredEvent event);
}

View File

@@ -1,11 +1,9 @@
package com.viewsh.module.iot.core.integration.publisher;
import com.viewsh.module.iot.core.integration.config.IntegrationEventProperties;
import com.viewsh.module.iot.core.integration.constants.DeviceTags;
import com.viewsh.module.iot.core.integration.constants.IntegrationTopics;
import com.viewsh.module.iot.core.integration.event.DeviceEventOccurredEvent;
import com.viewsh.module.iot.core.integration.event.DevicePropertyChangedEvent;
import com.viewsh.module.iot.core.integration.event.DeviceStatusChangedEvent;
import com.viewsh.module.iot.core.integration.config.IntegrationEventProperties;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.messaging.Message;
@@ -47,39 +45,18 @@ public class RocketMQIntegrationEventPublisher implements IntegrationEventPublis
}
}
@Override
public void publishPropertyChanged(DevicePropertyChangedEvent event) {
try {
String tag = DeviceTags.fromProductKey(event.getProductKey());
String destination = IntegrationTopics.DEVICE_PROPERTY + ":" + tag;
Message<DevicePropertyChangedEvent> message = MessageBuilder.withPayload(event).build();
rocketMQTemplate.syncSend(destination, message, properties.getSendTimeoutMs());
log.debug("[publishPropertyChanged] 发布设备属性变更事件: eventId={}, deviceId={}, productKey={}, properties={}",
event.getEventId(), event.getDeviceId(), event.getProductKey(), event.getChangedIdentifiers());
} catch (Exception e) {
log.error("[publishPropertyChanged] 发布设备属性变更事件失败: eventId={}, deviceId={}",
event.getEventId(), event.getDeviceId(), e);
}
}
@Override
public void publishEventOccurred(DeviceEventOccurredEvent event) {
try {
// 使用 productKey 作为 Tag
String tag = DeviceTags.fromProductKey(event.getProductKey());
String destination = IntegrationTopics.DEVICE_EVENT + ":" + tag;
Message<DeviceEventOccurredEvent> message = MessageBuilder.withPayload(event).build();
rocketMQTemplate.syncSend(destination, message, properties.getSendTimeoutMs());
log.debug("[publishEventOccurred] 发布设备事件上报事件: eventId={}, deviceId={}, productKey={}, event={}",
event.getEventId(), event.getDeviceId(), event.getProductKey(), event.getEventIdentifier());
} catch (Exception e) {
log.error("[publishEventOccurred] 发布设备事件上报事件失败: eventId={}, deviceId={}",
event.getEventId(), event.getDeviceId(), e);
}
}
// @Override
// public void publishPropertyChanged(DevicePropertyChangedEvent event) {
// // 暂不处理属性变更事件
// log.debug("[publishPropertyChanged] 跳过设备属性变更事件发布: eventId={}, deviceId={}, productKey={}",
// event.getEventId(), event.getDeviceId(), event.getProductKey());
// }
//
// @Override
// public void publishEventOccurred(DeviceEventOccurredEvent event) {
// // 暂不处理设备事件上报
// log.debug("[publishEventOccurred] 跳过设备事件上报发布: eventId={}, deviceId={}, productKey={}, event={}",
// event.getEventId(), event.getDeviceId(), event.getProductKey(), event.getEventIdentifier());
// }
}