Created
November 23, 2023 14:24
-
-
Save kevinherron/72b7ce12299f822fca0511320d171c31 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2019 the Eclipse Milo Authors | |
* | |
* This program and the accompanying materials are made | |
* available under the terms of the Eclipse Public License 2.0 | |
* which is available at https://www.eclipse.org/legal/epl-2.0/ | |
* | |
* SPDX-License-Identifier: EPL-2.0 | |
*/ | |
package org.eclipse.milo.examples.client; | |
import java.util.concurrent.CompletableFuture; | |
import org.eclipse.milo.opcua.sdk.client.OpcUaClient; | |
import org.eclipse.milo.opcua.sdk.client.subscriptions.ManagedDataItem; | |
import org.eclipse.milo.opcua.sdk.client.subscriptions.ManagedSubscription; | |
import org.eclipse.milo.opcua.stack.core.NodeIds; | |
import org.eclipse.milo.opcua.stack.core.UaException; | |
import org.eclipse.milo.opcua.stack.core.security.SecurityPolicy; | |
import org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
public class ManagedSubscriptionDataExample implements ClientExample { | |
public static void main(String[] args) throws Exception { | |
ManagedSubscriptionDataExample example = new ManagedSubscriptionDataExample(); | |
new ClientExampleRunner(example, false).run(); | |
} | |
private final Logger logger = LoggerFactory.getLogger(getClass()); | |
@Override | |
public void run(OpcUaClient client, CompletableFuture<OpcUaClient> future) throws Exception { | |
client.connect(); | |
createSubscriptionAndDataItem(client); | |
Thread.sleep(Integer.MAX_VALUE); | |
future.complete(client); | |
} | |
private void createSubscriptionAndDataItem(OpcUaClient client) throws UaException { | |
ManagedSubscription subscription = ManagedSubscription.create(client); | |
subscription.addDataChangeListener((items, values) -> { | |
for (int i = 0; i < items.size(); i++) { | |
logger.info( | |
"subscription value received: item={}, value={}", | |
items.get(i).getNodeId(), values.get(i).getValue() | |
); | |
} | |
}); | |
subscription.addStatusListener(new ManagedSubscription.StatusListener() { | |
@Override | |
public void onSubscriptionTransferFailed(ManagedSubscription subscription, StatusCode statusCode) { | |
try { | |
createSubscriptionAndDataItem(client); | |
} catch (UaException e) { | |
logger.error("Error creating new subscription", e); | |
} | |
} | |
}); | |
ManagedDataItem dataItem = subscription.createDataItem( | |
NodeIds.Server_ServerStatus_CurrentTime | |
); | |
if (dataItem.getStatusCode().isGood()) { | |
logger.info("item created for nodeId={}", dataItem.getNodeId()); | |
} else { | |
logger.warn( | |
"failed to create item for nodeId={} (status={})", | |
dataItem.getNodeId(), dataItem.getStatusCode() | |
); | |
throw new UaException(dataItem.getStatusCode()); | |
} | |
} | |
@Override | |
public String getEndpointUrl() { | |
return "opc.tcp://localhost:62541/milo"; | |
} | |
@Override | |
public SecurityPolicy getSecurityPolicy() { | |
return SecurityPolicy.None; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment