Skip to content

Commit

Permalink
~ sendRequest / sendRequestAsync refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinherron committed May 17, 2023
1 parent ffbf9ef commit 0aa598a
Show file tree
Hide file tree
Showing 20 changed files with 101 additions and 87 deletions.
Expand Up @@ -36,8 +36,7 @@ public static void main(String[] args) throws Exception {

@Override
public void run(OpcUaClient client, CompletableFuture<OpcUaClient> future) throws Exception {
// synchronous connect
client.connectAsync().get();
client.connect();

// start browsing at root folder
UaNode rootNode = client.getAddressSpace().getNode(NodeIds.RootFolder);
Expand Down Expand Up @@ -72,9 +71,7 @@ private CompletableFuture<Void> browseRecursive(OpcUaClient client, Tree<UaNode>
}

private static String indent(int depth) {
StringBuilder s = new StringBuilder();
s.append(" ".repeat(Math.max(0, depth)));
return s.toString();
return " ".repeat(Math.max(0, depth));
}

private static <T> void traverse(Tree<T> tree, int depth, BiConsumer<Integer, T> consumer) {
Expand Down
Expand Up @@ -12,10 +12,10 @@

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

import org.eclipse.milo.opcua.sdk.client.OpcUaClient;
import org.eclipse.milo.opcua.stack.core.NodeIds;
import org.eclipse.milo.opcua.stack.core.UaException;
import org.eclipse.milo.opcua.stack.core.types.builtin.NodeId;
import org.eclipse.milo.opcua.stack.core.types.enumerated.BrowseDirection;
import org.eclipse.milo.opcua.stack.core.types.enumerated.BrowseResultMask;
Expand All @@ -40,8 +40,7 @@ public static void main(String[] args) throws Exception {

@Override
public void run(OpcUaClient client, CompletableFuture<OpcUaClient> future) throws Exception {
// synchronous connect
client.connectAsync().get();
client.connect();

// start browsing at root folder
browseNode("", client, NodeIds.RootFolder);
Expand All @@ -60,7 +59,7 @@ private void browseNode(String indent, OpcUaClient client, NodeId browseRoot) {
);

try {
BrowseResult browseResult = client.browseAsync(browse).get();
BrowseResult browseResult = client.browse(browse);

List<ReferenceDescription> references = List.of(browseResult.getReferences());

Expand All @@ -71,7 +70,7 @@ private void browseNode(String indent, OpcUaClient client, NodeId browseRoot) {
rd.getNodeId().toNodeId(client.getNamespaceTable())
.ifPresent(nodeId -> browseNode(indent + " ", client, nodeId));
}
} catch (InterruptedException | ExecutionException e) {
} catch (UaException e) {
logger.error("Browsing nodeId={} failed: {}", browseRoot, e.getMessage(), e);
}
}
Expand Down
Expand Up @@ -33,8 +33,7 @@ public static void main(String[] args) throws Exception {

@Override
public void run(OpcUaClient client, CompletableFuture<OpcUaClient> future) throws Exception {
// synchronous connect
client.connectAsync().get();
client.connect();

// start browsing at root folder
browseNode("", client, NodeIds.RootFolder);
Expand Down
Expand Up @@ -50,8 +50,7 @@ public static void main(String[] args) throws Exception {

@Override
public void run(OpcUaClient client, CompletableFuture<OpcUaClient> future) throws Exception {
// synchronous connect
client.connectAsync().get();
client.connect();

// create a subscription and a monitored item
UaSubscription subscription = client.getSubscriptionManager()
Expand Down
Expand Up @@ -41,7 +41,7 @@ public static void main(String[] args) throws Exception {

@Override
public void run(OpcUaClient client, CompletableFuture<OpcUaClient> future) throws Exception {
client.connectAsync().get();
client.connect();

HistoryReadDetails historyReadDetails = new ReadRawModifiedDetails(
false,
Expand All @@ -61,13 +61,12 @@ public void run(OpcUaClient client, CompletableFuture<OpcUaClient> future) throw
List<HistoryReadValueId> nodesToRead = new ArrayList<>();
nodesToRead.add(historyReadValueId);

HistoryReadResponse historyReadResponse = client.historyReadAsync(
HistoryReadResponse historyReadResponse = client.historyRead(
historyReadDetails,
TimestampsToReturn.Both,
false,
nodesToRead
).get();

);

HistoryReadResult[] historyReadResults = historyReadResponse.getResults();

Expand Down
Expand Up @@ -32,7 +32,7 @@ public static void main(String[] args) throws Exception {

@Override
public void run(OpcUaClient client, CompletableFuture<OpcUaClient> future) throws Exception {
client.connectAsync().get();
client.connect();

ManagedSubscription subscription = ManagedSubscription.create(client);

Expand Down
Expand Up @@ -37,7 +37,7 @@ public static void main(String[] args) throws Exception {

@Override
public void run(OpcUaClient client, CompletableFuture<OpcUaClient> future) throws Exception {
client.connectAsync().get();
client.connect();

final CountDownLatch eventLatch = new CountDownLatch(3);

Expand Down
Expand Up @@ -35,8 +35,7 @@ public static void main(String[] args) throws Exception {

@Override
public void run(OpcUaClient client, CompletableFuture<OpcUaClient> future) throws Exception {
// synchronous connect
client.connectAsync().get();
client.connect();

// call the sqrt(x) function
sqrt(client, 16.0).exceptionally(ex -> {
Expand Down
Expand Up @@ -38,8 +38,7 @@ public static void main(String[] args) throws Exception {

@Override
public void run(OpcUaClient client, CompletableFuture<OpcUaClient> future) throws Exception {
// synchronous connect
client.connectAsync().get();
client.connect();

UaObjectNode objectNode = client.getAddressSpace()
.getObjectNode(NodeId.parse("ns=2;s=HelloWorld"));
Expand Down
Expand Up @@ -35,8 +35,7 @@ public static void main(String[] args) throws Exception {

@Override
public void run(OpcUaClient client, CompletableFuture<OpcUaClient> future) throws Exception {
// synchronous connect
client.connectAsync().get();
client.connect();

// synchronous read request via VariableNode
UaVariableNode node = client.getAddressSpace().getVariableNode(NodeIds.Server_ServerStatus_StartTime);
Expand Down
Expand Up @@ -36,8 +36,7 @@ public static void main(String[] args) throws Exception {

@Override
public void run(OpcUaClient client, CompletableFuture<OpcUaClient> future) throws Exception {
// synchronous connect
client.connectAsync().get();
client.connect();

// Get a typed reference to the Server object: ServerNode
ServerTypeNode serverNode = (ServerTypeNode) client.getAddressSpace().getObjectNode(
Expand Down
Expand Up @@ -38,8 +38,7 @@ public static void main(String[] args) throws Exception {

@Override
public void run(OpcUaClient client, CompletableFuture<OpcUaClient> future) throws Exception {
// synchronous connect
client.connectAsync().get();
client.connect();

registerCustomCodec(client);

Expand Down
Expand Up @@ -43,8 +43,7 @@ public static void main(String[] args) throws Exception {

@Override
public void run(OpcUaClient client, CompletableFuture<OpcUaClient> future) throws Exception {
// synchronous connect
client.connectAsync().get();
client.connect();

// create a subscription @ 1000ms
UaSubscription subscription = client.getSubscriptionManager().createSubscription(1000.0).get();
Expand Down
Expand Up @@ -37,10 +37,9 @@ public static void main(String[] args) throws Exception {

@Override
public void run(OpcUaClient client, CompletableFuture<OpcUaClient> future) throws Exception {
// synchronous connect
client.connectAsync().get();
client.connect();

TranslateBrowsePathsToNodeIdsResponse response = client.translateBrowsePathsAsync(List.of(new BrowsePath(
TranslateBrowsePathsToNodeIdsResponse response = client.translateBrowsePaths(List.of(new BrowsePath(
NodeIds.ObjectsFolder,
new RelativePath(new RelativePathElement[]{
new RelativePathElement(
Expand All @@ -62,7 +61,7 @@ public void run(OpcUaClient client, CompletableFuture<OpcUaClient> future) throw
new QualifiedName(2, "UInt64")
)
})
))).get();
)));

BrowsePathResult result = List.of(response.getResults()).get(0);
StatusCode statusCode = result.getStatusCode();
Expand Down
Expand Up @@ -49,8 +49,7 @@ public static void main(String[] args) throws Exception {

@Override
public void run(OpcUaClient client, CompletableFuture<OpcUaClient> future) throws Exception {
// synchronous connect
client.connectAsync().get();
client.connect();

// create a subscription @ 1000ms
UaSubscription subscription = client.getSubscriptionManager()
Expand Down Expand Up @@ -80,10 +79,10 @@ public void run(OpcUaClient client, CompletableFuture<OpcUaClient> future) throw
subscription.addTriggeringLinks(reportingItem, List.of(samplingItem)).get();

// trigger reporting of both by writing to the static item and changing its value
client.writeValuesAsync(
client.writeValues(
List.of(new NodeId(2, "HelloWorld/ScalarTypes/Float")),
List.of(new DataValue(Variant.ofFloat(1.0f)))
).get();
);

// let the example run for 5 seconds then terminate
Thread.sleep(5000);
Expand Down
Expand Up @@ -50,7 +50,7 @@ public void run(OpcUaClient client, CompletableFuture<OpcUaClient> future) throw
// and dynamically generate codecs for custom structures.
client.addSessionInitializer(new BinaryDataTypeDictionarySessionInitializer(StructCodec::new));

client.connectAsync().get();
client.connect();

readPerson(client);
readWorkOrder(client);
Expand Down
Expand Up @@ -55,7 +55,7 @@ public void run(OpcUaClient client, CompletableFuture<OpcUaClient> future) throw
client.addSessionInitializer(new DataTypeTreeSessionInitializer());
client.addSessionInitializer(new DataTypeCodecSessionInitializer());

client.connectAsync().get();
client.connect();

readWriteReadPerson(client);
readWriteReadWorkOrder(client);
Expand Down
Expand Up @@ -33,8 +33,7 @@ public static void main(String[] args) throws Exception {

@Override
public void run(OpcUaClient client, CompletableFuture<OpcUaClient> future) throws Exception {
// synchronous connect
client.connectAsync().get();
client.connect();

List<NodeId> nodeIds = List.of(new NodeId(2, "HelloWorld/ScalarTypes/Int32"));

Expand All @@ -44,12 +43,8 @@ public void run(OpcUaClient client, CompletableFuture<OpcUaClient> future) throw
// don't write status or timestamps
DataValue dv = new DataValue(v, null, null);

// write asynchronously....
CompletableFuture<List<StatusCode>> f =
client.writeValuesAsync(nodeIds, List.of(dv));

// ...but block for the results so we write in order
List<StatusCode> statusCodes = f.get();
List<StatusCode> statusCodes = client.writeValues(nodeIds, List.of(dv));

StatusCode status = statusCodes.get(0);

if (status.isGood()) {
Expand Down
Expand Up @@ -43,7 +43,7 @@ public void startClientAndServer() throws Exception {
client.addSessionInitializer(new DataTypeCodecSessionInitializer());
client.addSessionInitializer(new BinaryDataTypeDictionarySessionInitializer(StructCodec::new));

client.connectAsync().get();
client.connect();
}

@AfterAll
Expand Down

0 comments on commit 0aa598a

Please sign in to comment.