Prechádzať zdrojové kódy

增加设备厂家,增加瞬时压力字段

欧阳劲驰 2 týždňov pred
rodič
commit
e87a568826

+ 8 - 8
custom-gateway-core/src/main/java/com/shkpr/service/customgateway/core/components/DeviceIdGenerator.java

@@ -4,7 +4,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
 import com.global.base.log.LogLevelFlag;
 import com.global.base.log.LogPrintMgr;
 import com.shkpr.service.customgateway.core.constants.AreaCode;
-import com.shkpr.service.customgateway.core.constants.DeviceType;
+import com.shkpr.service.customgateway.core.constants.DeviceKind;
 import com.shkpr.service.customgateway.core.constants.LogFlagBusiType;
 import com.shkpr.service.customgateway.core.domain.Device;
 import com.shkpr.service.customgateway.core.domain.DeviceTag;
@@ -62,33 +62,33 @@ public class DeviceIdGenerator {
      * 生成设备
      *
      * @param areaCode 区号
-     * @param type     类型
+     * @param kind     种类
      * @param sn       远传设备id
      * @param name     设备名称
      * @return 设备
      */
-    public synchronized Device generateDevice(AreaCode areaCode, DeviceType type, List<DeviceTag> fields
+    public synchronized Device generateDevice(AreaCode areaCode, DeviceKind kind, List<DeviceTag> fields
             , String sn, String name) {
-        if (areaCode == null || type == null || StringUtils.isAnyBlank(sn, name)) return null;
+        if (areaCode == null || kind == null || StringUtils.isAnyBlank(sn, name)) return null;
 
         //构建设备
-        return new Device(generateDeviceId(areaCode, type), name, sn, type.getKey(), LocalDateTime.now(), fields);
+        return new Device(generateDeviceId(areaCode, kind), name, sn, kind.getKey(), null, LocalDateTime.now(), fields);
     }
 
     /**
      * 生成设备id
      *
      * @param areaCode 区号
-     * @param type     设备类型
+     * @param kind     设备种类
      * @return 设备id
      */
-    public synchronized String generateDeviceId(AreaCode areaCode, DeviceType type) {
+    public synchronized String generateDeviceId(AreaCode areaCode, DeviceKind kind) {
         //当前日期
         LocalDate now = LocalDate.now();
         //下一个序列
         int sequence = getNextSequence(now);
         //组装id
-        return String.format("%03d%02d%s%04d", areaCode.getCode(), type.getCode(), now.format(formatter), sequence);
+        return String.format("%03d%02d%s%04d", areaCode.getCode(), kind.getCode(), now.format(formatter), sequence);
     }
 
     /**

+ 54 - 12
custom-gateway-core/src/main/java/com/shkpr/service/customgateway/core/components/DeviceRegistry.java

@@ -5,11 +5,12 @@ import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
 import com.global.base.log.LogLevelFlag;
 import com.global.base.log.LogPrintMgr;
 import com.shkpr.service.customgateway.core.constants.DeviceField;
-import com.shkpr.service.customgateway.core.constants.DeviceType;
+import com.shkpr.service.customgateway.core.constants.DeviceKind;
 import com.shkpr.service.customgateway.core.constants.ExcelEnum;
 import com.shkpr.service.customgateway.core.constants.LogFlagBusiType;
 import com.shkpr.service.customgateway.core.domain.Device;
 import com.shkpr.service.customgateway.core.domain.DeviceExcel;
+import com.shkpr.service.customgateway.core.domain.DeviceTag;
 import com.shkpr.service.customgateway.core.utils.ExcelUtil;
 import org.apache.commons.lang3.ObjectUtils;
 import org.apache.commons.lang3.StringUtils;
@@ -192,6 +193,45 @@ public class DeviceRegistry {
     }
 
     /**
+     * 查找需要更新的设备
+     *
+     * @param deviceInfo 设备
+     * @return 需要新增的设备远传id
+     */
+    public Set<Device> findUpdatedDevices(Map<String, Set<String>> deviceInfo) {
+        //已存在的设备,k:设备SN,v:标签字段集合
+        Map<String, Set<String>> existingDeviceInfo = this.devices.stream()
+                .collect(Collectors.toMap(
+                        Device::getDeviceSn,
+                        it -> it.getTags().stream()
+                                .map(DeviceTag::getField)
+                                .collect(Collectors.toSet())
+                ));
+
+        //过滤不同的设备
+        return deviceInfo.entrySet().stream()
+                //过滤不通标签
+                .filter(entry -> {
+                    //新标签
+                    Set<String> newTags = entry.getValue();
+                    //已存在的标签
+                    Set<String> existingTags = existingDeviceInfo.get(entry.getKey());
+                    if (existingTags == null) return true;
+
+                    //比较标签
+                    return !existingTags.equals(newTags);
+                })
+                //提取当前设备
+                .map(entry -> this.devices.stream()
+                        .filter(device -> device.getDeviceSn().equals(entry.getKey()))
+                        .findFirst()
+                )
+                .filter(Optional::isPresent)
+                .map(Optional::get)
+                .collect(Collectors.toSet());
+    }
+
+    /**
      * 加载设备
      */
     public void loadDevices() {
@@ -244,24 +284,26 @@ public class DeviceRegistry {
      * @param path 路径
      */
     public void exportDevice(Path path) {
-        //类映射
-        Map<String, String> typeMap = Arrays.stream(DeviceType.values())
-                .collect(Collectors.toMap(DeviceType::getKey, DeviceType::getName));
+        //类映射
+        Map<String, String> kindMap = Arrays.stream(DeviceKind.values())
+                .collect(Collectors.toMap(DeviceKind::getKey, DeviceKind::getName));
         //字段映射
         Map<String, String> fieldMap = Arrays.stream(DeviceField.values())
                 .collect(Collectors.toMap(DeviceField::getKey, DeviceField::getName));
-        List<DeviceExcel> excels  = this.devices.stream().map(device -> {
+        List<DeviceExcel> excels = this.devices.stream().map(device -> {
+            //构建excel对象
             DeviceExcel deviceExcel = new DeviceExcel();
-
             deviceExcel.setDeviceId(device.getDeviceId());
             deviceExcel.setDeviceName(device.getDeviceName());
             deviceExcel.setDeviceSn(device.getDeviceSn());
-            deviceExcel.setDeviceType(typeMap.get(device.getDeviceType()));
-
-            deviceExcel.setField(device.getTags().stream().map(it -> {
-                return fieldMap.get(it.getField());
-            }).collect(Collectors.joining("/")));
-
+            deviceExcel.setDeviceKind(kindMap.get(device.getDeviceKind()));
+            deviceExcel.setMfrs(device.getMfrs());
+            //设置字段
+            deviceExcel.setField(
+                    device.getTags().stream()
+                            .map(it -> fieldMap.get(it.getField()))
+                            .collect(Collectors.joining("/"))
+            );
             return deviceExcel;
         }).collect(Collectors.toList());
 

+ 2 - 1
custom-gateway-core/src/main/java/com/shkpr/service/customgateway/core/constants/DeviceField.java

@@ -18,7 +18,8 @@ public enum DeviceField {
     FLOW_TOTAL_POS("flow_total_pos", "正向累计流量读数"),
     //反向累积流量读数
     FLOW_TOTAL_REV("flow_total_rev", "反向累计流量读数"),
-    ;
+    //瞬时压力
+    PRESS_CUR("press_cur", "瞬时压力");
     /**
      * 标识
      */

+ 2 - 2
custom-gateway-core/src/main/java/com/shkpr/service/customgateway/core/constants/DeviceType.java

@@ -9,14 +9,14 @@ import java.util.List;
 import static com.shkpr.service.customgateway.core.constants.DeviceField.*;
 
 /**
- * 设备类
+ * 设备
  *
  * @author 欧阳劲驰
  * @since 1.0.0
  */
 @Getter
 @AllArgsConstructor
-public enum DeviceType {
+public enum DeviceKind {
     //流量计
     FLOW("flow", "流量计", 2, "WaterMeter", Arrays.asList(FLOW_CUR, FLOW_TOTAL_POS, FLOW_TOTAL_REV)),
     ;

+ 24 - 2
custom-gateway-core/src/main/java/com/shkpr/service/customgateway/core/domain/Device.java

@@ -12,6 +12,8 @@ import org.springframework.format.annotation.DateTimeFormat;
 
 import java.time.LocalDateTime;
 import java.util.List;
+import java.util.Objects;
+import java.util.stream.Collectors;
 
 /**
  * 设备信息
@@ -36,9 +38,13 @@ public class Device {
      */
     private String deviceSn;
     /**
-     * 设备类
+     * 设备
      */
-    private String deviceType;
+    private String deviceKind;
+    /**
+     * 设备厂家
+     */
+    private String mfrs;
     /**
      * 创建时间
      */
@@ -51,4 +57,20 @@ public class Device {
      * 字段
      */
     private List<DeviceTag> tags;
+
+    @Override
+    public boolean equals(Object o) {
+        if (o == null || getClass() != o.getClass()) return false;
+        Device device = (Device) o;
+        return Objects.equals(deviceId, device.deviceId) && Objects.equals(deviceKind, device.deviceKind) &&
+                Objects.equals(
+                        tags.stream().map(DeviceTag::getField).collect(Collectors.joining()),
+                        device.tags.stream().map(DeviceTag::getField).collect(Collectors.joining())
+                );
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(deviceId, deviceKind, tags.stream().map(DeviceTag::getField).collect(Collectors.joining()));
+    }
 }

+ 7 - 2
custom-gateway-core/src/main/java/com/shkpr/service/customgateway/core/domain/DeviceExcel.java

@@ -29,8 +29,13 @@ public class DeviceExcel {
     /**
      * 设备类型
      */
-    @ExcelMapping("设备类型")
-    private String deviceType;
+    @ExcelMapping("设备种类")
+    private String deviceKind;
+    /**
+     * 设备厂家
+     */
+    @ExcelMapping("设备厂家")
+    private String mfrs;
     /**
      * 字段
      */

+ 1 - 1
custom-gateway-core/src/main/java/com/shkpr/service/customgateway/core/domain/DeviceTag.java

@@ -51,7 +51,7 @@ public class DeviceTag {
      */
     private String calcFormula;
 
-    public DeviceTag(String tag, String protocol,String measurement, String field, String valueType) {
+    public DeviceTag(String tag, String protocol, String measurement, String field, String valueType) {
         this.tag = tag;
         this.protocol = protocol;
         this.measurement = measurement;

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

@@ -27,10 +27,7 @@ import org.springframework.stereotype.Component;
 import java.io.IOException;
 import java.time.LocalDateTime;
 import java.time.temporal.ChronoUnit;
-import java.util.Arrays;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
+import java.util.*;
 import java.util.stream.Collectors;
 
 import static com.shkpr.service.customgateway.zydma.constants.IotPlatformMetadata.*;
@@ -109,9 +106,9 @@ public class IotCollector {
         LocalDateTime endTime = LocalDateTime.now().truncatedTo(ChronoUnit.HOURS);
 
         //===================按设备类型遍历===================
-        for (DeviceTypeMapping deviceTypeMapping : DeviceTypeMapping.values()) {
+        for (DeviceMapping deviceMapping : DeviceMapping.values()) {
             //参数
-            Map<String, Object> params = getHistoryDataParams(deviceTypeMapping, beginTime, endTime);
+            Map<String, Object> params = getHistoryDataParams(deviceMapping, beginTime, endTime);
             //请求结果项
             List<IotPlatformData> items = callingUtil.request(url, HttpMethod.POST, params, headers,
                     new TypeReference<IotPlatformResult<List<IotPlatformData>>>() {
@@ -123,7 +120,9 @@ public class IotCollector {
             Map<String, List<IotPlatformData>> snGroup = items.stream()
                     .collect(Collectors.groupingBy(IotPlatformData::getCode));
             //注册设备
-            registryDevices(deviceTypeMapping, snGroup);
+            registryDevices(deviceMapping, snGroup);
+            //更新设备
+            updateDevices(deviceMapping, snGroup);
 
             //设备列表
             List<Device> devices = deviceRegistry.findAll();
@@ -163,10 +162,10 @@ public class IotCollector {
     /**
      * 注册设备
      *
-     * @param deviceTypeMapping 设备类型映射
-     * @param snGroup           数据集合
+     * @param deviceMapping 设备类型映射
+     * @param snGroup       数据集合
      */
-    private void registryDevices(DeviceTypeMapping deviceTypeMapping, Map<String, List<IotPlatformData>> snGroup) {
+    private void registryDevices(DeviceMapping deviceMapping, Map<String, List<IotPlatformData>> snGroup) {
         //需要添加的设备
         Set<String> addedDevices = deviceRegistry.findAddedDevices(snGroup.keySet());
         //处理需要添加的设备:生成设备,并注册
@@ -178,11 +177,74 @@ public class IotCollector {
                 //生成设备
                 .map(sn -> deviceIdGenerator.generateDevice(
                         Devices.AREA_CODE,
-                        deviceTypeMapping.getType(),
-                        deviceTypeMapping.defaultTags(),
+                        deviceMapping.getKind(),
+                        buildTag(deviceMapping, snGroup.get(sn)),
                         sn, snGroup.get(sn).get(0).getName()
                 ))
                 //注册
                 .forEach(deviceRegistry::registerDevice);
     }
+
+    /**
+     * 注册设备
+     *
+     * @param deviceMapping 设备类型映射
+     * @param snGroup       数据集合
+     */
+    private void updateDevices(DeviceMapping deviceMapping, Map<String, List<IotPlatformData>> snGroup) {
+        //构建设备信息,k:远传id,v:标签集合
+        Map<String, Set<String>> deviceInfo = snGroup.entrySet().stream().collect(Collectors.toMap(
+                Map.Entry::getKey, entry ->
+                        entry.getValue().stream()
+                                //转表集合
+                                .map(item ->
+                                        //获取匹配的映射
+                                        deviceMapping.getFieldMappings().stream()
+                                                .filter(fieldMapping ->
+                                                        Objects.equals(item.getSensorName(), fieldMapping.getMapping()))
+                                                .findFirst()
+                                )
+                                .filter(Optional::isPresent)
+                                //获取字段key
+                                .map(optional -> optional.get().getField().getKey())
+                                .collect(Collectors.toSet())
+        ));
+        //需要添加的设备
+        Set<Device> updatedDevices = deviceRegistry.findUpdatedDevices(deviceInfo);
+        updatedDevices.forEach(device -> {
+            List<IotPlatformData> items = snGroup.get(device.getDeviceSn());
+
+            device.setTags(buildTag(deviceMapping, items));
+
+            deviceRegistry.updateDevice(device);
+        });
+    }
+
+    /**
+     * 构建采集标签
+     *
+     * @param deviceMapping 设备映射
+     * @param items         结果项
+     * @return 采集标签集合
+     */
+    private List<DeviceTag> buildTag(DeviceMapping deviceMapping, List<IotPlatformData> items) {
+        //传感器名称
+        Set<String> sensorNames = items.stream()
+                .map(IotPlatformData::getSensorName)
+                .collect(Collectors.toSet());
+
+        //构建标签
+        return deviceMapping.getFieldMappings().stream()
+                //过滤存在的标签
+                .filter(fieldMapping -> sensorNames.contains(fieldMapping.getMapping()))
+                //转换为采集标签
+                .map(fieldMapping -> new DeviceTag(
+                        fieldMapping.getMapping(),
+                        deviceMapping.getProtocol().getKey(),
+                        deviceMapping.getKind().getMeasurement(),
+                        fieldMapping.getField().getKey(),
+                        fieldMapping.getValueType().getKey()
+                ))
+                .collect(Collectors.toList());
+    }
 }

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

@@ -68,21 +68,21 @@ public abstract class IotPlatformMetadata {
     /**
      * 获取历史数据参数
      *
-     * @param deviceTypeMapping 设备类型映射
-     * @param beginTime         开始时间
-     * @param endTime           结束时间
+     * @param deviceMapping 设备类型映射
+     * @param beginTime     开始时间
+     * @param endTime       结束时间
      * @return 参数
      */
     public static Map<String, Object> getHistoryDataParams(
-            DeviceTypeMapping deviceTypeMapping, LocalDateTime beginTime, LocalDateTime endTime
+            DeviceMapping deviceMapping, LocalDateTime beginTime, LocalDateTime endTime
     ) {
         return new HashMap<String, Object>() {{
             put(Params.SITE_CODE, DefaultValues.SITE_CODE);
             put(Params.STREAM, Collections.singletonList(
                     new HashMap<String, String>() {{
-                        put(Params.DEVICE_TYPE, deviceTypeMapping.mapping);
-                        put(Params.SENSORS, deviceTypeMapping.fieldMappings.stream()
-                                .map(DeviceTypeMapping.FieldMapping::getMapping)
+                        put(Params.DEVICE_TYPE, deviceMapping.mapping);
+                        put(Params.SENSORS, deviceMapping.fieldMappings.stream()
+                                .map(DeviceMapping.FieldMapping::getMapping)
                                 .collect(Collectors.joining(DefaultValues.SENSORS_DELIMITER))
                         );
                         put(Params.DATE_FROM, beginTime.format(formatter));
@@ -122,43 +122,30 @@ public abstract class IotPlatformMetadata {
     }
 
     /**
-     * 设备类型映射
+     * 设备映射
      */
     @Getter
     @AllArgsConstructor
-    public enum DeviceTypeMapping {
-        //流量计
-        FLOW(DeviceType.FLOW, "水表", Arrays.asList(
-                new FieldMapping(DeviceField.FLOW_CUR, "瞬时流量",
-                        new DeviceTag("瞬时流量", ProtocolType.JSON.getKey(), DeviceType.FLOW.getMeasurement(),
-                                DeviceField.FLOW_CUR.getKey(), ValueType.DOUBLE_FLOAT.getKey()
-                        )
-                ),
-                new FieldMapping(DeviceField.FLOW_TOTAL_POS, "正向累计流量",
-                        new DeviceTag("正向累计流量", ProtocolType.JSON.getKey(), DeviceType.FLOW.getMeasurement(),
-                                DeviceField.FLOW_TOTAL_POS.getKey(), ValueType.DOUBLE_FLOAT.getKey()
-                        )
-                ),
-                new FieldMapping(DeviceField.FLOW_TOTAL_REV, "反向累计流量",
-                        new DeviceTag("反向累计流量", ProtocolType.JSON.getKey(), DeviceType.FLOW.getMeasurement(),
-                                DeviceField.FLOW_TOTAL_REV.getKey(), ValueType.DOUBLE_FLOAT.getKey()
-                        )
-                )
+    public enum DeviceMapping {
+        //上海锐莱
+        RUI_LAI(DeviceKind.FLOW, "水表", "上海锐莱", ProtocolType.JSON, Arrays.asList(
+                new FieldMapping(DeviceField.FLOW_CUR, "瞬时流量", ValueType.DOUBLE_FLOAT),
+                new FieldMapping(DeviceField.FLOW_TOTAL_POS, "正向累计流量", ValueType.DOUBLE_FLOAT),
+                new FieldMapping(DeviceField.FLOW_TOTAL_REV, "反向累计流量", ValueType.DOUBLE_FLOAT),
+                new FieldMapping(DeviceField.PRESS_CUR, "压力", ValueType.DOUBLE_FLOAT)
         ));
+
         //类型
-        private final DeviceType type;
+        private final DeviceKind kind;
         //映射
         private final String mapping;
+        //名称
+        private final String name;
+        //协议类型
+        private final ProtocolType protocol;
         //字段映射
         private final List<FieldMapping> fieldMappings;
 
-        //获取默认采集标签配置
-        public List<DeviceTag> defaultTags() {
-            return fieldMappings.stream()
-                    .map(IotPlatformMetadata.DeviceTypeMapping.FieldMapping::getDefaultTag)
-                    .collect(Collectors.toList());
-        }
-
         @Getter
         @Setter
         @AllArgsConstructor
@@ -167,8 +154,8 @@ public abstract class IotPlatformMetadata {
             private DeviceField field;
             //映射
             private String mapping;
-            //默认标签
-            private DeviceTag defaultTag;
+            //值类型
+            private ValueType valueType;
         }
 
     }

Rozdielové dáta súboru neboli zobrazené, pretože súbor je príliš veľký
+ 1305 - 125
dev_map.yml


+ 1 - 1
dev_seq.json

@@ -1,3 +1,3 @@
 {
-  "2025-11-06" : 122
+  "2025-11-06" : 118
 }