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