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