1

我有一个外部 OPC UA 服务器,我想从中读取数据。我使用用户名和密码认证,所以我的客户端初始化如下:

public class MyClient {

    // ...

    public MyClient() throws Exception {
        EndpointDescription[] endpoints =
            UaTcpStackClient.getEndpoints(OPCConstants.OPC_SERVER_URI).get();

        // using the first endpoint
        EndpointDescription endpoint = endpoints[0];

        // client configuration
        OpcUaClientConfig config = OpcUaClientConfig.builder()
            .setApplicationName(LocalizedText.english("Example Client"))
            .setApplicationUri(String.format("some:example-client:%s",
                    UUID.randomUUID()))
            .setIdentityProvider(new UsernameProvider(USERNAME, PWD))
            .setEndpoint(endpoint)
            .build();
    }
}

客户的要求如下:

public CompletableFuture<DataValue> getData(NodeId nodeId) {
    LOGGER.debug("Sending request");
    return client.readValue(60000000.0, TimestampsToReturn.Server, nodeId);
}

在初始化客户端并将其连接到服务器后,我从 main 方法调用此请求:

MyClient client = new MyClient();

NodeId requestedData = new NodeId(DATA_ID, DATA_KEY);
LOGGER.info("Sending synchronous TestStackRequest NodeId={}",
       requestedData);
client.connect();
DataValue response = client.getData(requestedData).get();
LOGGER.info("Received response value={}", response.getValue());
client.disconnect();

但是,此代码不起作用(尝试从服务器读取信息时会话已关闭)。我得到以下输出:

2018-04-12 17:43:27,765 调试 --- [ua-netty-event-loop-0] 回收器:-Dio.netty.recycler.maxCapacity.default:262144 2018-04-12
17:43:27,777 调试--- [ua-netty-event-loop-0] UaTcpClientAcknowledgeHandler : 在通道 = [id: 0xfd9519e3, L:/172.20.100.54:55805 - R:/172.20.100.135:4840] 上发送 Hello 消息。
2018-04-12 17:43:27,786 调试 --- [ua-netty-event-loop-0] UaTcpClientAcknowledgeHandler:在通道 = [id: 0xfd9519e3, L:/172.20.100.54:55805 - R:/ 172.20.100.135:4840]。
2018-04-12 17:43:27,793 调试 --- [ua-netty-event-loop-0] UaTcpClientMessageHandler:OpenSecureChannel 超时时间为 +5s
2018-04-12 17:43:27,946 调试 --- [ua-shared-pool-0] UaTcpClientMessageHandler:发送 OpenSecureChannelRequest(问题,id=0,currentToken=-1,previousToken=-1)。
2018-04-12 17:43:27,951 调试 --- [ua-netty-event-loop-0] UaTcpClientMessageHandler:OpenSecureChannel 超时取消 2018-04-12
17:43:27,961 调试 --- [ua-shared-pool -0] UaTcpClientMessageHandler:收到 OpenSecureChannelResponse。
2018-04-12 17:43:27,967 调试 --- [ua-shared-pool-0] UaTcpClientMessageHandler:SecureChannel id=1698234671,currentTokenId=1,previousTokenId=-1,lifetime=3600000ms,createdAt=DateTime{utcTime=131680285857690000 , javaDate=2018 年 4 月 12 日星期四 19:43:05 CEST}
2018-04-12 17:43:27,968 调试 --- [ua-netty-event-loop-0] UaTcpClientMessageHandler:0 条消息在握手完成之前排队;现在发送。 2018-04-12 17:43:28,001 调试 --- [ua-netty-event-loop-0] ClientChannelManager:断开连接完成,状态设置为空闲 2018-04-12 17:43:28,011 INFO --- [main ] OpcUaClient:Eclipse Milo OPC UA 堆栈版本:0.2.1 2018-04-12 17:43:28,011 INFO --- [main] OpcUaClient:Eclipse Milo OPC UA Client SDK 版本:0.2.1 2018-04-12 17: 43:28,189 INFO --- [main] CommunicationMain : 发送同步 TestStackRequest NodeId=NodeId{ns=6, id=::opcua:opcData.outGoing.basic.cycleStep} 2018-04-12 17:43:28,189 DEBUG -- - [main] ClientChannelManager : connect(), currentState=NotConnected
2018-04-12 17:43:27,968 调试 --- [ua-shared-pool-1] ClientChannelManager:通道引导成功:localAddress=/172.20.100.54:55805,remoteAddress=/172.20.100.135:4840
2018-04- 12 17:43:27,996 调试 --- [ua-shared-pool-0] ClientChannelManager : disconnect(), currentState=Connected
2018-04-12 17:43:27,997 调试 --- [ua-shared-pool-1 ] ClientChannelManager:发送 CloseSecureChannelRequest... 2018-04-12
17:43:28,000 DEBUG --- [ua-netty-event-loop-0] ClientChannelManager:channelInactive(),断开连接完成



2018-04-12 17: 43:28,056 DEBUG --- [main] OpcUaClient:添加了 ServiceFaultListener:org.eclipse.milo.opcua.sdk.client.session.SessionFsm$FaultListener@46d59067
2018-04-12 17:43:28,066 DEBUG --- [main ] OpcUaClient : 添加 SessionActivityListener: org.eclipse.milo.opcua.sdk.client.subscriptions.OpcUaSubscriptionManager$1@78452606


