Jay
2025-02-24 023d6b104c5056ced2fb0b834ae28363078203ba
提交 | 用户 | 时间
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 {
2fcc1a 66                     Object value = singleCompute(dto, dataMap, listGood, listBad);
a6de49 67                     InfluxPointValuePOJO pojo = GenInfluxPointValueUtils.getByPoint(dto, value);
H 68                     pojo.setTimestamp(collectTime.toInstant());
69                     result.add(pojo);
70                 } catch (Exception ex) {
71                     ex.printStackTrace();
72                     log.info("计算点异常!PointNo=" + dto.getPointNo());
73                 }
74             });
75             log.info("计算点处理结束");
76
77         } catch (Exception ex) {
78             ex.printStackTrace();
79             log.info("计算点处理异常!");
80         }
81         return result;
82     }
83
2fcc1a 84     private Object singleCompute(DaPointDTO dto, Map<String, Object> dataMap,List<String> listGood,List<String> listBad) {
a6de49 85         String expression = dto.getExpression();
6a8e24 86         log.info("PointNo=" + dto.getPointNo() + ";SourceExpression=" + expression);
a6de49 87         String[] arr = expression.split(regex);
3c511d 88         // 去掉arr中的空格
D 89         arr = Stream.of(arr).filter(StringUtils::isNotBlank).toArray(String[]::new);
2fcc1a 90         // 判断arr都在dataMap中包含
D 91         if (!Arrays.stream(arr).allMatch(dataMap::containsKey)) {
6a8e24 92             log.info("dataMap not contains key");
2fcc1a 93             listBad.add(dto.getPointNo());
D 94             return CommonConstant.BAD_VALUE;
95         }
a6de49 96
H 97         for (int i = 0; i < arr.length; i++) {
98             String s = arr[i];
99             if (StringUtils.isNotBlank(s) && dataMap.containsKey(s)) {
2fcc1a 100                 // 对每个数加(),否则负值报错
D 101                 expression = expression.replace(s, "(" + dataMap.get(s).toString() + ")");
a6de49 102             }
H 103         }
104         expression = expression.replace("&", "&&");
105         expression = expression.replace("|", "||");
106         expression = expression.replace("False", "false");
107         expression = expression.replace("True", "true");
108         log.info("PointNo=" + dto.getPointNo() + ";expression=" + expression);
109         String result = javaScriptHandler.eval(expression);
110         log.info("result=" + result);
2fcc1a 111         if (result == null || result.contains(JsErrorCode.Infinity.name()) || result.contains(JsErrorCode.NaN.name())) {
D 112             listBad.add(dto.getPointNo());
781e72 113             return CommonConstant.BAD_VALUE;
a6de49 114         } else {
H 115             if (DataTypeEnum.INT.getCode().equals(dto.getDataType())) {
2fcc1a 116                 listGood.add(dto.getPointNo());
a6de49 117                 return new BigDecimal(result).intValue();
H 118             } else if (DataTypeEnum.FLOAT.getCode().equals(dto.getDataType())) {
2fcc1a 119                 listGood.add(dto.getPointNo());
781e72 120                 return new BigDecimal(result).setScale(4, BigDecimal.ROUND_UP).doubleValue();
a6de49 121             } else if (DataTypeEnum.BOOLEAN.getCode().equals(dto.getDataType())) {
2fcc1a 122                 listGood.add(dto.getPointNo());
a6de49 123                 return Boolean.parseBoolean(result);
2fcc1a 124             } else {
D 125                 listBad.add(dto.getPointNo());
126                 throw new RuntimeException("计算异常,未知数据类型");
a6de49 127             }
H 128         }
129     }
130
131     public Map<String, Object> getCurrent(List<String> pointNos) {
132         Map<String, Object> data = new HashMap<>();
133         List<DaPointDTO> pointMathList = daPointService.getMathPoint(pointNos);
134         if (CollectionUtils.isEmpty(pointMathList)) {
135             return data;
136         }
137         pointMathList.forEach(item -> {
781e72 138             Object value = CommonConstant.BAD_VALUE;
139             if (redisTemplate.hasKey(PointCollector.PV + item.getPointNo())) {
140                 value = redisTemplate.opsForValue().get(PointCollector.PV + item.getPointNo());
141             } else {
142                 value = singleCompute(item);
143             }
144             data.put(item.getPointNo(), value);
a6de49 145         });
H 146         return data;
147     }
148
149     private Object singleCompute(DaPointDTO dto) {
781e72 150         String result = CommonConstant.BAD_VALUE.toString();
a6de49 151         Map<String, Object> dataMap = new HashMap<>();
H 152         String expression = dto.getExpression();
153         String[] arr = expression.split(regex);
154         for (int i = 0; i < arr.length; i++) {
155             String s = arr[i];
156             if (StringUtils.isBlank(s)) {
157                 continue;
158             }
159             List<String> pointNos = new ArrayList<>();
160             pointNos.add(s);
161             dataMap.putAll(measureHandle.getCurrent(pointNos));
162             dataMap.putAll(constantHandle.getCurrent(pointNos));
48d2a2 163             if (dataMap.get(s) == null) {
164                 log.info("计算点数据异常");
165                 log.info("pointNo=" + dto.getPointNo() +";dataMap.key=" + s);
166                 return CommonConstant.BAD_VALUE;
167             }
140065 168             String valueStr =  dataMap.get(s).toString();
169             if (StrUtils.isNumeric(valueStr) && new BigDecimal(valueStr).compareTo(CommonConstant.BAD_VALUE) == 0) {
170                 log.info("BAD_VALUE:" + s);
171             }
172             if (StrUtils.isNumeric(valueStr) && new BigDecimal(valueStr).compareTo(BigDecimal.ZERO) < 0) {
173                 valueStr = "(" + valueStr + ")";
174             }
175             expression = expression.replace(s, valueStr);
a6de49 176         }
H 177         expression = expression.replace("&", "&&");
178         expression = expression.replace("|", "||");
179         expression = expression.replace("False", "false");
180         expression = expression.replace("True", "true");
181         log.info("PointNo=" + dto.getPointNo() + ";expression=" + expression);
781e72 182         result = javaScriptHandler.eval(expression);
a6de49 183         log.info("result=" + result);
H 184         if (result == null) {
781e72 185             return CommonConstant.BAD_VALUE;
186         } else if (result.contains(JsErrorCode.Infinity.name()) || result.contains(JsErrorCode.NaN.name())) {
a6de49 187             log.info("计算异常,使用默认值");
H 188             return dto.getDefaultValue() == null ? BigDecimal.ZERO : dto.getDefaultValue();
189         } else {
190             if (DataTypeEnum.INT.getCode().equals(dto.getDataType())) {
191                 return new BigDecimal(result).intValue();
192             } else if (DataTypeEnum.FLOAT.getCode().equals(dto.getDataType())) {
781e72 193                 return new BigDecimal(result).setScale(2, BigDecimal.ROUND_UP).doubleValue();
a6de49 194             } else if (DataTypeEnum.BOOLEAN.getCode().equals(dto.getDataType())) {
H 195                 return Boolean.parseBoolean(result);
196             }
197         }
198         return result;
199     }
200 }