|
@@ -4,14 +4,18 @@ import com.judong.chuanyiserver.dao.DataSourceDao;
|
|
|
import com.judong.chuanyiserver.dao.ItemGroupDao;
|
|
|
import com.judong.chuanyiserver.dao.RawDataDao;
|
|
|
import com.judong.chuanyiserver.entity.*;
|
|
|
+import com.judong.chuanyiserver.enums.DataSourceTypeEnum;
|
|
|
import com.judong.chuanyiserver.enums.ResultEnum;
|
|
|
import com.judong.chuanyiserver.exception.CustomException;
|
|
|
import com.judong.chuanyiserver.util.*;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import lombok.val;
|
|
|
import org.eclipse.milo.opcua.sdk.client.OpcUaClient;
|
|
|
+import org.eclipse.milo.opcua.sdk.client.OpcUaSession;
|
|
|
import org.eclipse.milo.opcua.stack.core.types.builtin.*;
|
|
|
+import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger;
|
|
|
import org.eclipse.milo.opcua.stack.core.types.enumerated.TimestampsToReturn;
|
|
|
+import org.eclipse.milo.opcua.stack.core.types.structured.*;
|
|
|
import org.openscada.opc.dcom.da.OPCSERVERSTATE;
|
|
|
import org.openscada.opc.lib.da.Item;
|
|
|
import org.openscada.opc.lib.da.*;
|
|
@@ -149,32 +153,49 @@ public class OpcAsyncTask {
|
|
|
//异步读取OpcUa
|
|
|
public void runOpcUa(ItemGroup itemGroup, DataSource dataSource) throws Exception {
|
|
|
Integer id = itemGroup.getId();
|
|
|
+ DataSourceType dataSourceType = dataSourceDao.getDataSourceTypeById(dataSource.getId());
|
|
|
OpcUaClient opcUaClient = OpcUaUtil.createClient(dataSource);
|
|
|
List<com.judong.chuanyiserver.entity.Item> itemList = itemGroupDao.getItemListByGroupId(id);
|
|
|
- List<NodeId> nodeIdList = OpcUaUtil.genNodeId(itemList);
|
|
|
try {
|
|
|
opcUaClient.connect().get();
|
|
|
- Boolean flage = true;
|
|
|
- while (flage) {
|
|
|
- List<RawData> rawDataList = new ArrayList<>();
|
|
|
- List<DataValue> valueList = opcUaClient.readValues(0.0, TimestampsToReturn.Neither, nodeIdList).get();
|
|
|
- Date date = new Date();
|
|
|
- for (DataValue dataValue : valueList) {
|
|
|
- StatusCode statusCode = dataValue.getStatusCode();
|
|
|
- if (Blank.isNotEmpty(statusCode)) {
|
|
|
- if (statusCode.isGood()) {
|
|
|
- Variant value = dataValue.getValue();
|
|
|
- ExpandedNodeId expandedNodeId = value.getDataType().get();
|
|
|
- rawDataList.add(new RawData(itemGroup.getDataSourceId(), expandedNodeId.getIdentifier().toString(), expandedNodeId.getType().toString(), value.getValue().toString(), date));
|
|
|
+ if (dataSourceType.getDataSourceTypeKey().equals(DataSourceTypeEnum.OPC_UA_REAL.getValue())) {
|
|
|
+ Boolean flage = true;
|
|
|
+ List<NodeId> nodeIdList = OpcUaUtil.genNodeId(itemList);
|
|
|
+ while (flage) {
|
|
|
+ List<RawData> rawDataList = new ArrayList<>();
|
|
|
+ List<DataValue> valueList = opcUaClient.readValues(0.0, TimestampsToReturn.Neither, nodeIdList).get();
|
|
|
+ Date date = new Date();
|
|
|
+ for (DataValue dataValue : valueList) {
|
|
|
+ StatusCode statusCode = dataValue.getStatusCode();
|
|
|
+ if (Blank.isNotEmpty(statusCode)) {
|
|
|
+ if (statusCode.isGood()) {
|
|
|
+ Variant value = dataValue.getValue();
|
|
|
+ ExpandedNodeId expandedNodeId = value.getDataType().get();
|
|
|
+ rawDataList.add(new RawData(itemGroup.getDataSourceId(), expandedNodeId.getIdentifier().toString(), expandedNodeId.getType().toString(), value.getValue().toString(), date));
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
+ addRawDataList(id, rawDataList);
|
|
|
+ Thread.sleep(itemGroup.getModeValue() * 1000);
|
|
|
+ flage = (Boolean) redisUtil.get(ConstantStr.ITEM_GROUP + id);
|
|
|
+ if (Blank.isEmpty(flage)) {
|
|
|
+ flage = false;
|
|
|
+ }
|
|
|
}
|
|
|
- addRawDataList(id, rawDataList);
|
|
|
- Thread.sleep(itemGroup.getModeValue() * 1000);
|
|
|
- flage = (Boolean) redisUtil.get(ConstantStr.ITEM_GROUP + id);
|
|
|
- if (Blank.isEmpty(flage)) {
|
|
|
- flage = false;
|
|
|
+ } else if (dataSourceType.getDataSourceTypeKey().equals(DataSourceTypeEnum.OPC_UA_HISTORY.getValue())) {
|
|
|
+ List<HistoryReadValueId> historyReadValueIdList = OpcUaUtil.genHisNodeId(itemList);
|
|
|
+ List<RawData> rawDataList = new ArrayList<>();
|
|
|
+ Date date = new Date();
|
|
|
+ HistoryReadResponse historyReadResponse = opcUaClient.historyRead(new ReadRawModifiedDetails(false, DateTime.MIN_VALUE, DateTime.MIN_VALUE, UInteger.MAX, false),
|
|
|
+ TimestampsToReturn.Both, true, historyReadValueIdList).get();
|
|
|
+ HistoryReadResult[] results = historyReadResponse.getResults();
|
|
|
+ for (HistoryReadResult historyReadResult : results) {
|
|
|
+ System.out.println(historyReadResult);
|
|
|
+// rawDataList.add(new RawData(itemGroup.getDataSourceId(), expandedNodeId.getIdentifier().toString(), expandedNodeId.getType().toString(), value.getValue().toString(), date));
|
|
|
}
|
|
|
+ addRawDataList(id, rawDataList);
|
|
|
+ } else {
|
|
|
+ throw new CustomException(ResultEnum.SERVER_ERROR.getRespCode(), "目前还没有此种类型的连接方式");
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|