use of org.eclipse.milo.opcua.stack.core.types.structured.ReadValueId in project milo by eclipse.
the class DataTypeDictionaryReader method readDataTypeDescriptionValues.
private CompletableFuture<List<String>> readDataTypeDescriptionValues(List<NodeId> nodeIds) {
CompletableFuture<UInteger> maxNodesPerRead = readNode(new ReadValueId(Identifiers.Server_ServerCapabilities_OperationLimits_MaxNodesPerRead, AttributeId.Value.uid(), null, QualifiedName.NULL_VALUE)).thenApply(dv -> (UInteger) dv.getValue().getValue());
CompletableFuture<Integer> getPartitionSize = maxNodesPerRead.thenApply(m -> Math.max(1, Ints.saturatedCast(m.longValue()))).exceptionally(ex -> PARTITION_SIZE);
return getPartitionSize.thenCompose(partitionSize -> {
List<List<NodeId>> partitions = Lists.partition(nodeIds, partitionSize);
CompletableFuture<List<List<DataValue>>> sequence = FutureUtils.sequence(partitions.stream().map(list -> {
List<ReadValueId> readValueIds = list.stream().map(nodeId -> new ReadValueId(nodeId, AttributeId.Value.uid(), null, QualifiedName.NULL_VALUE)).collect(Collectors.toList());
return readNodes(readValueIds);
}));
return sequence.thenApply(values -> values.stream().flatMap(List::stream).map(v -> (String) v.getValue().getValue()).collect(Collectors.toList()));
});
}
use of org.eclipse.milo.opcua.stack.core.types.structured.ReadValueId in project milo by eclipse.
the class SubscriptionExample method run.
@Override
public void run(OpcUaClient client, CompletableFuture<OpcUaClient> future) throws Exception {
// synchronous connect
client.connect().get();
// create a subscription @ 1000ms
UaSubscription subscription = client.getSubscriptionManager().createSubscription(1000.0).get();
// subscribe to the Value attribute of the server's CurrentTime node
ReadValueId readValueId = new ReadValueId(Identifiers.Server_ServerStatus_CurrentTime, AttributeId.Value.uid(), null, QualifiedName.NULL_VALUE);
// IMPORTANT: client handle must be unique per item within the context of a subscription.
// You are not required to use the UaSubscription's client handle sequence; it is provided as a convenience.
// Your application is free to assign client handles by whatever means necessary.
UInteger clientHandle = subscription.nextClientHandle();
MonitoringParameters parameters = new MonitoringParameters(clientHandle, // sampling interval
1000.0, // filter, null means use default
null, // queue size
uint(10), // discard oldest
true);
MonitoredItemCreateRequest request = new MonitoredItemCreateRequest(readValueId, MonitoringMode.Reporting, parameters);
// when creating items in MonitoringMode.Reporting this callback is where each item needs to have its
// value/event consumer hooked up. The alternative is to create the item in sampling mode, hook up the
// consumer after the creation call completes, and then change the mode for all items to reporting.
UaSubscription.ItemCreationCallback onItemCreated = (item, id) -> item.setValueConsumer(this::onSubscriptionValue);
List<UaMonitoredItem> items = subscription.createMonitoredItems(TimestampsToReturn.Both, newArrayList(request), onItemCreated).get();
for (UaMonitoredItem item : items) {
if (item.getStatusCode().isGood()) {
logger.info("item created for nodeId={}", item.getReadValueId().getNodeId());
} else {
logger.warn("failed to create item for nodeId={} (status={})", item.getReadValueId().getNodeId(), item.getStatusCode());
}
}
// let the example run for 5 seconds then terminate
Thread.sleep(5000);
future.complete(client);
}
use of org.eclipse.milo.opcua.stack.core.types.structured.ReadValueId in project milo by eclipse.
the class TriggeringExample method run.
@Override
public void run(OpcUaClient client, CompletableFuture<OpcUaClient> future) throws Exception {
// synchronous connect
client.connect().get();
// create a subscription @ 1000ms
UaSubscription subscription = client.getSubscriptionManager().createSubscription(1000.0).get();
// subscribe to a static value that reports
ReadValueId readValueId1 = new ReadValueId(new NodeId(2, "HelloWorld/ScalarTypes/Float"), AttributeId.Value.uid(), null, QualifiedName.NULL_VALUE);
UaMonitoredItem reportingItem = createMonitoredItem(subscription, readValueId1, MonitoringMode.Reporting);
// subscribe to a dynamic value that only samples
ReadValueId readValueId2 = new ReadValueId(Identifiers.Server_ServerStatus_CurrentTime, AttributeId.Value.uid(), null, QualifiedName.NULL_VALUE);
UaMonitoredItem samplingItem = createMonitoredItem(subscription, readValueId2, MonitoringMode.Sampling);
subscription.addTriggeringLinks(reportingItem, newArrayList(samplingItem)).get();
// trigger reporting of both by writing to the static item and changing its value
client.writeValue(new NodeId(2, "HelloWorld/ScalarTypes/Float"), new DataValue(new Variant(1.0f))).get();
// let the example run for 5 seconds then terminate
Thread.sleep(5000);
future.complete(client);
}
use of org.eclipse.milo.opcua.stack.core.types.structured.ReadValueId in project milo by eclipse.
the class EventSubscriptionExample method run.
@Override
public void run(OpcUaClient client, CompletableFuture<OpcUaClient> future) throws Exception {
// synchronous connect
client.connect().get();
// create a subscription and a monitored item
UaSubscription subscription = client.getSubscriptionManager().createSubscription(1000.0).get();
ReadValueId readValueId = new ReadValueId(Identifiers.Server, AttributeId.EventNotifier.uid(), null, QualifiedName.NULL_VALUE);
// client handle must be unique per item
UInteger clientHandle = uint(clientHandles.getAndIncrement());
EventFilter eventFilter = new EventFilter(new SimpleAttributeOperand[] { new SimpleAttributeOperand(Identifiers.BaseEventType, new QualifiedName[] { new QualifiedName(0, "EventId") }, AttributeId.Value.uid(), null), new SimpleAttributeOperand(Identifiers.BaseEventType, new QualifiedName[] { new QualifiedName(0, "EventType") }, AttributeId.Value.uid(), null), new SimpleAttributeOperand(Identifiers.BaseEventType, new QualifiedName[] { new QualifiedName(0, "Severity") }, AttributeId.Value.uid(), null), new SimpleAttributeOperand(Identifiers.BaseEventType, new QualifiedName[] { new QualifiedName(0, "Time") }, AttributeId.Value.uid(), null), new SimpleAttributeOperand(Identifiers.BaseEventType, new QualifiedName[] { new QualifiedName(0, "Message") }, AttributeId.Value.uid(), null) }, new ContentFilter(null));
MonitoringParameters parameters = new MonitoringParameters(clientHandle, 0.0, ExtensionObject.encode(client.getStaticSerializationContext(), eventFilter), uint(10), true);
MonitoredItemCreateRequest request = new MonitoredItemCreateRequest(readValueId, MonitoringMode.Reporting, parameters);
List<UaMonitoredItem> items = subscription.createMonitoredItems(TimestampsToReturn.Both, newArrayList(request)).get();
// do something with the value updates
UaMonitoredItem monitoredItem = items.get(0);
final AtomicInteger eventCount = new AtomicInteger(0);
monitoredItem.setEventConsumer((item, vs) -> {
logger.info("Event Received from {}", item.getReadValueId().getNodeId());
for (int i = 0; i < vs.length; i++) {
logger.info("\tvariant[{}]: {}", i, vs[i].getValue());
}
if (eventCount.incrementAndGet() == 3) {
future.complete(client);
}
});
}
use of org.eclipse.milo.opcua.stack.core.types.structured.ReadValueId in project milo by eclipse.
the class OpcUaClient method readNamespaceTableAsync.
/**
* Read the server's NamespaceTable and update the local copy.
* <p>
* This call completes asynchronously.
*
* @return a {@link CompletableFuture} that completes successfully with the updated
* {@link NamespaceTable} or completes exceptionally if a service- or operation-level error
* occurs.
*/
public CompletableFuture<NamespaceTable> readNamespaceTableAsync() {
return getSession().thenCompose(session -> {
RequestHeader requestHeader = newRequestHeader(session.getAuthenticationToken());
ReadRequest readRequest = new ReadRequest(requestHeader, 0.0, TimestampsToReturn.Neither, new ReadValueId[] { new ReadValueId(Identifiers.Server_NamespaceArray, AttributeId.Value.uid(), null, QualifiedName.NULL_VALUE) });
CompletableFuture<String[]> namespaceArray = sendRequest(readRequest).thenApply(ReadResponse.class::cast).thenApply(response -> Objects.requireNonNull(response.getResults())).thenApply(results -> (String[]) results[0].getValue().getValue());
return namespaceArray.thenAccept(this::updateNamespaceTable).thenApply(v -> getNamespaceTable());
});
}
Aggregations