package com.iailab.module.data.channel.opcua.collector;
|
|
import com.iailab.module.data.common.enums.CommonConstant;
|
import com.iailab.module.data.common.enums.DataSourceType;
|
import com.iailab.framework.common.util.object.ConvertUtils;
|
import com.iailab.module.data.common.utils.TagUtils;
|
import com.iailab.module.data.channel.opcua.entity.ChannelOPCUADeviceEntity;
|
import com.iailab.module.data.channel.opcua.service.ChannelOPCUADeviceService;
|
import com.iailab.module.data.channel.opcua.dto.ChannelOPCUADeviceDTO;
|
import lombok.extern.slf4j.Slf4j;
|
import org.eclipse.milo.opcua.sdk.client.OpcUaClient;
|
import javax.annotation.Resource;
|
import org.springframework.stereotype.Component;
|
import org.springframework.util.CollectionUtils;
|
|
import java.math.BigDecimal;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.concurrent.ConcurrentHashMap;
|
|
/**
|
* @author PanZhibao
|
* @Description
|
* @createTime 2024年05月12日
|
*/
|
@Slf4j
|
@Component
|
public class OpcUaCollector {
|
|
@Resource
|
private OpcUaUtils opcUaUtils;
|
|
@Resource
|
private ChannelOPCUADeviceService channelOPCUADeviceService;
|
|
private Map<String, OpcUaClient> clientMap = new ConcurrentHashMap<>();
|
|
private Map<String, ChannelOPCUADeviceEntity> deviceMap = new HashMap<>();
|
|
|
private OpcUaClient getClient(String sourceId) throws Exception {
|
try {
|
if (!clientMap.containsKey(sourceId)) {
|
log.info("根据数据源获取Client,sourceId=" + sourceId);
|
ChannelOPCUADeviceEntity opcuaDevice = channelOPCUADeviceService.info(sourceId);
|
deviceMap.put(sourceId, opcuaDevice);
|
ChannelOPCUADeviceDTO configDto = ConvertUtils.sourceToTarget(opcuaDevice, ChannelOPCUADeviceDTO.class);
|
OpcUaClient opcUaClient = opcUaUtils.createClient(configDto);
|
if (opcUaClient != null) {
|
clientMap.put(sourceId, opcUaClient);
|
}
|
}
|
} catch (Exception ex) {
|
log.info("=========getClient Exception============");
|
log.info("ex.message+" + ex.getMessage());
|
ex.printStackTrace();
|
throw new Exception(ex.getMessage());
|
}
|
OpcUaClient opcUaClient = clientMap.get(sourceId);
|
if (!opcUaClient.connect().isDone()) {
|
opcUaClient.connect().get();
|
}
|
return opcUaClient;
|
}
|
|
public String getTagValue(String sourceId, String tagNo) throws Exception {
|
OpcUaClient opcUaClient = this.getClient(sourceId);
|
String value = OpcUaUtils.readNode(opcUaClient, tagNo);
|
return value;
|
}
|
|
public void setTagData(String sourceId, String tagNo, String newValue, String dataType) {
|
try {
|
OpcUaClient opcUaClient = this.getClient(sourceId);
|
switch (dataType) {
|
case "float":
|
log.debug("Float,TagNo=" + tagNo + ",Value=" + Float.parseFloat(newValue));
|
OpcUaUtils.writeFloatValue(opcUaClient, tagNo, Float.parseFloat(newValue));
|
break;
|
case "int":
|
log.debug("Int,TagNo=" + tagNo + ",Value=" + new BigDecimal(newValue).intValue());
|
OpcUaUtils.writeIntValue(opcUaClient, tagNo, new BigDecimal(newValue).intValue());
|
break;
|
case "boolean":
|
log.debug("Boolean,TagNo=" + tagNo + ",Value=" + newValue);
|
OpcUaUtils.writeBooleanValue(opcUaClient, tagNo, Boolean.parseBoolean(newValue));
|
break;
|
default:
|
log.warn("##################### No DataType ####################");
|
break;
|
}
|
|
} catch (Exception ex) {
|
ex.printStackTrace();
|
}
|
}
|
|
public Map<String, Object> getTagValues(List<String[]> params) {
|
if (CollectionUtils.isEmpty(params)) {
|
return new HashMap<>();
|
}
|
Map<String, Object> result = new HashMap<>(params.size());
|
params.forEach(item -> {
|
|
try {
|
OpcUaClient opcUaClient = this.getClient(item[0]);
|
String value = OpcUaUtils.readNode(opcUaClient, item[1]);
|
result.put(TagUtils.genTagId(DataSourceType.OPCUA.getCode(), deviceMap.get(item[0]).getServerName(), item[1]), value);
|
} catch (Exception ex) {
|
ex.printStackTrace();
|
result.put(TagUtils.genTagId(DataSourceType.OPCUA.getCode(), deviceMap.get(item[0]).getServerName(), item[1]), CommonConstant.BAD_VALUE);
|
}
|
});
|
return result;
|
}
|
}
|