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