|
@@ -4,14 +4,19 @@ 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.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.HistoryReadResponse;
|
|
|
+import org.eclipse.milo.opcua.stack.core.types.structured.HistoryReadResult;
|
|
|
+import org.eclipse.milo.opcua.stack.core.types.structured.HistoryReadValueId;
|
|
|
+import org.eclipse.milo.opcua.stack.core.types.structured.ReadRawModifiedDetails;
|
|
|
import org.openscada.opc.dcom.da.OPCSERVERSTATE;
|
|
|
import org.openscada.opc.lib.da.Item;
|
|
|
import org.openscada.opc.lib.da.*;
|
|
@@ -22,7 +27,6 @@ import org.springframework.stereotype.Component;
|
|
|
import javax.annotation.Resource;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.*;
|
|
|
-import java.util.concurrent.CompletableFuture;
|
|
|
import java.util.concurrent.Future;
|
|
|
|
|
|
import static com.judong.chuanyiserver.util.KepOpcServerUtil.getVal;
|
|
@@ -139,6 +143,7 @@ public class OpcAsyncTask {
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
+ redisUtil.convertAndSend(ConstantStr.ITEM_GROUP, Result.no(ResultEnum.SERVER_ERROR.getRespCode(), "组" + itemGroup.getGroupName() + "运行异常,错误信息为:" + e.getMessage()));
|
|
|
} finally {
|
|
|
server.dispose();
|
|
|
redisUtil.del(ConstantStr.ITEM_GROUP + id);
|
|
@@ -149,35 +154,53 @@ 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();
|
|
|
+ redisUtil.convertAndSend(ConstantStr.ITEM_GROUP, Result.no(ResultEnum.SERVER_ERROR.getRespCode(), "组" + itemGroup.getGroupName() + "运行异常,错误信息为:" + e.getMessage()));
|
|
|
} finally {
|
|
|
if (Blank.isNotEmpty(opcUaClient)) {
|
|
|
opcUaClient.disconnect();
|