2018-04-12 17:43:28,190 调试 --- [main] ClientChannelManager : connect() while NotConnected
java.lang.Exception
at org.eclipse.milo.opcua.stack.client.ClientChannelManager.connect(ClientChannelManager.java: 67)
在 org.eclipse.milo.opcua.sdk.client.OpcUaClient.connect(OpcUaClient.java:313)
在 org.eclipse.milo.opcua.stack.client.UaTcpStackClient.connect(UaTcpStackClient.java:127) 在 com .mycompany.opcua.participants.CommunicationMain.testClient(CommunicationMain.java:69) at com.mycompany.opcua.participants.CommunicationMain.main(CommunicationMain.java:51) 2018-04-12 17:43:28,190 调试 --- [main] MyClient : 发送请求
在 com.mycompany.opcua.participants.MyClient.connect(MyClient.java:147)



2018-04-12 17:43:28,197 调试 --- [ua-netty-event-loop-1] UaTcpClientAcknowledgeHandler:在通道 = [id: 0xd9b3f832, L:/172.20.100.54:55806 - R:/ 上发送 Hello 消息172.20.100.135:4840]。 2018-04-12 17:43:28,205 调试 --- [ua-netty-event-loop-1] UaTcpClientMessageHandler:OpenSecureChannel 超时计划为 +5 秒 2018-04-12 17:43:28,205 调试 --- [ua- shared-pool-0] UaTcpClientMessageHandler :发送 OpenSecureChannelRequest(问题,id=0,currentToken=-1,previousToken=-1)。 2018-04-12 17:43:28,208 调试 --- [ua-netty-event-loop-1] UaTcpClientMessageHandler:OpenSecureChannel 超时取消 2018-04-12 17:43:28,208 调试 --- [ua-shared-pool -0] UaTcpClientMessageHandler:收到 OpenSecureChannelResponse。
2018-04-12 17:43:28,204 调试 --- [ua-netty-event-loop-1] UaTcpClientAcknowledgeHandler:在通道 = [id: 0xd9b3f832, L:/172.20.100.54:55806 - R:/ 上收到确认消息172.20.100.135:4840]。




2018-04-12 17:43:28,209 调试 --- [ua-shared-pool-0] UaTcpClientMessageHandler:SecureChannel id=1698234672,currentTokenId=1,previousTokenId=-1,lifetime=3600000ms,createdAt=DateTime{utcTime=131680285860260000 , javaDate=Thu Apr 12 19:43:06 CEST 2018} 2018-04-12
17:43:28,209 DEBUG --- [ua-netty-event-loop-1] UaTcpClientMessageHandler:0 条消息在握手完成前排队; 现在发送。
2018-04-12 17:43:28,209 调试 --- [ua-shared-pool-1] ClientChannelManager:通道引导成功:localAddress=/172.20.100.54:55806,remoteAddress=/172.20.100.135:4840
2018-04- 12 17:43:28,210 调试 --- [ua-shared-pool-0] SessionFsm : S(Inactive) x E(CreateSessionEvent) = S'(Creating)
线程“主”java.util.concurrent.ExecutionException 中的异常:UaException:状态=Bad_SessionClosed,消息=会话已被客户端关闭。
在 java.base/java.util.concurrent.CompletableFuture.reportGet(CompletableFuture.java:395)2018-04-12 17:43:28,212 DEBUG --- [ua-shared-pool-1] SessionFsm:发送 CreateSessionRequest.. .

在 java.base/java.util.concurrent.CompletableFuture.get(CompletableFuture.java:1999)
在 com.mycompany.opcua.participants.CommunicationMain.testClient(CommunicationMain.java:70)
在 com.mycompany.opcua.participants.CommunicationMain .main(CommunicationMain.java:51)
原因:UaException: status=Bad_SessionClosed, message=会话已被客户端关闭。
在 org.eclipse.milo.opcua.stack.core.util.FutureUtils.failedUaFuture(FutureUtils.java:100)
在 org.eclipse.milo.opcua.stack.core.util.FutureUtils.failedUaFuture(FutureUtils.java:88)
在 org.eclipse.milo.opcua.sdk.client.session.states.Inactive.(Inactive.java:28)
在 org.eclipse.milo.opcua.sdk.client.session.SessionFsm.(SessionFsm.java:69)
在 org.eclipse.milo.opcua.sdk.client.OpcUaClient.(OpcUaClient.java:159)2018-04-12 17:43:28,212 INFO --- [NonceUtilSecureRandom] NonceUtil : SecureRandom 在 0 毫秒内播种。

在 com.mycompany.opcua.participants.MyClient.(MyClient.java:112)
在 com.mycompany.opcua.participants.CommunicationMain.testClient(CommunicationMain.java:60)
... 还有 1 个

我使用 Eclipse milo 0.2.1 作为 OPC UA 库。

你能告诉我帽子会导致这个问题以及如何解决吗?这可能是与此相关的竞争条件吗?

我可以使用其他客户端(UaExpert)连接到同一台服务器。

先感谢您。

4

1 回答 1

0

您进行的所有调用(connect()disconnect()readValues())都是异步的,因此这里可能发生的情况是您在尝试读取时没有连接。

在进行下一步之前,请确保为这些示例阻止结果。(你不是在做这个connect()

于 2018-04-12T16:31:56.047 回答