提交 | 用户 | 时间
|
a6de49
|
1 |
package com.iailab.module.data.point.collection.handler; |
H |
2 |
|
6a8e24
|
3 |
import com.alibaba.fastjson.JSON; |
140065
|
4 |
import com.iailab.framework.common.util.string.StrUtils; |
781e72
|
5 |
import com.iailab.module.data.common.enums.CommonConstant; |
a6de49
|
6 |
import com.iailab.module.data.common.enums.DataTypeEnum; |
H |
7 |
import com.iailab.module.data.common.enums.JsErrorCode; |
|
8 |
import com.iailab.module.data.common.utils.JavaScriptHandler; |
781e72
|
9 |
import com.iailab.module.data.point.collection.PointCollector; |
a6de49
|
10 |
import com.iailab.module.data.point.collection.utils.GenInfluxPointValueUtils; |
H |
11 |
import com.iailab.module.data.point.dto.DaPointDTO; |
|
12 |
import com.iailab.module.data.point.service.DaPointService; |
|
13 |
import com.iailab.module.data.influxdb.pojo.InfluxPointValuePOJO; |
|
14 |
import lombok.extern.slf4j.Slf4j; |
|
15 |
import org.apache.commons.lang3.StringUtils; |
|
16 |
import javax.annotation.Resource; |
781e72
|
17 |
|
潘 |
18 |
import org.springframework.beans.factory.annotation.Autowired; |
|
19 |
import org.springframework.data.redis.core.RedisTemplate; |
a6de49
|
20 |
import org.springframework.stereotype.Component; |
H |
21 |
import org.springframework.util.CollectionUtils; |
|
22 |
|
|
23 |
import java.math.BigDecimal; |
|
24 |
import java.util.*; |
3c511d
|
25 |
import java.util.stream.Collectors; |
D |
26 |
import java.util.stream.Stream; |
a6de49
|
27 |
|
H |
28 |
/** |
|
29 |
* 计算点处理 |
|
30 |
* |
|
31 |
* @author PanZhibao |
|
32 |
* @Description |
|
33 |
* @createTime 2023年05月03日 17:40:00 |
|
34 |
*/ |
|
35 |
@Slf4j |
|
36 |
@Component |
|
37 |
public class CalculateHandle { |
|
38 |
|
|
39 |
@Resource |
|
40 |
private DaPointService daPointService; |
|
41 |
|
|
42 |
@Resource |
|
43 |
private MeasureHandle measureHandle; |
|
44 |
|
|
45 |
@Resource |
|
46 |
private ConstantHandle constantHandle; |
|
47 |
|
|
48 |
@Resource |
|
49 |
private JavaScriptHandler javaScriptHandler; |
781e72
|
50 |
|
潘 |
51 |
@Autowired |
|
52 |
private RedisTemplate<String, Object> redisTemplate; |
a6de49
|
53 |
|
d41f14
|
54 |
public static final String regex = "[+\\-\\*/()\\&\\|\\>\\<]"; |
a6de49
|
55 |
|
2fcc1a
|
56 |
public List<InfluxPointValuePOJO> handle(Date collectTime, List<DaPointDTO> dtos, Map<String, Object> dataMap,List<String> listGood,List<String> listBad) { |
a6de49
|
57 |
List<InfluxPointValuePOJO> result = new ArrayList<>(); |
H |
58 |
try { |
|
59 |
log.info("计算点处理开始"); |
|
60 |
if (CollectionUtils.isEmpty(dtos)) { |
|
61 |
return result; |
|
62 |
} |
6a8e24
|
63 |
log.info(JSON.toJSONString(listBad)); |
a6de49
|
64 |
dtos.forEach(dto -> { |
H |
65 |
try { |
dbd8a0
|
66 |
Object rawValue = singleCompute(dto, dataMap, listGood, listBad); |
潘 |
67 |
BigDecimal coefficient = dto.getUnittransfactor() == null ? BigDecimal.ONE : dto.getUnittransfactor(); |
|
68 |
BigDecimal calValue = new BigDecimal(rawValue.toString()).multiply(coefficient); |
|
69 |
InfluxPointValuePOJO pojo = GenInfluxPointValueUtils.getByPoint(dto, calValue); |
a6de49
|
70 |
pojo.setTimestamp(collectTime.toInstant()); |
H |
71 |
result.add(pojo); |
|
72 |
} catch (Exception ex) { |
|
73 |
ex.printStackTrace(); |
|
74 |
log.info("计算点异常!PointNo=" + dto.getPointNo()); |
|
75 |
} |
|
76 |
}); |
|
77 |
log.info("计算点处理结束"); |
|
78 |
|
|
79 |
} catch (Exception ex) { |
|
80 |
ex.printStackTrace(); |
|
81 |
log.info("计算点处理异常!"); |
|
82 |
} |
|
83 |
return result; |
|
84 |
} |
|
85 |
|
2fcc1a
|
86 |
private Object singleCompute(DaPointDTO dto, Map<String, Object> dataMap,List<String> listGood,List<String> listBad) { |
a6de49
|
87 |
String expression = dto.getExpression(); |
6a8e24
|
88 |
log.info("PointNo=" + dto.getPointNo() + ";SourceExpression=" + expression); |
a6de49
|
89 |
String[] arr = expression.split(regex); |
3c511d
|
90 |
// 去掉arr中的空格 |
D |
91 |
arr = Stream.of(arr).filter(StringUtils::isNotBlank).toArray(String[]::new); |
2fcc1a
|
92 |
// 判断arr都在dataMap中包含 |
D |
93 |
if (!Arrays.stream(arr).allMatch(dataMap::containsKey)) { |
6a8e24
|
94 |
log.info("dataMap not contains key"); |
2fcc1a
|
95 |
listBad.add(dto.getPointNo()); |
D |
96 |
return CommonConstant.BAD_VALUE; |
|
97 |
} |
a6de49
|
98 |
|
H |
99 |
for (int i = 0; i < arr.length; i++) { |
|
100 |
String s = arr[i]; |
|
101 |
if (StringUtils.isNotBlank(s) && dataMap.containsKey(s)) { |
2fcc1a
|
102 |
// 对每个数加(),否则负值报错 |
D |
103 |
expression = expression.replace(s, "(" + dataMap.get(s).toString() + ")"); |
a6de49
|
104 |
} |
H |
105 |
} |
|
106 |
expression = expression.replace("&", "&&"); |
|
107 |
expression = expression.replace("|", "||"); |
|
108 |
expression = expression.replace("False", "false"); |
|
109 |
expression = expression.replace("True", "true"); |
|
110 |
log.info("PointNo=" + dto.getPointNo() + ";expression=" + expression); |
|
111 |
String result = javaScriptHandler.eval(expression); |
|
112 |
log.info("result=" + result); |
2fcc1a
|
113 |
if (result == null || result.contains(JsErrorCode.Infinity.name()) || result.contains(JsErrorCode.NaN.name())) { |
D |
114 |
listBad.add(dto.getPointNo()); |
781e72
|
115 |
return CommonConstant.BAD_VALUE; |
a6de49
|
116 |
} else { |
H |
117 |
if (DataTypeEnum.INT.getCode().equals(dto.getDataType())) { |
2fcc1a
|
118 |
listGood.add(dto.getPointNo()); |
a6de49
|
119 |
return new BigDecimal(result).intValue(); |
H |
120 |
} else if (DataTypeEnum.FLOAT.getCode().equals(dto.getDataType())) { |
2fcc1a
|
121 |
listGood.add(dto.getPointNo()); |
781e72
|
122 |
return new BigDecimal(result).setScale(4, BigDecimal.ROUND_UP).doubleValue(); |
a6de49
|
123 |
} else if (DataTypeEnum.BOOLEAN.getCode().equals(dto.getDataType())) { |
2fcc1a
|
124 |
listGood.add(dto.getPointNo()); |
a6de49
|
125 |
return Boolean.parseBoolean(result); |
2fcc1a
|
126 |
} else { |
D |
127 |
listBad.add(dto.getPointNo()); |
|
128 |
throw new RuntimeException("计算异常,未知数据类型"); |
a6de49
|
129 |
} |
H |
130 |
} |
|
131 |
} |
|
132 |
|
|
133 |
public Map<String, Object> getCurrent(List<String> pointNos) { |
|
134 |
Map<String, Object> data = new HashMap<>(); |
|
135 |
List<DaPointDTO> pointMathList = daPointService.getMathPoint(pointNos); |
|
136 |
if (CollectionUtils.isEmpty(pointMathList)) { |
|
137 |
return data; |
|
138 |
} |
|
139 |
pointMathList.forEach(item -> { |
781e72
|
140 |
Object value = CommonConstant.BAD_VALUE; |
潘 |
141 |
if (redisTemplate.hasKey(PointCollector.PV + item.getPointNo())) { |
|
142 |
value = redisTemplate.opsForValue().get(PointCollector.PV + item.getPointNo()); |
|
143 |
} else { |
dbd8a0
|
144 |
Object rawValue = singleCompute(item); |
潘 |
145 |
BigDecimal coefficient = item.getUnittransfactor() == null ? BigDecimal.ONE : item.getUnittransfactor(); |
|
146 |
value = new BigDecimal(rawValue.toString()).multiply(coefficient); |
781e72
|
147 |
} |
潘 |
148 |
data.put(item.getPointNo(), value); |
a6de49
|
149 |
}); |
H |
150 |
return data; |
|
151 |
} |
|
152 |
|
|
153 |
private Object singleCompute(DaPointDTO dto) { |
781e72
|
154 |
String result = CommonConstant.BAD_VALUE.toString(); |
a6de49
|
155 |
Map<String, Object> dataMap = new HashMap<>(); |
H |
156 |
String expression = dto.getExpression(); |
|
157 |
String[] arr = expression.split(regex); |
|
158 |
for (int i = 0; i < arr.length; i++) { |
|
159 |
String s = arr[i]; |
|
160 |
if (StringUtils.isBlank(s)) { |
|
161 |
continue; |
|
162 |
} |
|
163 |
List<String> pointNos = new ArrayList<>(); |
|
164 |
pointNos.add(s); |
|
165 |
dataMap.putAll(measureHandle.getCurrent(pointNos)); |
|
166 |
dataMap.putAll(constantHandle.getCurrent(pointNos)); |
48d2a2
|
167 |
if (dataMap.get(s) == null) { |
潘 |
168 |
log.info("计算点数据异常"); |
|
169 |
log.info("pointNo=" + dto.getPointNo() +";dataMap.key=" + s); |
|
170 |
return CommonConstant.BAD_VALUE; |
|
171 |
} |
140065
|
172 |
String valueStr = dataMap.get(s).toString(); |
潘 |
173 |
if (StrUtils.isNumeric(valueStr) && new BigDecimal(valueStr).compareTo(CommonConstant.BAD_VALUE) == 0) { |
|
174 |
log.info("BAD_VALUE:" + s); |
|
175 |
} |
|
176 |
if (StrUtils.isNumeric(valueStr) && new BigDecimal(valueStr).compareTo(BigDecimal.ZERO) < 0) { |
|
177 |
valueStr = "(" + valueStr + ")"; |
|
178 |
} |
|
179 |
expression = expression.replace(s, valueStr); |
a6de49
|
180 |
} |
H |
181 |
expression = expression.replace("&", "&&"); |
|
182 |
expression = expression.replace("|", "||"); |
|
183 |
expression = expression.replace("False", "false"); |
|
184 |
expression = expression.replace("True", "true"); |
|
185 |
log.info("PointNo=" + dto.getPointNo() + ";expression=" + expression); |
781e72
|
186 |
result = javaScriptHandler.eval(expression); |
a6de49
|
187 |
log.info("result=" + result); |
H |
188 |
if (result == null) { |
781e72
|
189 |
return CommonConstant.BAD_VALUE; |
潘 |
190 |
} else if (result.contains(JsErrorCode.Infinity.name()) || result.contains(JsErrorCode.NaN.name())) { |
a6de49
|
191 |
log.info("计算异常,使用默认值"); |
H |
192 |
return dto.getDefaultValue() == null ? BigDecimal.ZERO : dto.getDefaultValue(); |
|
193 |
} else { |
|
194 |
if (DataTypeEnum.INT.getCode().equals(dto.getDataType())) { |
|
195 |
return new BigDecimal(result).intValue(); |
|
196 |
} else if (DataTypeEnum.FLOAT.getCode().equals(dto.getDataType())) { |
781e72
|
197 |
return new BigDecimal(result).setScale(2, BigDecimal.ROUND_UP).doubleValue(); |
a6de49
|
198 |
} else if (DataTypeEnum.BOOLEAN.getCode().equals(dto.getDataType())) { |
H |
199 |
return Boolean.parseBoolean(result); |
|
200 |
} |
|
201 |
} |
|
202 |
return result; |
|
203 |
} |
|
204 |
} |