use of de.fraunhofer.iosb.ilt.faaast.service.model.value.DataElementValue in project FAAAST-Service by FraunhoferIOSB.
the class RequestHandlerManagerTest method testReadValueFromAssetConnection.
@Test
public void testReadValueFromAssetConnection() throws AssetConnectionException {
RequestHandler requestHandler = new DeleteSubmodelByIdRequestHandler(persistence, messageBus, assetConnectionManager);
PropertyValue expected = new PropertyValue.Builder().value(new StringValue("test")).build();
when(assetConnectionManager.hasValueProvider(any())).thenReturn(true);
when(assetValueProvider.getValue()).thenReturn(expected);
DataElementValue actual = requestHandler.readDataElementValueFromAssetConnection(new DefaultReference());
Assert.assertEquals(expected, actual);
}
use of de.fraunhofer.iosb.ilt.faaast.service.model.value.DataElementValue in project FAAAST-Service by FraunhoferIOSB.
the class OpcUaAssetConnectionTest method testWriteReadValue.
private void testWriteReadValue(String nodeId, PropertyValue expected) throws AssetConnectionException, InterruptedException {
Reference reference = AasUtils.parseReference("(Property)[ID_SHORT]Temperature");
ServiceContext serviceContext = mock(ServiceContext.class);
doReturn(ElementValueTypeInfo.builder().type(expected.getClass()).datatype(expected.getValue().getDataType()).build()).when(serviceContext).getTypeInfo(reference);
OpcUaAssetConnection connection = new OpcUaAssetConnection(CoreConfig.builder().build(), OpcUaAssetConnectionConfig.builder().valueProvider(reference, OpcUaValueProviderConfig.builder().nodeId(nodeId).build()).host(serverUrl).build(), serviceContext);
connection.getValueProviders().get(reference).setValue(expected);
DataElementValue actual = connection.getValueProviders().get(reference).getValue();
connection.close();
Assert.assertEquals(expected, actual);
}
use of de.fraunhofer.iosb.ilt.faaast.service.model.value.DataElementValue in project FAAAST-Service by FraunhoferIOSB.
the class AasServiceNodeManager method setRelationshipValue.
/**
* Sets the values for the given RelationshipElement.
*
* @param aasElement The desired RelationshipElement.
* @param value The new value.
* @throws StatusException If the operation fails
*/
private void setRelationshipValue(AASRelationshipElementType aasElement, RelationshipElementValue value) throws StatusException {
if (aasElement == null) {
throw new IllegalArgumentException("aasElement is null");
} else if (value == null) {
throw new IllegalArgumentException("value is null");
}
try {
Reference ref = new DefaultReference.Builder().keys(value.getFirst()).build();
setAasReferenceData(ref, aasElement.getFirstNode(), false);
ref = new DefaultReference.Builder().keys(value.getSecond()).build();
setAasReferenceData(ref, aasElement.getSecondNode(), false);
if ((aasElement instanceof AASAnnotatedRelationshipElementType) && (value instanceof AnnotatedRelationshipElementValue)) {
AASAnnotatedRelationshipElementType annotatedElement = (AASAnnotatedRelationshipElementType) aasElement;
AnnotatedRelationshipElementValue annotatedValue = (AnnotatedRelationshipElementValue) value;
UaNode[] annotationNodes = annotatedElement.getAnnotationNode().getComponents();
Map<String, DataElementValue> valueMap = annotatedValue.getAnnotations();
if (annotationNodes.length != valueMap.size()) {
logger.warn("Size of Value (" + valueMap.size() + ") doesn't match the number of AnnotationNodes (" + annotationNodes.length + ")");
throw new IllegalArgumentException("Size of Value doesn't match the number of AnnotationNodes");
}
// The Key of the Map is the IDShort of the DataElement (in our case the BrowseName)
for (UaNode annotationNode : annotationNodes) {
if (valueMap.containsKey(annotationNode.getBrowseName().getName())) {
setDataElementValue(annotationNode, valueMap.get(annotationNode.getBrowseName().getName()));
}
}
} else {
logger.info("setRelationshipValue: No AnnotatedRelationshipElement " + aasElement.getBrowseName().getName());
}
} catch (Throwable ex) {
logger.error("setAnnotatedRelationshipValue Exception", ex);
throw ex;
}
}
use of de.fraunhofer.iosb.ilt.faaast.service.model.value.DataElementValue 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.model.value.DataElementValue in project FAAAST-Service by FraunhoferIOSB.
the class OpcUaAssetConnection method registerValueProvider.
/**
* {@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
* @throws AssetConnectionException if nodeId could not be parsed
*/
@Override
public void registerValueProvider(Reference reference, OpcUaValueProviderConfig valueProvider) throws AssetConnectionException {
final String baseErrorMessage = "error registering value 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)));
}
final VariableNode node;
try {
node = client.getAddressSpace().getVariableNode(parseNodeId(valueProvider.getNodeId()));
} catch (UaException ex) {
throw new AssetConnectionException(String.format("%s - could not parse nodeId (nodeId: %s)", baseErrorMessage, valueProvider.getNodeId()), ex);
}
this.valueProviders.put(reference, new AssetValueProvider() {
@Override
public DataElementValue getValue() throws AssetConnectionException {
try {
DataValue dataValue = client.readValue(0, TimestampsToReturn.Neither, node.getNodeId()).get();
checkStatusCode(dataValue.getStatusCode(), "error reading value from asset conenction");
return new PropertyValue(valueConverter.convert(dataValue.getValue(), datatype));
} catch (AssetConnectionException | InterruptedException | ExecutionException e) {
throw new AssetConnectionException(String.format("error reading value from asset conenction (reference: %s)", AasUtils.asString(reference)), e);
}
}
@Override
public void setValue(DataElementValue value) throws AssetConnectionException {
if (value == null) {
throw new AssetConnectionException(String.format("error setting value on asset connection - value must be non-null (reference: %s)", AasUtils.asString(reference)));
}
if (!PropertyValue.class.isAssignableFrom(value.getClass())) {
throw new AssetConnectionException(String.format("error setting value on asset connection - unsupported element type (reference: %s, element type: %s)", AasUtils.asString(reference), value.getClass()));
}
try {
StatusCode result = client.writeValue(node.getNodeId(), new DataValue(valueConverter.convert(((PropertyValue) value).getValue(), node.getDataType()), null, null)).get();
checkStatusCode(result, "error setting value on asset connection");
} catch (InterruptedException | ExecutionException e) {
throw new AssetConnectionException("error writing asset connection value", e);
}
}
});
}
Aggregations