package com.iailab.module.data.point.collection; 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.point.collection.handler.CalculateHandle; import com.iailab.module.data.point.collection.handler.CumulateHandle; import com.iailab.module.data.point.common.PointTypeEnum; import com.iailab.module.data.point.dto.DaPointDTO; import com.iailab.module.data.point.service.DaPointService; import com.iailab.module.data.influxdb.pojo.InfluxPointValuePOJO; 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.handler.ConstantHandle; import com.iailab.module.data.point.collection.handler.MeasureHandle; import com.iailab.module.data.point.dto.DaPointWriteValueDTO; import com.iailab.module.data.influxdb.service.InfluxDBService; 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年04月25日 16:16:00 */ @Slf4j @Component public class PointCollector { @Resource private DaPointService daPointService; @Resource private ConstantHandle constantHandle; @Resource private MeasureHandle measureHandle; @Resource private CalculateHandle calculateHandle; @Resource private KingIOCollector kingIOCollector; @Resource private InfluxDBService influxDBService; @Resource private ModBusCollector modBusCollector; @Resource private OpcUaCollector opcUaCollector; @Resource private CumulateHandle cumulateHandle; /** * 采集 * * @param collectTime * @param minfreq */ public void collect(Date collectTime, String minfreq) { try { Map dataMap = new HashMap<>(); List pointValues = new ArrayList<>(); log.info("读取常量点"); List pointConstantList = daPointService.getConstantPoint(minfreq); pointValues.addAll(constantHandle.handle(collectTime, pointConstantList, dataMap)); log.info("读取测量点"); List pointMeasureList = daPointService.getMeasurePoint(minfreq); pointValues.addAll(measureHandle.handle(collectTime, pointMeasureList, dataMap)); log.info("读取计算点"); List pointCalculateList = daPointService.getMathPoint(minfreq); pointValues.addAll(calculateHandle.handle(collectTime, pointCalculateList, dataMap)); log.info("读取累计点"); List pointCumulateList = daPointService.getCumulatePoint(minfreq); pointValues.addAll(cumulateHandle.handle(collectTime, pointCumulateList)); log.info("存入数据库"); influxDBService.asyncWritePointValues(pointValues); log.info("采集完成"); } catch (Exception ex) { log.info("采集异常!"); ex.printStackTrace(); } } public Map getCurrentValue(List pointNos) { try { Map data = new HashMap<>(); if (CollectionUtils.isEmpty(pointNos)) { return data; } data.putAll(constantHandle.getCurrent(pointNos)); data.putAll(measureHandle.getCurrent(pointNos)); data.putAll(calculateHandle.getCurrent(pointNos)); return data; } catch (Exception ex) { return R.error(ex.getMessage()); } } public void setValue(DaPointWriteValueDTO writeValue) throws Exception { DaPointDTO daPointDTO = daPointService.getByNo(writeValue.getPointNo()); if (daPointDTO == null) { throw new Exception("点位不存在"); } if (PointTypeEnum.CONSTANT.getCode().equals(daPointDTO.getPointType())) { daPointDTO.setDefaultValue(new BigDecimal(writeValue.getPointValue().toString())); daPointService.updateDefaultValue(daPointDTO); } else if (PointTypeEnum.MEASURE_POINT.getCode().equals(daPointDTO.getPointType())) { DaPointDTO mPoint = daPointService.getMeasurePointByNo(daPointDTO.getPointNo()); if (DataSourceType.OPCUA.getCode().equals(mPoint.getSourceType())) { opcUaCollector.setTagData(mPoint.getSourceId(), mPoint.getTagNo(), writeValue.getPointValue().toString(), mPoint.getDataType()); } else if (DataSourceType.ModBus.getCode().equals(mPoint.getSourceType())) { modBusCollector.setTagValue(mPoint.getSourceId(), mPoint.getTagNo(), writeValue.getPointValue().toString()); } else if (DataSourceType.KIO.getCode().equals(mPoint.getSourceType())) { kingIOCollector.setTagValue(mPoint.getSourceId(), mPoint.getTagNo(), writeValue.getPointValue().toString(), mPoint.getDataType()); } else { log.info("没有匹配的TagNo=" + mPoint.getTagNo()); } } } }