dongyukun
2025-06-04 f5ec1f3326f75e18222859a534ed5a249f5e2cc3
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package com.iailab.module.data.point.collection.handler;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.iailab.framework.common.util.string.StrUtils;
import com.iailab.module.data.common.enums.CommonConstant;
import com.iailab.module.data.common.enums.DataTypeEnum;
import com.iailab.module.data.common.enums.JsErrorCode;
import com.iailab.module.data.common.utils.JavaScriptHandler;
import com.iailab.module.data.enums.DataPointFreqEnum;
import com.iailab.module.data.point.collection.PointCollector;
import com.iailab.module.data.point.collection.utils.GenInfluxPointValueUtils;
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 org.apache.commons.lang3.StringUtils;
 
import javax.annotation.Resource;
import javax.validation.constraints.Max;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
 
import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Stream;
 
/**
 * 计算点处理
 *
 * @author PanZhibao
 * @Description
 * @createTime 2023年05月03日 17:40:00
 */
@Slf4j
@Component
public class CalculateHandle {
 
    @Resource
    private DaPointService daPointService;
 
    @Resource
    private MeasureHandle measureHandle;
 
    @Resource
    private ConstantHandle constantHandle;
 
    @Resource
    private CumulateHandle cumulateHandle;
 
    @Resource
    private ExtremalHandle extremalHandle;
 
    @Resource
    private JavaScriptHandler javaScriptHandler;
 
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
 
    public final static String regex = "[+\\-\\*/()\\&\\|\\>\\<]";
 
    private final static String POINT_PREFIX = "C";
 
    private final static String PENDING_FLAG = "pending";
 
    private final static int MAX_RECURSION = 10;
 
