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