Skip to content

Instantly share code, notes, and snippets.

@kevinherron
Created July 28, 2017 14:45
Show Gist options
  • Save kevinherron/5313f6f58861a11f58c419ca88e59a11 to your computer and use it in GitHub Desktop.
Save kevinherron/5313f6f58861a11f58c419ca88e59a11 to your computer and use it in GitHub Desktop.
HistoryReadExample
package org.eclipse.milo.examples.client;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.eclipse.milo.opcua.sdk.client.OpcUaClient;
import org.eclipse.milo.opcua.sdk.client.api.config.OpcUaClientConfig;
import org.eclipse.milo.opcua.sdk.client.api.identity.AnonymousProvider;
import org.eclipse.milo.opcua.stack.client.UaTcpStackClient;
import org.eclipse.milo.opcua.stack.core.security.SecurityPolicy;
import org.eclipse.milo.opcua.stack.core.types.builtin.ByteString;
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.LocalizedText;
import org.eclipse.milo.opcua.stack.core.types.builtin.NodeId;
import org.eclipse.milo.opcua.stack.core.types.builtin.QualifiedName;
import org.eclipse.milo.opcua.stack.core.types.enumerated.TimestampsToReturn;
import org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription;
import org.eclipse.milo.opcua.stack.core.types.structured.HistoryData;
import org.eclipse.milo.opcua.stack.core.types.structured.HistoryModifiedData;
import org.eclipse.milo.opcua.stack.core.types.structured.HistoryReadDetails;
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.ModificationInfo;
import org.eclipse.milo.opcua.stack.core.types.structured.ReadRawModifiedDetails;
import static org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint;
public class HistoryReadExample {
public static void main(String[] args) throws Exception {
OpcUaClient client = createClient();
HistoryReadDetails historyReadDetails = new ReadRawModifiedDetails(
false,
DateTime.MIN_VALUE,
DateTime.now(),
uint(0),
true
);
HistoryReadValueId historyReadValueId = new HistoryReadValueId(
new NodeId(5, "Counter1"),
null,
QualifiedName.NULL_VALUE,
ByteString.NULL_VALUE
);
List<HistoryReadValueId> nodesToRead = new ArrayList<>();
nodesToRead.add(historyReadValueId);
HistoryReadResponse historyReadResponse = client.historyRead(
historyReadDetails,
TimestampsToReturn.Both,
false,
nodesToRead
).get();
HistoryReadResult historyReadResult = historyReadResponse.getResults()[0];
HistoryData historyData = historyReadResult.getHistoryData().decode();
DataValue[] dataValues = historyData.getDataValues();
for (int i = 0; i < dataValues.length; i++) {
DataValue value = dataValues[i];
System.out.println("value=" + value);
}
}
private static OpcUaClient createClient() throws Exception {
EndpointDescription[] endpoints = UaTcpStackClient.getEndpoints(
"opc.tcp://localhost:53530/OPCUA/SimulationServer").get();
EndpointDescription endpoint = Arrays.stream(endpoints)
.filter(e -> e.getSecurityPolicyUri().equals(SecurityPolicy.None.getSecurityPolicyUri()))
.findFirst().orElseThrow(() -> new Exception("no desired endpoints returned"));
OpcUaClientConfig config = OpcUaClientConfig.builder()
.setApplicationName(LocalizedText.english("eclipse milo opc-ua client"))
.setApplicationUri("urn:eclipse:milo:examples:client")
.setEndpoint(endpoint)
.setIdentityProvider(new AnonymousProvider())
.setRequestTimeout(uint(5000))
.build();
return new OpcUaClient(config);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment