dengzedong
2025-01-18 3c511d4ef08ffece38891435a9d16ee38319345b
提交 | 用户 | 时间
a6de49 1 package com.iailab.module.data.point.collection.handler;
H 2
140065 3 import com.iailab.framework.common.util.string.StrUtils;
781e72 4 import com.iailab.module.data.common.enums.CommonConstant;
a6de49 5 import com.iailab.module.data.common.enums.DataTypeEnum;
H 6 import com.iailab.module.data.common.enums.JsErrorCode;
7 import com.iailab.module.data.common.utils.JavaScriptHandler;
781e72 8 import com.iailab.module.data.point.collection.PointCollector;
a6de49 9 import com.iailab.module.data.point.collection.utils.GenInfluxPointValueUtils;
H 10 import com.iailab.module.data.point.dto.DaPointDTO;
11 import com.iailab.module.data.point.service.DaPointService;
12 import com.iailab.module.data.influxdb.pojo.InfluxPointValuePOJO;
13 import lombok.extern.slf4j.Slf4j;
14 import org.apache.commons.lang3.StringUtils;
15 import javax.annotation.Resource;
781e72 16
17 import org.springframework.beans.factory.annotation.Autowired;
18 import org.springframework.data.redis.core.RedisTemplate;
a6de49 19 import org.springframework.stereotype.Component;
H 20 import org.springframework.util.CollectionUtils;
21
22 import java.math.BigDecimal;
23 import java.util.*;
3c511d 24 import java.util.stream.Collectors;
D 25 import java.util.stream.Stream;
a6de49 26
H 27 /**
28  * 计算点处理
29  *
30  * @author PanZhibao
31  * @Description
32  * @createTime 2023年05月03日 17:40:00
33  */
34 @Slf4j
35 @Component
36 public class CalculateHandle {
37
38     @Resource
39     private DaPointService daPointService;
40
41     @Resource
42     private MeasureHandle measureHandle;
43
44     @Resource
45     private ConstantHandle constantHandle;
46
47     @Resource
48     private JavaScriptHandler javaScriptHandler;
781e72 49
50     @Autowired
51     private RedisTemplate<String, Object> redisTemplate;
a6de49 52
d41f14 53     public static final String regex = "[+\\-\\*/()\\&\\|\\>\\<]";
a6de49 54
2fcc1a 55     public List<InfluxPointValuePOJO> handle(Date collectTime, List<DaPointDTO> dtos, Map<String, Object> dataMap,List<String> listGood,List<String> listBad) {
a6de49 56         List<InfluxPointValuePOJO> result = new ArrayList<>();
H 57         try {
58             log.info("计算点处理开始");
59             if (CollectionUtils.isEmpty(dtos)) {
60                 return result;
61             }
62             dtos.forEach(dto -> {
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();
H 84         String[] arr = expression.split(regex);
3c511d 85         // 去掉arr中的空格
D 86         arr = Stream.of(arr).filter(StringUtils::isNotBlank).toArray(String[]::new);
2fcc1a 87         // 判断arr都在dataMap中包含
D 88         if (!Arrays.stream(arr).allMatch(dataMap::containsKey)) {
89             listBad.add(dto.getPointNo());
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 }