|
@@ -0,0 +1,63 @@
|
|
|
+package vip.xiaonuo.coldchain.modular.service.dataprocess.handler.impl;
|
|
|
+
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.context.ApplicationEventPublisher;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import rk.netDevice.sdk.p2.RealTimeData;
|
|
|
+import vip.xiaonuo.coldchain.modular.bean.influxdb.SensorData;
|
|
|
+import vip.xiaonuo.coldchain.modular.event.SensorDataEvent;
|
|
|
+import vip.xiaonuo.coldchain.modular.service.dataprocess.handler.AbstractColdChainDataHandler;
|
|
|
+import vip.xiaonuo.coldchain.modular.service.dataprocess.model.RenKeColdChainMessageData;
|
|
|
+
|
|
|
+import java.time.Instant;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author jackzhou
|
|
|
+ * @version 1.0
|
|
|
+ * @project jfcloud-coldchain
|
|
|
+ * @description
|
|
|
+ * @date 2024/11/12 13:03:45
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Component
|
|
|
+@RequiredArgsConstructor
|
|
|
+public class RenKeColdChainDataHandler extends AbstractColdChainDataHandler<RenKeColdChainMessageData> {
|
|
|
+ private final ApplicationEventPublisher eventPublisher; // To publish events
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean handleRealTimeData(RenKeColdChainMessageData renKeColdChainMessageData) {
|
|
|
+ RealTimeData data = renKeColdChainMessageData.getRealTimeData();
|
|
|
+ final int deviceId = data.getDeviceId();
|
|
|
+ data.getNodeList().forEach(nodeData -> {
|
|
|
+ SensorData sensorData = new SensorData();
|
|
|
+ // 如果记录时间为空,使用当前时间
|
|
|
+ if (nodeData.getRecordTime() == null) {
|
|
|
+ Instant defaultTime = Instant.now();
|
|
|
+ sensorData.setTime(defaultTime);
|
|
|
+ } else {
|
|
|
+ sensorData.setTime(nodeData.getRecordTime().toInstant());
|
|
|
+ }
|
|
|
+ // 只处理有效数据:温度和湿度不为0
|
|
|
+ if (nodeData.getTem() != 0.0 || nodeData.getHum() != 0.0) {
|
|
|
+ log.info("记录ID: {}, 记录时间: {}, 温度: {}, 湿度: {}", deviceId + " : " + nodeData.getNodeId(), nodeData.getRecordTime(), nodeData.getTem(), nodeData.getHum());
|
|
|
+ sensorData.setTemperature(nodeData.getTem());
|
|
|
+ sensorData.setHumidity(nodeData.getHum());
|
|
|
+ sensorData.setLat(nodeData.getLat());
|
|
|
+ sensorData.setLng(nodeData.getLng());
|
|
|
+ sensorData.putTag("name","周小杰");
|
|
|
+ writeData(sensorData);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ return Boolean.TRUE;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 异步将处理后的数据写入 InfluxDB
|
|
|
+ *
|
|
|
+ * @param sensorData 传感器数据
|
|
|
+ */
|
|
|
+ public void writeData(SensorData sensorData) {
|
|
|
+ eventPublisher.publishEvent(new SensorDataEvent(this, sensorData));
|
|
|
+ }
|
|
|
+}
|