Browse Source

简化物联网数据对象转换

欧阳劲驰 2 weeks ago
parent
commit
cb1f099ad3

+ 1 - 1
custom-gateway-zydma/src/main/java/com/shkpr/service/customgateway/zydma/components/IotCollector.java

@@ -152,7 +152,7 @@ public class IotCollector {
                             .collect(Collectors.toList());
                     //===================构建influxdb===================
                     results.addAll(dates.stream()
-                            .map(it -> dataToPoint(it, device, tag))
+                            .map(it -> it.toPoint(device, tag))
                             .collect(Collectors.toList()));
                 }
             }

+ 0 - 36
custom-gateway-zydma/src/main/java/com/shkpr/service/customgateway/zydma/constants/IotPlatformMetadata.java

@@ -3,11 +3,8 @@ package com.shkpr.service.customgateway.zydma.constants;
 import com.fasterxml.jackson.annotation.JsonAlias;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.shkpr.service.customgateway.core.constants.*;
-import com.shkpr.service.customgateway.core.domain.Device;
-import com.shkpr.service.customgateway.core.domain.DeviceTag;
 import com.shkpr.service.customgateway.core.domain.IntegrationKey;
 import com.shkpr.service.customgateway.core.properties.CallingProperties;
-import com.shkpr.service.customgateway.zydma.domain.IotPlatformData;
 import lombok.AllArgsConstructor;
 import lombok.Data;
 import lombok.Getter;
@@ -16,16 +13,12 @@ import org.apache.commons.lang3.ObjectUtils;
 import org.apache.http.HttpHeaders;
 import org.apache.http.client.fluent.Request;
 import org.apache.http.client.fluent.Response;
-import org.influxdb.dto.Point;
 import org.springframework.http.MediaType;
 
 import java.io.IOException;
 import java.time.LocalDateTime;
-import java.time.ZoneId;
 import java.time.format.DateTimeFormatter;
-import java.time.temporal.ChronoUnit;
 import java.util.*;
-import java.util.concurrent.TimeUnit;
 import java.util.stream.Collectors;
 
 /**
@@ -93,35 +86,6 @@ public abstract class IotPlatformMetadata {
     }
 
     /**
-     * 数据转influxdb
-     *
-     * @param data 数据
-     * @param tag  采集标签
-     * @return influxdb记录
-     */
-    public static Point dataToPoint(IotPlatformData.Data data, Device device, DeviceTag tag) {
-        //获取时间戳
-        long timestamp = data.getPt()
-                .truncatedTo(ChronoUnit.MINUTES)
-                .atZone(ZoneId.systemDefault())
-                .toInstant()
-                .toEpochMilli();
-        //构建influxdb
-        return Point
-                //表名
-                .measurement(tag.getMeasurement())
-                //设备id
-                .tag(InfluxdbMetadata.Tags.DEVICE_ID, device.getDeviceId())
-                //时间
-                .time(timestamp, TimeUnit.MILLISECONDS)
-                //值
-                .addField(tag.getField(), data.getPv())
-                //
-                .addField(InfluxdbMetadata.Tags.DEVICE_SN, device.getDeviceSn())
-                .build();
-    }
-
-    /**
      * 设备映射
      */
     @Getter

+ 36 - 0
custom-gateway-zydma/src/main/java/com/shkpr/service/customgateway/zydma/domain/IotPlatformData.java

@@ -1,11 +1,18 @@
 package com.shkpr.service.customgateway.zydma.domain;
 
 import com.fasterxml.jackson.annotation.JsonFormat;
+import com.shkpr.service.customgateway.core.constants.InfluxdbMetadata;
+import com.shkpr.service.customgateway.core.domain.Device;
+import com.shkpr.service.customgateway.core.domain.DeviceTag;
 import lombok.Data;
+import org.influxdb.dto.Point;
 import org.springframework.format.annotation.DateTimeFormat;
 
 import java.time.LocalDateTime;
+import java.time.ZoneId;
+import java.time.temporal.ChronoUnit;
 import java.util.List;
+import java.util.concurrent.TimeUnit;
 
 /**
  * 物联网平台数据
@@ -47,5 +54,34 @@ public class IotPlatformData {
          * 值
          */
         private Double pv;
+
+        /**
+         * 转influxdb对象
+         *
+         * @param device 设备
+         * @param tag    采集标签
+         * @return influxdb对象
+         */
+        public Point toPoint(Device device, DeviceTag tag) {
+            //获取时间戳
+            long timestamp = this.pt
+                    .truncatedTo(ChronoUnit.MINUTES)
+                    .atZone(ZoneId.systemDefault())
+                    .toInstant()
+                    .toEpochMilli();
+            //构建influxdb
+            return Point
+                    //表名
+                    .measurement(tag.getMeasurement())
+                    //设备id
+                    .tag(InfluxdbMetadata.Tags.DEVICE_ID, device.getDeviceId())
+                    //时间
+                    .time(timestamp, TimeUnit.MILLISECONDS)
+                    //值
+                    .addField(tag.getField(), this.pv)
+                    //远传id
+                    .addField(InfluxdbMetadata.Tags.DEVICE_SN, device.getDeviceSn())
+                    .build();
+        }
     }
 }