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