    public List<InfluxPointValuePOJO> handle(Date collectTime, List<DaPointDTO> dtos, Map<String, Object> dataMap, List<String> listGood, List<String> listBad) {
        List<InfluxPointValuePOJO> result = new ArrayList<>();
        try {
            log.info("计算点处理开始");
            if (CollectionUtils.isEmpty(dtos)) {
                return result;
            }
            Map<String, DaPointDTO> pendingMap = new HashMap<>();
            log.info(JSON.toJSONString(listBad));
            dtos.forEach(dto -> {
                try {
                    Object rawValue = singleCompute(dto, dataMap, listGood, listBad);
                    if (PENDING_FLAG.equals(rawValue.toString())) {
                        pendingMap.put(dto.getPointNo(), dto);
                    } else {
                        BigDecimal coefficient = dto.getUnittransfactor() == null ? BigDecimal.ONE : dto.getUnittransfactor();
                        BigDecimal calValue = new BigDecimal(rawValue.toString()).multiply(coefficient);
                        if (dto.getMaxValue() != null && calValue.compareTo(dto.getMaxValue()) > 0) {
                            calValue = dto.getMaxValue();
                        } else if (dto.getMinValue() != null && calValue.compareTo(dto.getMinValue()) < 0) {
                            calValue = dto.getMinValue();
                        }
                        dataMap.put(dto.getPointNo(), calValue);
                        InfluxPointValuePOJO pojo = GenInfluxPointValueUtils.getByPoint(dto, calValue);
                        pojo.setTimestamp(GenInfluxPointValueUtils.getByMin(collectTime, DataPointFreqEnum.getEumByCode(dto.getMinfreqid())));
                        result.add(pojo);
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                    log.info("计算点异常!PointNo=" + dto.getPointNo());
                }
            });
 
 
            Map<DaPointDTO, Object> valueResult = new HashMap<>();
            handPending(pendingMap, dataMap, listGood, listBad, valueResult, 1);
            log.info("valueResult size=" + valueResult.size());
            valueResult.forEach((key, value) -> {
                InfluxPointValuePOJO pojo = GenInfluxPointValueUtils.getByPoint(key, value);
                pojo.setTimestamp(GenInfluxPointValueUtils.getByMin(collectTime, DataPointFreqEnum.getEumByCode(key.getMinfreqid())));
                result.add(pojo);
            });
            log.info("计算点处理结束");
        } catch (Exception ex) {
            ex.printStackTrace();
            log.info("计算点处理异常!");
        }
        return result;
    }
    private void handPending(Map<String, DaPointDTO> pendingMap, Map<String, Object> dataMap,
                                            List<String> listGood, List<String> listBad, Map<DaPointDTO, Object> valueResult, int count) {
 
        Map<String, DaPointDTO> tempMap = new HashMap<>();
        if (CollectionUtils.isEmpty(pendingMap)) {
            log.info("pendingMap is empty");
            return;
        }
        log.info("处理包含计算点的");
        log.info("handPending count=" + count);
        if (count > MAX_RECURSION) {
            log.info("最多递归10次");
            return;
        }
        count = count + 1;
        for(String key : pendingMap.keySet()) {
            DaPointDTO dto = pendingMap.get(key);
            Object rawValue = singleCompute(dto, dataMap, listGood, listBad);
            if (PENDING_FLAG.equals(rawValue.toString())) {
                tempMap.put(key, dto);
            } else {
                BigDecimal coefficient = dto.getUnittransfactor() == null ? BigDecimal.ONE : dto.getUnittransfactor();
                BigDecimal calValue = new BigDecimal(rawValue.toString()).multiply(coefficient);
                if (dto.getMaxValue() != null && calValue.compareTo(dto.getMaxValue()) > 0) {
                    calValue = dto.getMaxValue();
                } else if (dto.getMinValue() != null && calValue.compareTo(dto.getMinValue()) < 0) {
                    calValue = dto.getMinValue();
                }
                dataMap.put(dto.getPointNo(), calValue);
                valueResult.put(dto, calValue);
            }
        }
        if (!CollectionUtils.isEmpty(tempMap)) {
            this.handPending(tempMap, dataMap, listGood, listBad, valueResult, count);
        }
    }
 
    private Object singleCompute(DaPointDTO dto, Map<String, Object> dataMap, List<String> listGood, List<String> listBad) {
        String expression = dto.getExpression();
        log.info("PointNo=" + dto.getPointNo() + ";SourceExpression=" + expression);
        String[] arr = expression.split(regex);
        // 去掉arr中的空格
        arr = Stream.of(arr).filter(StringUtils::isNotBlank).toArray(String[]::new);
        // 判断arr都在dataMap中包含
        /*if (!Arrays.stream(arr).allMatch(dataMap::containsKey)) {
            log.info("dataMap not contains key");
            listBad.add(dto.getPointNo());
            return CommonConstant.BAD_VALUE;
        }*/
 
        for (int i = 0; i < arr.length; i++) {
            String s = arr[i];
            if (StringUtils.isNotBlank(s) && dataMap.containsKey(s)) {
                // 对每个数加(),否则负值报错
                expression = expression.replace(s, "(" + dataMap.get(s).toString() + ")");
            } else if(StringUtils.isNotBlank(s) && s.trim().startsWith(POINT_PREFIX)) {
                log.info("包含计算点,先挂起");
                log.info("dataMap=" + JSONObject.toJSONString(dataMap));
                return PENDING_FLAG;
            }  else if(StringUtils.isNotBlank(s) && !dataMap.containsKey(s)) {
                log.info("dataMap not contains key " + s);
                listBad.add(dto.getPointNo());
                return CommonConstant.BAD_VALUE;
            }
        }
        expression = expression.replace("&", "&&");
        expression = expression.replace("|", "||");
        expression = expression.replace("False", "false");
        expression = expression.replace("True", "true");
        log.info("PointNo=" + dto.getPointNo() + ";expression=" + expression);
 
        String result = javaScriptHandler.eval(expression);
        log.info("result=" + result);
        if (result == null || result.contains(JsErrorCode.Infinity.name()) || result.contains(JsErrorCode.NaN.name())) {
            listBad.add(dto.getPointNo());
            return CommonConstant.BAD_VALUE;
        } else {
            if (DataTypeEnum.INT.getCode().equals(dto.getDataType())) {
                listGood.add(dto.getPointNo());
                return new BigDecimal(result).intValue();
            } else if (DataTypeEnum.FLOAT.getCode().equals(dto.getDataType())) {
                listGood.add(dto.getPointNo());
                return new BigDecimal(result).setScale(4, BigDecimal.ROUND_UP).doubleValue();
            } else if (DataTypeEnum.BOOLEAN.getCode().equals(dto.getDataType())) {
                listGood.add(dto.getPointNo());
                return Boolean.parseBoolean(result);
            } else {
                listBad.add(dto.getPointNo());
                throw new RuntimeException("计算异常,未知数据类型");
            }
        }
    }
 
    public Map<String, Object> getCurrent(List<String> pointNos) {
        Map<String, Object> data = new HashMap<>();
        List<DaPointDTO> pointMathList = daPointService.getMathPoint(pointNos);
        if (CollectionUtils.isEmpty(pointMathList)) {
            return data;
        }
        pointMathList.forEach(item -> {
            Object value = CommonConstant.BAD_VALUE;
            if (redisTemplate.hasKey(PointCollector.PV + item.getPointNo())) {
                value = redisTemplate.opsForValue().get(PointCollector.PV + item.getPointNo());
            } else {
                Object rawValue = singleCompute(item, 1);
                BigDecimal coefficient = item.getUnittransfactor() == null ? BigDecimal.ONE : item.getUnittransfactor();
                value = new BigDecimal(rawValue.toString()).multiply(coefficient);
            }
            data.put(item.getPointNo(), value);
        });
        return data;
    }
 
    private Object singleCompute(DaPointDTO dto, int count) {
        String result = CommonConstant.BAD_VALUE.toString();
        Map<String, Object> dataMap = new HashMap<>();
        String expression = dto.getExpression();
        String[] arr = expression.split(regex);
        for (int i = 0; i < arr.length; i++) {
            String s = arr[i];
            if (StringUtils.isBlank(s)) {
                continue;
            }
            List<String> pointNos = new ArrayList<>();
            pointNos.add(s);
            dataMap.putAll(measureHandle.getCurrent(pointNos));
            dataMap.putAll(constantHandle.getCurrent(pointNos));
            dataMap.putAll(cumulateHandle.getCurrent(pointNos));
            dataMap.putAll(extremalHandle.getCurrent(pointNos));
            if (s.trim().startsWith(POINT_PREFIX)) {
                log.info("计算点递归查询");
                List<DaPointDTO> pointMathList = daPointService.getMathPoint(pointNos);
                if (CollectionUtils.isEmpty(pointMathList)) {
                    return result;
                }
                log.info("count = " + count);
                if (count > MAX_RECURSION) {
                    return result;
                }
                Object v = this.singleCompute(pointMathList.get(0), count);
                dataMap.put(s, v);
                count = count + 1;
            }
            if (dataMap.get(s) == null) {
                log.info("计算点数据异常");
                log.info("pointNo=" + dto.getPointNo() + ";dataMap.key=" + s);
                return CommonConstant.BAD_VALUE;
            }
            String valueStr = dataMap.get(s).toString();
            if (StrUtils.isNumeric(valueStr) && new BigDecimal(valueStr).compareTo(CommonConstant.BAD_VALUE) == 0) {
                log.info("BAD_VALUE:" + s);
            }
            if (StrUtils.isNumeric(valueStr) && new BigDecimal(valueStr).compareTo(BigDecimal.ZERO) < 0) {
                valueStr = "(" + valueStr + ")";
            }
            expression = expression.replace(s, valueStr);
        }
        expression = expression.replace("&", "&&");
        expression = expression.replace("|", "||");
        expression = expression.replace("False", "false");
        expression = expression.replace("True", "true");
        log.info("PointNo=" + dto.getPointNo() + ";expression=" + expression);
        result = javaScriptHandler.eval(expression);
        log.info("result=" + result);
        if (result == null) {
            return CommonConstant.BAD_VALUE;
        } else if (result.contains(JsErrorCode.Infinity.name()) || result.contains(JsErrorCode.NaN.name())) {
            log.info("计算异常,使用默认值");
            return dto.getDefaultValue() == null ? BigDecimal.ZERO : dto.getDefaultValue();
        } else {
            if (DataTypeEnum.INT.getCode().equals(dto.getDataType())) {
                return new BigDecimal(result).intValue();
            } else if (DataTypeEnum.FLOAT.getCode().equals(dto.getDataType())) {
                return new BigDecimal(result).setScale(2, BigDecimal.ROUND_UP).doubleValue();
            } else if (DataTypeEnum.BOOLEAN.getCode().equals(dto.getDataType())) {
                return Boolean.parseBoolean(result);
            }
        }
        return result;
    }
}