package com.iailab.module.data.point.collection.handler; import com.iailab.module.data.common.enums.CommonConstant; import com.iailab.module.data.common.enums.DataSourceType; import com.iailab.module.data.common.enums.DataTypeEnum; import com.iailab.module.data.common.utils.TagUtils; 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.utils.GenInfluxPointValueUtils; import com.iailab.module.data.point.common.PointDataTypeEnum; import com.iailab.module.data.point.dto.DaPointDTO; import com.iailab.module.data.point.service.DaPointService; import com.iailab.module.data.http.collector.HttpCollectorForZxzk; import com.iailab.module.data.http.service.impl.HttpTagCollector; import com.iailab.module.data.influxdb.pojo.InfluxPointValuePOJO; import lombok.extern.slf4j.Slf4j; import javax.annotation.Resource; import org.springframework.stereotype.Component; import org.springframework.util.CollectionUtils; import java.math.BigDecimal; import java.util.*; /** * 测量点处理 * * @author PanZhibao * @Description * @createTime 2023年05月03日 22:36:00 */ @Slf4j @Component public class MeasureHandle { private BigDecimal maxValue = new BigDecimal("1000000000"); private BigDecimal minValue = new BigDecimal("0"); @Resource private ModBusCollector modBusCollector; @Resource private KingIOCollector kingIOCollector; @Resource private OpcUaCollector opcUaCollector; @Resource private HttpTagCollector httpTagCollector; @Resource private HttpCollectorForZxzk httpCollectorForZxzk; @Resource private DaPointService daPointService; public List handle(Date collectTime, List dtos, Map dataMap) { log.info("测量点处理开始"); List result = new ArrayList<>(); if (CollectionUtils.isEmpty(dtos)) { return result; } List opcuaTagIds = new ArrayList<>(); List opcDaTagIds = new ArrayList<>(); List modbusTagIds = new ArrayList<>(); List kioTagIds = new ArrayList<>(); List httpTagZxzk = new ArrayList<>(); List tagYearPeiIds = new ArrayList<>(); List tagCurrentPerformanceIds = new ArrayList<>(); List tagCurrentSaleIds = new ArrayList<>(); List tagPrdTimeDistIds = new ArrayList<>(); dtos.stream().forEach(item -> { if (DataSourceType.OPCUA.getCode().equals(item.getSourceType())) { opcuaTagIds.add(new String[]{item.getSourceId(), item.getTagNo()}); } else if (DataSourceType.OPCDA.getCode().equals(item.getSourceType())) { opcDaTagIds.add(new String[]{item.getSourceId(), item.getTagNo()}); } else if (DataSourceType.ModBus.getCode().equals(item.getSourceType())) { modbusTagIds.add(new String[]{item.getSourceId(), item.getTagNo()}); } else if (DataSourceType.KIO.getCode().equals(item.getSourceType())) { kioTagIds.add(new String[]{item.getSourceId(), item.getTagNo()}); } else if (DataSourceType.HTTP.getCode().equals(item.getSourceType())) { if (CommonConstant.YEAR_PEI_HTTP_TAG.equals(item.getSourceId())) { tagYearPeiIds.add(item.getTagNo()); } else if (CommonConstant.CURRENT_PERFORMANCE_HTTP_TAG.equals(item.getSourceId())) { tagCurrentPerformanceIds.add(item.getTagNo()); } else if (CommonConstant.CURRENT_SALE_HTTP_TAG.equals(item.getSourceId())) { tagCurrentSaleIds.add(item.getTagNo()); } else if (CommonConstant.PRD_TIME_DIST_HTTP_TAG.equals(item.getSourceId())) { tagPrdTimeDistIds.add(item.getTagNo()); } else if (CommonConstant.HTTP_API_ZXZK_IH.equals(item.getSourceName())) { httpTagZxzk.add(new String[]{item.getSourceId(), item.getTagNo()}); } } }); if (!CollectionUtils.isEmpty(opcuaTagIds)) { Map tagValues = opcUaCollector.getTagValues(opcuaTagIds); this.toCommonResult(collectTime, dtos, tagValues, dataMap, result); } if (!CollectionUtils.isEmpty(opcDaTagIds)) { } if (!CollectionUtils.isEmpty(modbusTagIds)) { Map tagValues = modBusCollector.getTagValues(modbusTagIds); this.toCommonResult(collectTime, dtos, tagValues, dataMap, result); } if (!CollectionUtils.isEmpty(kioTagIds)) { Map tagValues = kingIOCollector.getTagValues(kioTagIds); this.toCommonResult(collectTime, dtos, tagValues, dataMap, result); } if (!CollectionUtils.isEmpty(httpTagZxzk)) { Map tagValues = httpCollectorForZxzk.getTagValues(httpTagZxzk); this.toCommonResult(collectTime, dtos, tagValues, dataMap, result); } if (!CollectionUtils.isEmpty(tagYearPeiIds)) { Map tagValues = httpTagCollector.collect(CommonConstant.YEAR_PEI_HTTP_TAG, tagYearPeiIds); this.toCommonResult(collectTime, dtos, tagValues, dataMap, result); } else if (!CollectionUtils.isEmpty(tagCurrentPerformanceIds)) { Map tagValues = httpTagCollector.collect(CommonConstant.CURRENT_PERFORMANCE_HTTP_TAG, tagCurrentPerformanceIds); this.toCommonResult(collectTime, dtos, tagValues, dataMap, result); } else if (!CollectionUtils.isEmpty(tagCurrentSaleIds)) { Map tagValues = httpTagCollector.collect(CommonConstant.CURRENT_SALE_HTTP_TAG, tagCurrentSaleIds); this.toCommonResult(collectTime, dtos, tagValues, dataMap, result); } else if (!CollectionUtils.isEmpty(tagPrdTimeDistIds)) { Map tagValues = httpTagCollector.collect(CommonConstant.PRD_TIME_DIST_HTTP_TAG, tagPrdTimeDistIds); this.toCommonResult(collectTime, dtos, tagValues, dataMap, result); } log.info("测量点处理结束"); return result; } private void toCommonResult(Date collectTime, List dtos, Map tagValues, Map dataMap, List result) { if (!CollectionUtils.isEmpty(tagValues)) { tagValues.forEach((k, v) -> { dataMap.put(k, v); }); dtos.forEach(dto -> { String tagId = TagUtils.genTagId(dto.getSourceType(), dto.getSourceName(), dto.getTagNo()); if (tagValues.get(tagId) != null) { Object value = handleData(dto, tagValues.get(tagId)); InfluxPointValuePOJO pojo = GenInfluxPointValueUtils.getByPoint(dto, value); pojo.setTimestamp(collectTime.toInstant()); dataMap.put(dto.getPointNo(), value); result.add(pojo); } else { System.out.println("值异常!TagId=" + tagId); } }); } } private Object handleData(DaPointDTO dto, Object value) { Object result = value; try { if (DataTypeEnum.FLOAT.getCode().equals(dto.getDataType()) || DataTypeEnum.INT.getCode().equals(dto.getDataType())) { BigDecimal rawValue = new BigDecimal(value.toString()); // 异常值处理 if (rawValue.compareTo(maxValue) > 0 || rawValue.compareTo(minValue) < 0) { rawValue = CommonConstant.BAD_VALUE; } BigDecimal coefficient = dto.getUnittransfactor() == null ? BigDecimal.ONE : dto.getUnittransfactor(); BigDecimal calValue = rawValue.multiply(coefficient); if (dto.getMaxValue() != null && calValue.compareTo(dto.getMaxValue()) > 0) { result = dto.getMaxValue(); } else if (dto.getMinValue() != null && calValue.compareTo(dto.getMinValue()) < 0) { result = dto.getMinValue(); } else { result = calValue; } if (DataTypeEnum.FLOAT.getCode().equals(dto.getDataType())) { result = ((BigDecimal) result).doubleValue(); } else { result = ((BigDecimal) result).intValue(); } } else if (DataTypeEnum.BOOLEAN.getCode().equals(dto.getDataType())) { result = Boolean.parseBoolean(value.toString()); } } catch (Exception ex) { log.warn("handleData异常,PointNo=" + dto.getPointNo()); ex.printStackTrace(); } return result; } public Map getCurrent(List pointNos) { Map data = new HashMap<>(); List pointMeasureList = daPointService.getMeasurePoint(pointNos); pointMeasureList.forEach( item -> { try { Object value = CommonConstant.BAD_VALUE; 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()); } else if (DataSourceType.KIO.getCode().equals(item.getSourceType())) { value = kingIOCollector.getTagValue(item.getSourceId(), item.getTagNo()); } else if (DataSourceType.HTTP.getCode().equals(item.getSourceType())) { if (CommonConstant.HTTP_API_ZXZK_IH.equals(item.getSourceName())) { value = httpCollectorForZxzk.getTagValue(item.getSourceId(), item.getTagNo()); } } else { log.info("没有匹配的TagNo=" + item.getTagNo()); } log.info("没有匹配的TagNo=" + item.getTagNo()); log.info("valueStr=" + value.toString()); log.info("DataType=" + item.getDataType()); if (!PointDataTypeEnum.BOOLEAN.getCode().equals(item.getDataType())) { BigDecimal decValue = new BigDecimal(value.toString()); if (PointDataTypeEnum.FLOAT.getCode().equals(item.getDataType())) { decValue = decValue.setScale(2, BigDecimal.ROUND_HALF_UP); } else if (PointDataTypeEnum.INT.getCode().equals(item.getDataType())) { decValue = decValue.setScale(0, BigDecimal.ROUND_HALF_UP); } data.put(item.getPointNo(), decValue); } else { data.put(item.getPointNo(), value); } } catch (Exception ex) { ex.printStackTrace(); } } ); return data; } }