use of org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger in project plc4x by apache.
the class Plc4xNamespace method addConfiguredNodes.
private void addConfiguredNodes(UaFolderNode rootNode, DeviceConfiguration c) {
final List<Tag> tags = c.getTags();
final String connectionString = c.getConnectionString();
for (int i = 0; i < tags.size(); i++) {
logger.info("Adding Tag " + tags.get(i).getAlias() + " - " + tags.get(i).getAddress());
String name = tags.get(i).getAlias();
final String tag = tags.get(i).getAddress();
Class datatype = null;
NodeId typeId = Identifiers.String;
UaVariableNode node = null;
Variant variant = null;
try {
datatype = plc4xServer.getField(tag, connectionString).getDefaultJavaType();
final int length = plc4xServer.getField(tag, connectionString).getNumberOfElements();
typeId = Plc4xCommunication.getNodeId(plc4xServer.getField(tag, connectionString).getPlcDataType());
if (length > 1) {
node = new UaVariableNode.UaVariableNodeBuilder(getNodeContext()).setNodeId(newNodeId(name)).setAccessLevel(AccessLevel.READ_WRITE).setUserAccessLevel(AccessLevel.READ_WRITE).setBrowseName(newQualifiedName(name)).setDisplayName(LocalizedText.english(name)).setDataType(typeId).setTypeDefinition(Identifiers.BaseDataVariableType).setValueRank(ValueRank.OneDimension.getValue()).setArrayDimensions(new UInteger[] { uint(length) }).build();
Object array = Array.newInstance(datatype, length);
for (int j = 0; j < length; j++) {
Array.set(array, j, false);
}
variant = new Variant(array);
} else {
node = new UaVariableNode.UaVariableNodeBuilder(getNodeContext()).setNodeId(newNodeId(name)).setAccessLevel(AccessLevel.READ_WRITE).setUserAccessLevel(AccessLevel.READ_WRITE).setBrowseName(newQualifiedName(name)).setDisplayName(LocalizedText.english(name)).setDataType(typeId).setTypeDefinition(Identifiers.BaseDataVariableType).build();
variant = new Variant(0);
}
node.setValue(new DataValue(variant));
node.getFilterChain().addLast(AttributeFilters.getValue(ctx -> plc4xServer.getValue(ctx, tag, connectionString)));
node.getFilterChain().addLast(AttributeFilters.setValue((ctx, value) -> {
if (length > 1) {
plc4xServer.setValue(tag, Arrays.toString((Object[]) value.getValue().getValue()), connectionString);
} else {
plc4xServer.setValue(tag, value.getValue().getValue().toString(), connectionString);
}
}));
} catch (PlcConnectionException e) {
logger.info("Couldn't find data type");
System.exit(1);
}
getNodeManager().addNode(node);
rootNode.addOrganizes(node);
}
}
use of org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger 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.builtin.unsigned.UInteger 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.builtin.unsigned.UInteger in project milo by eclipse.
the class SubscriptionWatchdogTimerTest method testSubscriptionWatchdogTimer.
@Disabled("run this test manually")
@Test
public void testSubscriptionWatchdogTimer() throws UaException, InterruptedException {
ManagedDataItem dataItem = subscription.createDataItem(Identifiers.Server_ServerStatus_State);
assertTrue(dataItem.getStatusCode().isGood());
CountDownLatch latch = new CountDownLatch(2);
subscription.addStatusListener(new ManagedSubscription.StatusListener() {
@Override
public void onSubscriptionWatchdogTimerElapsed(ManagedSubscription subscription) {
System.out.println("onWatchdogTimerElapsed() id=" + subscription.getSubscription().getSubscriptionId());
latch.countDown();
}
});
client.getSubscriptionManager().addSubscriptionListener(new SubscriptionListener() {
@Override
public void onSubscriptionWatchdogTimerElapsed(UaSubscription subscription) {
System.out.println("onWatchdogTimerElapsed() id=" + subscription.getSubscriptionId());
latch.countDown();
}
});
UInteger subscriptionId = subscription.getSubscription().getSubscriptionId();
server.getSubscriptions().get(subscriptionId).deleteSubscription();
System.out.printf("deleted subscription id=%s on server...%n", subscriptionId);
System.out.println("waiting for watchdog timer to elapse...");
assertTrue(latch.await(15, TimeUnit.SECONDS));
}
use of org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger 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);
}
});
}
Aggregations