提交 | 用户 | 时间
|
a6de49
|
1 |
package com.iailab.module.data.point.collection.handler; |
H |
2 |
|
|
3 |
import com.iailab.module.data.common.enums.DataTypeEnum; |
|
4 |
import com.iailab.module.data.common.enums.JsErrorCode; |
|
5 |
import com.iailab.module.data.common.utils.JavaScriptHandler; |
|
6 |
import com.iailab.module.data.point.collection.utils.GenInfluxPointValueUtils; |
|
7 |
import com.iailab.module.data.point.dto.DaPointDTO; |
|
8 |
import com.iailab.module.data.point.service.DaPointService; |
|
9 |
import com.iailab.module.data.influxdb.pojo.InfluxPointValuePOJO; |
|
10 |
import lombok.extern.slf4j.Slf4j; |
|
11 |
import org.apache.commons.lang3.StringUtils; |
|
12 |
import javax.annotation.Resource; |
|
13 |
import org.springframework.stereotype.Component; |
|
14 |
import org.springframework.util.CollectionUtils; |
|
15 |
|
|
16 |
import java.math.BigDecimal; |
|
17 |
import java.util.*; |
|
18 |
|
|
19 |
/** |
|
20 |
* 计算点处理 |
|
21 |
* |
|
22 |
* @author PanZhibao |
|
23 |
* @Description |
|
24 |
* @createTime 2023年05月03日 17:40:00 |
|
25 |
*/ |
|
26 |
@Slf4j |
|
27 |
@Component |
|
28 |
public class CalculateHandle { |
|
29 |
|
|
30 |
@Resource |
|
31 |
private DaPointService daPointService; |
|
32 |
|
|
33 |
@Resource |
|
34 |
private MeasureHandle measureHandle; |
|
35 |
|
|
36 |
@Resource |
|
37 |
private ConstantHandle constantHandle; |
|
38 |
|
|
39 |
@Resource |
|
40 |
private JavaScriptHandler javaScriptHandler; |
|
41 |
|
d41f14
|
42 |
public static final String regex = "[+\\-\\*/()\\&\\|\\>\\<]"; |
a6de49
|
43 |
|
H |
44 |
public List<InfluxPointValuePOJO> handle(Date collectTime, List<DaPointDTO> dtos, Map<String, Object> dataMap) { |
|
45 |
List<InfluxPointValuePOJO> result = new ArrayList<>(); |
|
46 |
try { |
|
47 |
log.info("计算点处理开始"); |
|
48 |
if (CollectionUtils.isEmpty(dtos)) { |
|
49 |
return result; |
|
50 |
} |
|
51 |
dtos.forEach(dto -> { |
|
52 |
try { |
|
53 |
Object value = singleCompute(dto, dataMap); |
|
54 |
InfluxPointValuePOJO pojo = GenInfluxPointValueUtils.getByPoint(dto, value); |
|
55 |
pojo.setTimestamp(collectTime.toInstant()); |
|
56 |
result.add(pojo); |
|
57 |
} catch (Exception ex) { |
|
58 |
ex.printStackTrace(); |
|
59 |
log.info("计算点异常!PointNo=" + dto.getPointNo()); |
|
60 |
} |
|
61 |
}); |
|
62 |
log.info("计算点处理结束"); |
|
63 |
|
|
64 |
} catch (Exception ex) { |
|
65 |
ex.printStackTrace(); |
|
66 |
log.info("计算点处理异常!"); |
|
67 |
} |
|
68 |
return result; |
|
69 |
} |
|
70 |
|
|
71 |
private Object singleCompute(DaPointDTO dto, Map<String, Object> dataMap) { |
|
72 |
String expression = dto.getExpression(); |
|
73 |
String[] arr = expression.split(regex); |
|
74 |
|
|
75 |
for (int i = 0; i < arr.length; i++) { |
|
76 |
String s = arr[i]; |
|
77 |
if (StringUtils.isNotBlank(s) && dataMap.containsKey(s)) { |
|
78 |
expression = expression.replace(s, dataMap.get(s).toString()); |
|
79 |
} |
|
80 |
} |
|
81 |
expression = expression.replace("&", "&&"); |
|
82 |
expression = expression.replace("|", "||"); |
|
83 |
expression = expression.replace("False", "false"); |
|
84 |
expression = expression.replace("True", "true"); |
|
85 |
log.info("PointNo=" + dto.getPointNo() + ";expression=" + expression); |
|
86 |
String result = javaScriptHandler.eval(expression); |
|
87 |
log.info("result=" + result); |
|
88 |
if (result == null) { |
|
89 |
return null; |
|
90 |
} else if (result.contains(JsErrorCode.Infinity.name()) || |
|
91 |
result.contains(JsErrorCode.NaN.name())) { |
|
92 |
log.info("计算异常,使用默认值"); |
|
93 |
return dto.getDefaultValue() == null ? BigDecimal.ZERO : dto.getDefaultValue(); |
|
94 |
} else { |
|
95 |
if (DataTypeEnum.INT.getCode().equals(dto.getDataType())) { |
|
96 |
return new BigDecimal(result).intValue(); |
|
97 |
} else if (DataTypeEnum.FLOAT.getCode().equals(dto.getDataType())) { |
|
98 |
return new BigDecimal(result).setScale(10, BigDecimal.ROUND_UP).doubleValue(); |
|
99 |
} else if (DataTypeEnum.BOOLEAN.getCode().equals(dto.getDataType())) { |
|
100 |
return Boolean.parseBoolean(result); |
|
101 |
} |
|
102 |
} |
|
103 |
return result; |
|
104 |
} |
|
105 |
|
|
106 |
public Map<String, Object> getCurrent(List<String> pointNos) { |
|
107 |
Map<String, Object> data = new HashMap<>(); |
|
108 |
List<DaPointDTO> pointMathList = daPointService.getMathPoint(pointNos); |
|
109 |
if (CollectionUtils.isEmpty(pointMathList)) { |
|
110 |
return data; |
|
111 |
} |
|
112 |
pointMathList.forEach(item -> { |
|
113 |
data.put(item.getPointNo(), singleCompute(item)); |
|
114 |
}); |
|
115 |
return data; |
|
116 |
} |
|
117 |
|
|
118 |
private Object singleCompute(DaPointDTO dto) { |
|
119 |
Map<String, Object> dataMap = new HashMap<>(); |
|
120 |
String expression = dto.getExpression(); |
|
121 |
String[] arr = expression.split(regex); |
|
122 |
for (int i = 0; i < arr.length; i++) { |
|
123 |
String s = arr[i]; |
|
124 |
if (StringUtils.isBlank(s)) { |
|
125 |
continue; |
|
126 |
} |
|
127 |
List<String> pointNos = new ArrayList<>(); |
|
128 |
pointNos.add(s); |
|
129 |
dataMap.putAll(measureHandle.getCurrent(pointNos)); |
|
130 |
dataMap.putAll(constantHandle.getCurrent(pointNos)); |
|
131 |
expression = expression.replace(s, dataMap.get(s).toString()); |
|
132 |
} |
|
133 |
expression = expression.replace("&", "&&"); |
|
134 |
expression = expression.replace("|", "||"); |
|
135 |
expression = expression.replace("False", "false"); |
|
136 |
expression = expression.replace("True", "true"); |
|
137 |
log.info("PointNo=" + dto.getPointNo() + ";expression=" + expression); |
|
138 |
String result = javaScriptHandler.eval(expression); |
|
139 |
log.info("result=" + result); |
|
140 |
if (result == null) { |
|
141 |
return null; |
|
142 |
} else if (result.contains(JsErrorCode.Infinity.name()) || |
|
143 |
result.contains(JsErrorCode.NaN.name())) { |
|
144 |
log.info("计算异常,使用默认值"); |
|
145 |
return dto.getDefaultValue() == null ? BigDecimal.ZERO : dto.getDefaultValue(); |
|
146 |
} else { |
|
147 |
if (DataTypeEnum.INT.getCode().equals(dto.getDataType())) { |
|
148 |
return new BigDecimal(result).intValue(); |
|
149 |
} else if (DataTypeEnum.FLOAT.getCode().equals(dto.getDataType())) { |
|
150 |
return new BigDecimal(result).setScale(10, BigDecimal.ROUND_UP).doubleValue(); |
|
151 |
} else if (DataTypeEnum.BOOLEAN.getCode().equals(dto.getDataType())) { |
|
152 |
return Boolean.parseBoolean(result); |
|
153 |
} |
|
154 |
} |
|
155 |
return result; |
|
156 |
} |
|
157 |
} |