潘志宝
2024-09-19 bfcca60e2dbf84bce27def31f92e1c99f94a1d6e
提交 | 用户 | 时间
a6de49 1 package com.iailab.module.data.point.collection.handler;
H 2
9d7e02 3 import com.iailab.module.data.channel.opcda.collector.OpcDACollector;
a6de49 4 import com.iailab.module.data.common.enums.CommonConstant;
H 5 import com.iailab.module.data.common.enums.DataSourceType;
6 import com.iailab.module.data.common.enums.DataTypeEnum;
7 import com.iailab.module.data.common.utils.TagUtils;
8 import com.iailab.module.data.channel.kio.collector.KingIOCollector;
9 import com.iailab.module.data.channel.modbus.collector.ModBusCollector;
10 import com.iailab.module.data.channel.opcua.collector.OpcUaCollector;
11 import com.iailab.module.data.point.collection.utils.GenInfluxPointValueUtils;
12 import com.iailab.module.data.point.common.PointDataTypeEnum;
13 import com.iailab.module.data.point.dto.DaPointDTO;
14 import com.iailab.module.data.point.service.DaPointService;
15 import com.iailab.module.data.influxdb.pojo.InfluxPointValuePOJO;
16 import lombok.extern.slf4j.Slf4j;
17 import javax.annotation.Resource;
9d7e02 18
19 import org.springframework.beans.factory.annotation.Autowired;
a6de49 20 import org.springframework.stereotype.Component;
H 21 import org.springframework.util.CollectionUtils;
22
23 import java.math.BigDecimal;
24 import java.util.*;
25
26 /**
27  * 测量点处理
28  *
29  * @author PanZhibao
30  * @Description
31  * @createTime 2023年05月03日 22:36:00
32  */
33 @Slf4j
34 @Component
35 public class MeasureHandle {
36
37     private BigDecimal maxValue = new BigDecimal("1000000000");
38
39     private BigDecimal minValue = new BigDecimal("0");
40
41     @Resource
42     private ModBusCollector modBusCollector;
43
44     @Resource
45     private KingIOCollector kingIOCollector;
46
47     @Resource
48     private OpcUaCollector opcUaCollector;
49
9d7e02 50     @Autowired
51     private OpcDACollector opcDACollector;
a6de49 52
H 53     @Resource
54     private DaPointService daPointService;
55
56     public List<InfluxPointValuePOJO> handle(Date collectTime, List<DaPointDTO> dtos, Map<String, Object> dataMap) {
57         log.info("测量点处理开始");
58         List<InfluxPointValuePOJO> result = new ArrayList<>();
59         if (CollectionUtils.isEmpty(dtos)) {
60             return result;
61         }
62
9d7e02 63         List<String[]> opcUaTagIds = new ArrayList<>();
a6de49 64         List<String[]> opcDaTagIds = new ArrayList<>();
H 65         List<String[]> modbusTagIds = new ArrayList<>();
66         List<String[]> kioTagIds = new ArrayList<>();
67         List<String[]> httpTagZxzk = new ArrayList<>();
68
69
70         dtos.stream().forEach(item -> {
71             if (DataSourceType.OPCUA.getCode().equals(item.getSourceType())) {
9d7e02 72                 opcUaTagIds.add(new String[]{item.getSourceId(), item.getTagNo()});
a6de49 73             } else if (DataSourceType.OPCDA.getCode().equals(item.getSourceType())) {
H 74                 opcDaTagIds.add(new String[]{item.getSourceId(), item.getTagNo()});
75             } else if (DataSourceType.ModBus.getCode().equals(item.getSourceType())) {
76                 modbusTagIds.add(new String[]{item.getSourceId(), item.getTagNo()});
77             } else if (DataSourceType.KIO.getCode().equals(item.getSourceType())) {
78                 kioTagIds.add(new String[]{item.getSourceId(), item.getTagNo()});
79             }
80         });
81
9d7e02 82         Map<String, Object> tagValues = new HashMap<>();
83         if (!CollectionUtils.isEmpty(opcUaTagIds)) {
84             tagValues.putAll(opcUaCollector.getTagValues(opcUaTagIds));
a6de49 85         }
H 86         if (!CollectionUtils.isEmpty(opcDaTagIds)) {
9d7e02 87             tagValues.putAll(opcDACollector.getTagValues(modbusTagIds));
a6de49 88         }
H 89         if (!CollectionUtils.isEmpty(modbusTagIds)) {
9d7e02 90             tagValues.putAll(modBusCollector.getTagValues(modbusTagIds));
a6de49 91         }
H 92         if (!CollectionUtils.isEmpty(kioTagIds)) {
9d7e02 93             tagValues.putAll(kingIOCollector.getTagValues(kioTagIds));
94         }
95         this.toCommonResult(collectTime, dtos, tagValues, dataMap, result);
a6de49 96         log.info("测量点处理结束");
H 97         return result;
98     }
99
100     private void toCommonResult(Date collectTime, List<DaPointDTO> dtos, Map<String, Object> tagValues,
101                           Map<String, Object> dataMap, List<InfluxPointValuePOJO> result) {
102         if (!CollectionUtils.isEmpty(tagValues)) {
103             tagValues.forEach((k, v) -> {
104                 dataMap.put(k, v);
105             });
106             dtos.forEach(dto -> {
107                 String tagId = TagUtils.genTagId(dto.getSourceType(), dto.getSourceName(), dto.getTagNo());
108                 if (tagValues.get(tagId) != null) {
109                     Object value = handleData(dto, tagValues.get(tagId));
110                     InfluxPointValuePOJO pojo = GenInfluxPointValueUtils.getByPoint(dto, value);
111                     pojo.setTimestamp(collectTime.toInstant());
112                     dataMap.put(dto.getPointNo(), value);
113                     result.add(pojo);
114                 } else {
115                     System.out.println("值异常!TagId=" + tagId);
116                 }
117             });
118         }
119     }
120
121     private Object handleData(DaPointDTO dto, Object value) {
122         Object result = value;
123         try {
124             if (DataTypeEnum.FLOAT.getCode().equals(dto.getDataType()) || DataTypeEnum.INT.getCode().equals(dto.getDataType())) {
125                 BigDecimal rawValue = new BigDecimal(value.toString());
126
127                 // 异常值处理
128                 if (rawValue.compareTo(maxValue) > 0 || rawValue.compareTo(minValue) < 0) {
129                     rawValue = CommonConstant.BAD_VALUE;
130                 }
131                 BigDecimal coefficient = dto.getUnittransfactor() == null ? BigDecimal.ONE : dto.getUnittransfactor();
132                 BigDecimal calValue = rawValue.multiply(coefficient);
133                 if (dto.getMaxValue() != null && calValue.compareTo(dto.getMaxValue()) > 0) {
134                     result = dto.getMaxValue();
135                 } else if (dto.getMinValue() != null && calValue.compareTo(dto.getMinValue()) < 0) {
136                     result = dto.getMinValue();
137                 } else {
138                     result = calValue;
139                 }
140                 if (DataTypeEnum.FLOAT.getCode().equals(dto.getDataType())) {
141                     result = ((BigDecimal) result).doubleValue();
142                 } else {
143                     result = ((BigDecimal) result).intValue();
144                 }
145             } else if (DataTypeEnum.BOOLEAN.getCode().equals(dto.getDataType())) {
146                 result = Boolean.parseBoolean(value.toString());
147             }
148
149         } catch (Exception ex) {
150             log.warn("handleData异常,PointNo=" + dto.getPointNo());
151             ex.printStackTrace();
152         }
153         return result;
154     }
155
156     public Map<String, Object> getCurrent(List<String> pointNos) {
157         Map<String, Object> data = new HashMap<>();
158         List<DaPointDTO> pointMeasureList = daPointService.getMeasurePoint(pointNos);
159         pointMeasureList.forEach(
160                 item -> {
161                     try {
162                         Object value = CommonConstant.BAD_VALUE;
163                         if (DataSourceType.OPCUA.getCode().equals(item.getSourceType())) {
164                             value = opcUaCollector.getTagValue(item.getSourceId(), item.getTagNo());
165                         } else if (DataSourceType.ModBus.getCode().equals(item.getSourceType())) {
166                             value = modBusCollector.getTagValue(item.getSourceId(), item.getTagNo());
167                         } else if (DataSourceType.KIO.getCode().equals(item.getSourceType())) {
168                             value = kingIOCollector.getTagValue(item.getSourceId(), item.getTagNo());
169                         } else if (DataSourceType.HTTP.getCode().equals(item.getSourceType())) {
bfcca6 170
a6de49 171                         } else {
H 172                             log.info("没有匹配的TagNo=" + item.getTagNo());
173                         }
174                         log.info("没有匹配的TagNo=" + item.getTagNo());
175                         log.info("valueStr=" + value.toString());
176                         log.info("DataType=" + item.getDataType());
177                         if (!PointDataTypeEnum.BOOLEAN.getCode().equals(item.getDataType())) {
178                             BigDecimal decValue =  new BigDecimal(value.toString());
179                             if (PointDataTypeEnum.FLOAT.getCode().equals(item.getDataType())) {
180                                 decValue = decValue.setScale(2, BigDecimal.ROUND_HALF_UP);
181                             } else if (PointDataTypeEnum.INT.getCode().equals(item.getDataType())) {
182                                 decValue = decValue.setScale(0, BigDecimal.ROUND_HALF_UP);
183                             }
184                             data.put(item.getPointNo(), decValue);
185                         } else {
186                             data.put(item.getPointNo(), value);
187                         }
188                     } catch (Exception ex) {
189                         ex.printStackTrace();
190                     }
191
192                 }
193         );
194         return data;
195     }
196 }