use of org.eclipse.milo.opcua.sdk.client.subscriptions.ManagedSubscription in project milo by eclipse.
the class ManagedSubscriptionDataExample method run.
@Override
public void run(OpcUaClient client, CompletableFuture<OpcUaClient> future) throws Exception {
client.connect().get();
ManagedSubscription subscription = ManagedSubscription.create(client);
subscription.addDataChangeListener((items, values) -> {
for (int i = 0; i < items.size(); i++) {
logger.info("subscription value received: item={}, value={}", items.get(i).getNodeId(), values.get(i).getValue());
}
});
ManagedDataItem dataItem = subscription.createDataItem(Identifiers.Server_ServerStatus_CurrentTime);
if (dataItem.getStatusCode().isGood()) {
logger.info("item created for nodeId={}", dataItem.getNodeId());
// let the example run for 5 seconds before completing
Thread.sleep(5000);
dataItem.delete();
} else {
logger.warn("failed to create item for nodeId={} (status={})", dataItem.getNodeId(), dataItem.getStatusCode());
}
future.complete(client);
}
use of org.eclipse.milo.opcua.sdk.client.subscriptions.ManagedSubscription in project milo by eclipse.
the class UaMethodTest method findMethod.
@Test
public void findMethod() throws UaException {
ManagedSubscription subscription = ManagedSubscription.create(client);
ManagedDataItem dataItem = subscription.createDataItem(Identifiers.Server_ServerStatus_CurrentTime);
AddressSpace addressSpace = client.getAddressSpace();
UaObjectNode serverNode = addressSpace.getObjectNode(Identifiers.Server);
UaMethod getMonitoredItems = serverNode.getMethod("GetMonitoredItems");
assertNotNull(getMonitoredItems);
Argument[] inputArguments = getMonitoredItems.getInputArguments();
Argument[] outputArguments = getMonitoredItems.getOutputArguments();
assertEquals(1, inputArguments.length);
assertEquals("SubscriptionId", inputArguments[0].getName());
assertEquals(2, outputArguments.length);
assertEquals("ServerHandles", outputArguments[0].getName());
assertEquals("ClientHandles", outputArguments[1].getName());
Variant[] outputs = getMonitoredItems.call(new Variant[] { new Variant(subscription.getSubscription().getSubscriptionId()) });
UInteger[] expected0 = { dataItem.getMonitoredItem().getMonitoredItemId() };
UInteger[] expected1 = { dataItem.getMonitoredItem().getClientHandle() };
assertArrayEquals(expected0, (UInteger[]) outputs[0].getValue());
assertArrayEquals(expected1, (UInteger[]) outputs[1].getValue());
}
use of org.eclipse.milo.opcua.sdk.client.subscriptions.ManagedSubscription in project milo by eclipse.
the class UaMethodTest method callMethod.
@Test
public void callMethod() throws UaException {
ManagedSubscription subscription = ManagedSubscription.create(client);
ManagedDataItem dataItem = subscription.createDataItem(Identifiers.Server_ServerStatus_CurrentTime);
AddressSpace addressSpace = client.getAddressSpace();
UaObjectNode serverNode = addressSpace.getObjectNode(Identifiers.Server);
Variant[] outputs = serverNode.callMethod("GetMonitoredItems", new Variant[] { new Variant(subscription.getSubscription().getSubscriptionId()) });
UInteger[] expected0 = { dataItem.getMonitoredItem().getMonitoredItemId() };
UInteger[] expected1 = { dataItem.getMonitoredItem().getClientHandle() };
assertArrayEquals(expected0, (UInteger[]) outputs[0].getValue());
assertArrayEquals(expected1, (UInteger[]) outputs[1].getValue());
}
use of org.eclipse.milo.opcua.sdk.client.subscriptions.ManagedSubscription in project milo by eclipse.
the class ManagedSubscriptionEventExample method run.
@Override
public void run(OpcUaClient client, CompletableFuture<OpcUaClient> future) throws Exception {
client.connect().get();
final CountDownLatch eventLatch = new CountDownLatch(3);
ManagedSubscription subscription = ManagedSubscription.create(client);
subscription.addEventChangeListener((eventItems, variants) -> {
for (int i = 0; i < eventItems.size(); i++) {
ManagedEventItem eventItem = eventItems.get(i);
Variant[] eventFieldValues = variants.get(i);
logger.info("Event Received from {}", eventItem.getNodeId());
for (int j = 0; j < eventFieldValues.length; j++) {
logger.info("\tvariant[{}]: {}", j, eventFieldValues[j].getValue());
}
}
eventLatch.countDown();
});
EventFilter eventFilter = new EventFilterBuilder().select(Identifiers.BaseEventType, new QualifiedName(0, "EventId")).select(Identifiers.BaseEventType, new QualifiedName(0, "EventType")).select(Identifiers.BaseEventType, new QualifiedName(0, "Severity")).select(Identifiers.BaseEventType, new QualifiedName(0, "Time")).select(Identifiers.BaseEventType, new QualifiedName(0, "Message")).build();
ManagedEventItem eventItem = subscription.createEventItem(Identifiers.Server, eventFilter);
// wait for some events to arrive before completing
eventLatch.await();
eventItem.delete();
future.complete(client);
}
Aggregations