-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReadExample.java
96 lines (77 loc) · 3.5 KB
/
ReadExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
* Copyright (c) 2016 Kevin Herron
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.html.
*/
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import com.google.common.collect.ImmutableList;
import org.eclipse.milo.examples.client.ClientExample;
import org.eclipse.milo.opcua.sdk.client.OpcUaClient;
import org.eclipse.milo.opcua.sdk.client.api.nodes.VariableNode;
import org.eclipse.milo.opcua.stack.core.Identifiers;
import org.eclipse.milo.opcua.stack.core.types.builtin.DataValue;
import org.eclipse.milo.opcua.stack.core.types.builtin.DateTime;
import org.eclipse.milo.opcua.stack.core.types.builtin.NodeId;
import org.eclipse.milo.opcua.stack.core.types.enumerated.ServerState;
import org.eclipse.milo.opcua.stack.core.types.enumerated.TimestampsToReturn;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ReadExample implements ClientExample {
public static void main(String[] args) throws Exception {
ReadExample example = new ReadExample();
new ClientExampleRunner(example).run();
}
private final Logger logger = LoggerFactory.getLogger(getClass());
@Override
public void run(OpcUaClient client, CompletableFuture<OpcUaClient> future) throws Exception {
// synchronous connect
client.connect().get();
// synchronous read request via VariableNode
VariableNode node = client.getAddressSpace().createVariableNode(Identifiers.Server_ServerStatus_StartTime);
DataValue value = node.readValue().get();
ArrayList<String> subscriptionList = CsvReader.getSubscriptions(null);
JSONObject json = new JSONObject();
subscriptionList.forEach((item)->{
NodeId nodeId = new NodeId(2, item);
VariableNode node2 = client.getAddressSpace().createVariableNode(nodeId);
DataValue value2 = null;
try {
value2 = node2.readValue().get();
} catch (InterruptedException | ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
json.put("id", item);
json.put("v", value2.getValue().getValue());
json.put("q", true);
DateTime dt = (DateTime)(value.getValue().getValue());
json.put("t", dt.getUtcTime());
System.out.println(json.toString());
});
// asynchronous read request
readServerStateAndTime(client).thenAccept(values -> {
DataValue v0 = values.get(0);
DataValue v1 = values.get(1);
logger.info("State={}", ServerState.from((Integer) v0.getValue().getValue()));
logger.info("CurrentTime={}", v1.getValue().getValue());
future.complete(client);
});
}
private CompletableFuture<List<DataValue>> readServerStateAndTime(OpcUaClient client) {
List<NodeId> nodeIds = ImmutableList.of(
Identifiers.Server_ServerStatus_State,
Identifiers.Server_ServerStatus_CurrentTime);
return client.readValues(0.0, TimestampsToReturn.Both, nodeIds);
}
}