| | |
| | | package com.iailab.module.data.point.collection.handler; |
| | | |
| | | 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.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 lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import javax.annotation.Resource; |
| | | |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.util.CollectionUtils; |
| | | |
| | |
| | | |
| | | @Resource |
| | | private JavaScriptHandler javaScriptHandler; |
| | | |
| | | @Autowired |
| | | private RedisTemplate<String, Object> redisTemplate; |
| | | |
| | | public static final String regex = "[+\\-\\*/()\\&\\|\\>\\<]"; |
| | | |
| | |
| | | String result = javaScriptHandler.eval(expression); |
| | | log.info("result=" + result); |
| | | if (result == null) { |
| | | return null; |
| | | return CommonConstant.BAD_VALUE; |
| | | } else if (result.contains(JsErrorCode.Infinity.name()) || |
| | | result.contains(JsErrorCode.NaN.name())) { |
| | | log.info("计算异常,使用默认值"); |
| | |
| | | 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(10, BigDecimal.ROUND_UP).doubleValue(); |
| | | return new BigDecimal(result).setScale(4, BigDecimal.ROUND_UP).doubleValue(); |
| | | } else if (DataTypeEnum.BOOLEAN.getCode().equals(dto.getDataType())) { |
| | | return Boolean.parseBoolean(result); |
| | | } |
| | |
| | | return data; |
| | | } |
| | | pointMathList.forEach(item -> { |
| | | data.put(item.getPointNo(), singleCompute(item)); |
| | | Object value = CommonConstant.BAD_VALUE; |
| | | if (redisTemplate.hasKey(PointCollector.PV + item.getPointNo())) { |
| | | value = redisTemplate.opsForValue().get(PointCollector.PV + item.getPointNo()); |
| | | } else { |
| | | value = singleCompute(item); |
| | | } |
| | | data.put(item.getPointNo(), value); |
| | | }); |
| | | return data; |
| | | } |
| | | |
| | | private Object singleCompute(DaPointDTO dto) { |
| | | String result = CommonConstant.BAD_VALUE.toString(); |
| | | Map<String, Object> dataMap = new HashMap<>(); |
| | | String expression = dto.getExpression(); |
| | | String[] arr = expression.split(regex); |
| | |
| | | expression = expression.replace("False", "false"); |
| | | expression = expression.replace("True", "true"); |
| | | log.info("PointNo=" + dto.getPointNo() + ";expression=" + expression); |
| | | String result = javaScriptHandler.eval(expression); |
| | | result = javaScriptHandler.eval(expression); |
| | | log.info("result=" + result); |
| | | if (result == null) { |
| | | return null; |
| | | } else if (result.contains(JsErrorCode.Infinity.name()) || |
| | | result.contains(JsErrorCode.NaN.name())) { |
| | | 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(10, BigDecimal.ROUND_UP).doubleValue(); |
| | | return new BigDecimal(result).setScale(2, BigDecimal.ROUND_UP).doubleValue(); |
| | | } else if (DataTypeEnum.BOOLEAN.getCode().equals(dto.getDataType())) { |
| | | return Boolean.parseBoolean(result); |
| | | } |