iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/point/collection/PointCollector.java
@@ -3,6 +3,7 @@ import com.iailab.module.data.common.enums.DataSourceType; import com.iailab.module.data.common.utils.R; import com.iailab.module.data.channel.kio.collector.KingIOCollector; import com.iailab.module.data.influxdb.pojo.InfluxPointValueBoolPOJO; import com.iailab.module.data.influxdb.pojo.InfluxPointValueDigPOJO; import com.iailab.module.data.influxdb.pojo.InfluxPointValueSimPOJO; import com.iailab.module.data.point.collection.handler.CalculateHandle; @@ -23,6 +24,7 @@ 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; @@ -68,6 +70,13 @@ @Autowired private DaPointCollectStatusService daPointCollectStatusService; @Autowired private RedisTemplate<String, Object> redisTemplate; public static final String PV = "PV_"; public static final int offset = 60 * 3; /** * 采集 * @@ -95,9 +104,22 @@ List<DaPointDTO> pointCumulateList = daPointService.getCumulatePoint(minfreq); pointValues.addAll(cumulateHandle.handle(collectTime, pointCumulateList)); log.info("存入数据库"); log.info("存入时序库"); influxDBService.asyncWritePointValues(pointValues); log.info("存入缓存"); for (InfluxPointValuePOJO pointValue : pointValues) { if (pointValue instanceof InfluxPointValueSimPOJO) { InfluxPointValueSimPOJO simPOJO = (InfluxPointValueSimPOJO) pointValue; redisTemplate.opsForValue().set(PV + simPOJO.getPoint(), simPOJO.getValue(), offset); } else if (pointValue instanceof InfluxPointValueDigPOJO) { InfluxPointValueDigPOJO digPOJO = (InfluxPointValueDigPOJO) pointValue; redisTemplate.opsForValue().set(PV + digPOJO.getPoint(), digPOJO.getValue(), offset); } else if (pointValue instanceof InfluxPointValueBoolPOJO) { InfluxPointValueBoolPOJO boolPOJO = (InfluxPointValueBoolPOJO) pointValue; redisTemplate.opsForValue().set(PV + boolPOJO.getPoint(), boolPOJO.getValue(), offset); } } log.info("更新采集状态"); daPointCollectStatusService.recordStatusList(pointValues, collectTime); log.info("采集完成"); iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/point/collection/handler/CalculateHandle.java
@@ -1,8 +1,10 @@ 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; @@ -10,6 +12,9 @@ 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; @@ -38,6 +43,9 @@ @Resource private JavaScriptHandler javaScriptHandler; @Autowired private RedisTemplate<String, Object> redisTemplate; public static final String regex = "[+\\-\\*/()\\&\\|\\>\\<]"; @@ -86,7 +94,7 @@ 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("计算异常,使用默认值"); @@ -95,7 +103,7 @@ 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); } @@ -110,12 +118,19 @@ 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); @@ -135,19 +150,18 @@ 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); } iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/point/collection/handler/CumulateHandle.java
@@ -7,12 +7,14 @@ import com.iailab.module.data.common.enums.CommonConstant; import com.iailab.module.data.enums.DataPointFreqEnum; import com.iailab.module.data.influxdb.pojo.InfluxPointValuePOJO; 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.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; import org.springframework.util.CollectionUtils; @@ -37,6 +39,9 @@ @Autowired @Lazy private DataPointApi dataPointApi; @Autowired private RedisTemplate<String, Object> redisTemplate; public List<InfluxPointValuePOJO> handle(Date collectTime, List<DaPointDTO> dtos) { List<InfluxPointValuePOJO> result = new ArrayList<>(); @@ -73,7 +78,13 @@ Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.MILLISECOND, 0); pointMathList.forEach(item -> { data.put(item.getPointNo(), singleCompute(item, calendar.getTime())); Object value = CommonConstant.BAD_VALUE; if (redisTemplate.hasKey(PointCollector.PV + item.getPointNo())) { value = redisTemplate.opsForValue().get(PointCollector.PV + item.getPointNo()); } else { value = singleCompute(item, calendar.getTime()); } data.put(item.getPointNo(), value); }); return data; } iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/point/collection/handler/MeasureHandle.java
@@ -10,6 +10,7 @@ import com.iailab.module.data.channel.kio.collector.KingIOCollector; import com.iailab.module.data.channel.modbus.collector.ModBusCollector; import com.iailab.module.data.channel.opcua.collector.OpcUaCollector; import com.iailab.module.data.point.collection.PointCollector; import com.iailab.module.data.point.collection.utils.GenInfluxPointValueUtils; import com.iailab.module.data.point.common.PointDataTypeEnum; import com.iailab.module.data.point.dto.DaPointDTO; @@ -19,6 +20,7 @@ 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; @@ -57,6 +59,9 @@ @Resource private DaPointService daPointService; @Autowired private RedisTemplate<String, Object> redisTemplate; public List<InfluxPointValuePOJO> handle(Date collectTime, List<DaPointDTO> dtos, Map<String, Object> dataMap) { log.info("测量点处理开始"); @@ -178,7 +183,9 @@ item -> { try { Object value = CommonConstant.BAD_VALUE; if (DataSourceType.OPCUA.getCode().equals(item.getSourceType())) { if (redisTemplate.hasKey(PointCollector.PV + item.getPointNo())) { value = redisTemplate.opsForValue().get(PointCollector.PV + item.getPointNo()); } else if (DataSourceType.OPCUA.getCode().equals(item.getSourceType())) { value = opcUaCollector.getTagValue(item.getSourceId(), item.getTagNo()); } else if (DataSourceType.ModBus.getCode().equals(item.getSourceType())) { value = modBusCollector.getTagValue(item.getSourceId(), item.getTagNo()); @@ -186,12 +193,13 @@ value = kingIOCollector.getTagValue(item.getSourceId(), item.getTagNo()); } else if (DataSourceType.HTTP.getCode().equals(item.getSourceType())) { value = httpCollectorForIhd.getTagValue(item.getSourceId(), item.getTagNo(), item.getDimension(), item.getValueType()); // 存入缓存 redisTemplate.opsForValue().set(PointCollector.PV + item.getPointNo(), value, PointCollector.offset); } else { log.info("没有匹配的TagNo=" + item.getTagNo()); } log.info("没有匹配的TagNo=" + item.getTagNo()); log.info("valueStr=" + value.toString()); log.info("DataType=" + item.getDataType()); log.info("TagNo=" + item.getTagNo() + ",value=" + value.toString()); if (!PointDataTypeEnum.BOOLEAN.getCode().equals(item.getDataType())) { BigDecimal decValue = new BigDecimal(value.toString()); if (PointDataTypeEnum.FLOAT.getCode().equals(item.getDataType())) {