use of de.fraunhofer.iosb.ilt.faaast.service.assetconnection.NewDataListener in project FAAAST-Service by FraunhoferIOSB.
the class OpcUaAssetConnectionTest method testSubscribe.
private void testSubscribe(String nodeId, PropertyValue expected) throws AssetConnectionException, InterruptedException, ExecutionException, UaException {
Reference reference = AasUtils.parseReference("(Property)[ID_SHORT]Temperature");
long interval = 1000;
ServiceContext serviceContext = mock(ServiceContext.class);
TypeInfo infoExample = ElementValueTypeInfo.builder().type(PropertyValue.class).datatype(expected.getValue().getDataType()).build();
doReturn(infoExample).when(serviceContext).getTypeInfo(reference);
OpcUaAssetConnection connection = new OpcUaAssetConnection();
connection.init(CoreConfig.builder().build(), OpcUaAssetConnectionConfig.builder().host(serverUrl).subscriptionProvider(reference, OpcUaSubscriptionProviderConfig.builder().nodeId(nodeId).interval(interval).build()).valueProvider(reference, OpcUaValueProviderConfig.builder().nodeId(nodeId).build()).build(), serviceContext);
final AtomicReference<DataElementValue> response = new AtomicReference<>();
CountDownLatch condition = new CountDownLatch(1);
connection.getSubscriptionProviders().get(reference).addNewDataListener(new NewDataListener() {
@Override
public void newDataReceived(DataElementValue data) {
response.set(data);
condition.countDown();
}
});
connection.getValueProviders().get(reference).setValue(expected);
long waitTime = 5 * interval;
TimeUnit waitTimeUnit = isDebugging() ? TimeUnit.SECONDS : TimeUnit.MILLISECONDS;
Assert.assertTrue(String.format("test failed because there was no response within defined time (%d %s)", waitTime, waitTimeUnit), condition.await(waitTime, waitTimeUnit));
Assert.assertEquals(expected, response.get());
}
use of de.fraunhofer.iosb.ilt.faaast.service.assetconnection.NewDataListener in project FAAAST-Service by FraunhoferIOSB.
the class OpcUaAssetConnection method registerSubscriptionProvider.
/**
* {@inheritdoc}
*
* @throws AssetConnectionException if reference does not point to a
* {@link io.adminshell.aas.v3.model.Property}
* @throws AssetConnectionException if referenced
* {@link io.adminshell.aas.v3.model.Property} does not have datatype
* defined
*/
@Override
public void registerSubscriptionProvider(Reference reference, OpcUaSubscriptionProviderConfig subscriptionProvider) throws AssetConnectionException {
final String baseErrorMessage = "error registering subscription provider";
TypeInfo typeInfo = serviceContext.getTypeInfo(reference);
if (typeInfo == null) {
throw new AssetConnectionException(String.format("%s - could not resolve type information (reference: %s)", baseErrorMessage, AasUtils.asString(reference)));
}
if (!ElementValueTypeInfo.class.isAssignableFrom(typeInfo.getClass())) {
throw new AssetConnectionException(String.format("%s - reference must point to element with value (reference: %s)", baseErrorMessage, AasUtils.asString(reference)));
}
ElementValueTypeInfo valueTypeInfo = (ElementValueTypeInfo) typeInfo;
if (!PropertyValue.class.isAssignableFrom(valueTypeInfo.getType())) {
throw new AssetConnectionException(String.format("%s - unsupported element type (reference: %s, element type: %s)", baseErrorMessage, AasUtils.asString(reference), valueTypeInfo.getType()));
}
final Datatype datatype = valueTypeInfo.getDatatype();
if (datatype == null) {
throw new AssetConnectionException(String.format("%s - missing datatype (reference: %s)", baseErrorMessage, AasUtils.asString(reference)));
}
this.subscriptionProviders.put(reference, new AssetSubscriptionProvider() {
@Override
public void addNewDataListener(NewDataListener listener) throws AssetConnectionException {
if (!subscriptions.containsKey(subscriptionProvider.getNodeId())) {
SubscriptionHandler handler = new SubscriptionHandler();
handler.datatype = datatype;
try {
handler.originalValue = client.readValue(0, TimestampsToReturn.Neither, client.getAddressSpace().getVariableNode(parseNodeId(subscriptionProvider.getNodeId())).getNodeId()).get();
} catch (UaException | InterruptedException | ExecutionException ex) {
logger.warn("{} - reading initial value of subscribed node failed (reference: {}, nodeId: {})", baseErrorMessage, AasUtils.asString(reference), subscriptionProvider.getNodeId());
}
try {
handler.dataItem = opcUaSubscription.createDataItem(parseNodeId(subscriptionProvider.getNodeId()), LambdaExceptionHelper.rethrowConsumer(x -> {
x.addDataValueListener(LambdaExceptionHelper.rethrowConsumer(v -> handler.notify(v)));
}));
} catch (UaException ex) {
logger.warn("{} - could not create subscrption item (reference: {}, nodeId: {})", baseErrorMessage, AasUtils.asString(reference), subscriptionProvider.getNodeId());
}
subscriptions.put(subscriptionProvider.getNodeId(), handler);
}
List<NewDataListener> listeners = subscriptions.get(subscriptionProvider.getNodeId()).listeners;
if (!listeners.contains(listener)) {
listeners.add(listener);
}
}
@Override
public void removeNewDataListener(NewDataListener listener) throws AssetConnectionException {
if (subscriptions.containsKey(subscriptionProvider.getNodeId())) {
SubscriptionHandler handler = subscriptions.get(subscriptionProvider.getNodeId());
if (handler.listeners.contains(listener)) {
handler.listeners.remove(listener);
}
if (handler.listeners.isEmpty()) {
try {
handler.dataItem.delete();
subscriptions.remove(subscriptionProvider.getNodeId());
} catch (UaException ex) {
throw new AssetConnectionException(String.format("%s - removing subscription failed (reference: %s, nodeId: %s)", baseErrorMessage, AasUtils.asString(reference), subscriptionProvider.getNodeId()), ex);
}
}
}
}
});
}
use of de.fraunhofer.iosb.ilt.faaast.service.assetconnection.NewDataListener in project FAAAST-Service by FraunhoferIOSB.
the class MqttAssetConnectionTest method testSubscriptionProvider.
public void testSubscriptionProvider(ContentFormat contentFormat, String message, String query, PropertyValue expected) throws AssetConnectionException, InterruptedException, ValueFormatException {
MqttAssetConnection assetConnection = newConnection(ElementValueTypeInfo.builder().datatype(expected.getValue().getDataType()).type(expected.getClass()).build(), MqttSubscriptionProviderConfig.builder().contentFormat(contentFormat).query(query).build());
CountDownLatch condition = new CountDownLatch(1);
final AtomicReference<DataElementValue> response = new AtomicReference<>();
assetConnection.getSubscriptionProviders().get(DEFAULT_REFERENCE).addNewDataListener(new NewDataListener() {
@Override
public void newDataReceived(DataElementValue data) {
response.set(data);
condition.countDown();
}
});
publishMqtt(DEFAULT_TOPIC, message);
condition.await(DEFAULT_TIMEOUT, isDebugging() ? TimeUnit.SECONDS : TimeUnit.MILLISECONDS);
Assert.assertEquals(expected, response.get());
}
Aggregations