| | |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | import java.util.stream.Stream; |
| | | |
| | | /** |
| | | * 计算点处理 |
| | |
| | | |
| | | public static final String regex = "[+\\-\\*/()\\&\\|\\>\\<]"; |
| | | |
| | | public List<InfluxPointValuePOJO> handle(Date collectTime, List<DaPointDTO> dtos, Map<String, Object> dataMap) { |
| | | 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("计算点处理开始"); |
| | |
| | | } |
| | | dtos.forEach(dto -> { |
| | | try { |
| | | Object value = singleCompute(dto, dataMap); |
| | | Object value = singleCompute(dto, dataMap, listGood, listBad); |
| | | InfluxPointValuePOJO pojo = GenInfluxPointValueUtils.getByPoint(dto, value); |
| | | pojo.setTimestamp(collectTime.toInstant()); |
| | | result.add(pojo); |
| | |
| | | return result; |
| | | } |
| | | |
| | | private Object singleCompute(DaPointDTO dto, Map<String, Object> dataMap) { |
| | | private Object singleCompute(DaPointDTO dto, Map<String, Object> dataMap,List<String> listGood,List<String> listBad) { |
| | | String expression = dto.getExpression(); |
| | | 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)) { |
| | | 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()); |
| | | // 对每个数加(),否则负值报错 |
| | | expression = expression.replace(s, "(" + dataMap.get(s).toString() + ")"); |
| | | } |
| | | } |
| | | expression = expression.replace("&", "&&"); |
| | |
| | | log.info("PointNo=" + dto.getPointNo() + ";expression=" + expression); |
| | | String result = javaScriptHandler.eval(expression); |
| | | log.info("result=" + result); |
| | | if (result == null) { |
| | | if (result == null || result.contains(JsErrorCode.Infinity.name()) || result.contains(JsErrorCode.NaN.name())) { |
| | | listBad.add(dto.getPointNo()); |
| | | 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())) { |
| | | 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("计算异常,未知数据类型"); |
| | | } |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | public Map<String, Object> getCurrent(List<String> pointNos) { |
| | |
| | | pointNos.add(s); |
| | | dataMap.putAll(measureHandle.getCurrent(pointNos)); |
| | | dataMap.putAll(constantHandle.getCurrent(pointNos)); |
| | | 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); |