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