package com.iailab.module.data.point.collection.handler;
|
|
import com.iailab.module.data.channel.http.collector.SourceApiEnum;
|
import com.iailab.module.data.channel.http.collector.ihdb.HttpCollectorForIhd;
|
import com.iailab.module.data.channel.opcda.collector.OpcDACollector;
|
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.influxdb.pojo.InfluxPointValuePOJO;
|
import lombok.extern.slf4j.Slf4j;
|
import javax.annotation.Resource;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
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;
|
|
@Autowired
|
private OpcDACollector opcDACollector;
|
|
@Autowired
|
private HttpCollectorForIhd httpCollectorForIhd;
|
|
@Resource
|
private DaPointService daPointService;
|
|
public List<InfluxPointValuePOJO> handle(Date collectTime, List<DaPointDTO> dtos, Map<String, Object> dataMap) {
|
log.info("测量点处理开始");
|
List<InfluxPointValuePOJO> result = new ArrayList<>();
|
if (CollectionUtils.isEmpty(dtos)) {
|
return result;
|
}
|
|
List<String[]> opcUaTagIds = new ArrayList<>();
|
List<String[]> opcDaTagIds = new ArrayList<>();
|
List<String[]> modbusTagIds = new ArrayList<>();
|
List<String[]> kioTagIds = new ArrayList<>();
|
List<Object[]> httpTagIhd = 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 (SourceApiEnum.iHyperDB.getCode().equals(item.getSourceName())) {
|
if (item.getTagNo() != null && item.getDimension() != null && item.getValueType() != null) {
|
httpTagIhd.add(new Object[]{item.getSourceId(), item.getTagNo(), item.getDimension(), item.getValueType()});
|
}
|
}
|
}
|
});
|
|
Map<String, Object> tagValues = new HashMap<>();
|
if (!CollectionUtils.isEmpty(opcUaTagIds)) {
|
tagValues.putAll(opcUaCollector.getTagValues(opcUaTagIds));
|
}
|
if (!CollectionUtils.isEmpty(opcDaTagIds)) {
|
tagValues.putAll(opcDACollector.getTagValues(modbusTagIds));
|
}
|
if (!CollectionUtils.isEmpty(modbusTagIds)) {
|
tagValues.putAll(modBusCollector.getTagValues(modbusTagIds));
|
}
|
if (!CollectionUtils.isEmpty(kioTagIds)) {
|
tagValues.putAll(kingIOCollector.getTagValues(kioTagIds));
|
}
|
if (!CollectionUtils.isEmpty(httpTagIhd)) {
|
tagValues.putAll(httpCollectorForIhd.getTagValues(httpTagIhd, collectTime));
|
}
|
this.toCommonResult(collectTime, dtos, tagValues, dataMap, result);
|
log.info("测量点处理结束");
|
return result;
|
}
|
|
private void toCommonResult(Date collectTime, List<DaPointDTO> dtos, Map<String, Object> tagValues,
|
Map<String, Object> dataMap, List<InfluxPointValuePOJO> 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 (value == null) {
|
return CommonConstant.BAD_VALUE;
|
}
|
if (DataTypeEnum.FLOAT.getCode().equals(dto.getDataType()) || DataTypeEnum.INT.getCode().equals(dto.getDataType())) {
|
BigDecimal rawValue = new BigDecimal(value.toString());
|
if(CommonConstant.BAD_VALUE.compareTo(rawValue) == 0) {
|
return CommonConstant.BAD_VALUE;
|
}
|
// 异常值处理
|
if (rawValue.compareTo(maxValue) > 0 || rawValue.compareTo(minValue) < 0) {
|
return 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<String, Object> getCurrent(List<String> pointNos) {
|
Map<String, Object> data = new HashMap<>();
|
List<DaPointDTO> 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())) {
|
value = httpCollectorForIhd.getTagValue(item.getSourceId(), item.getTagNo(), item.getDimension(), item.getValueType());
|
} 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;
|
}
|
}
|