use of de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetValueProvider in project FAAAST-Service by FraunhoferIOSB.
the class RequestHandlerManagerTest method testReadValueFromAssetConnectionAndUpdatePersistence.
@Test
public void testReadValueFromAssetConnectionAndUpdatePersistence() throws AssetConnectionException, ResourceNotFoundException {
RequestHandler requestHandler = new DeleteSubmodelByIdRequestHandler(persistence, messageBus, assetConnectionManager);
Reference parentRef = new DefaultReference.Builder().key(new DefaultKey.Builder().value("sub").idType(KeyType.IRI).type(KeyElements.SUBMODEL).build()).build();
SubmodelElement prop1 = new DefaultProperty.Builder().idShort("prop1").value("test").valueType("string").build();
SubmodelElement range = new DefaultRange.Builder().idShort("range1").max("1.0").min("0").valueType("double").build();
SubmodelElement prop2 = new DefaultProperty.Builder().idShort("prop2").value("test").valueType("string").build();
SubmodelElementCollection collection = new DefaultSubmodelElementCollection.Builder().idShort("col1").value(prop2).build();
SubmodelElement prop1_new = new DefaultProperty.Builder().idShort("prop1").value("testNew").valueType("string").build();
SubmodelElement range_new = new DefaultRange.Builder().idShort("range1").max("1.0").min("0").valueType("double").build();
SubmodelElement prop2_new = new DefaultProperty.Builder().idShort("prop2").value("testNew").valueType("string").build();
List<SubmodelElement> submodelElements = List.of(prop1, range, collection);
AssetValueProvider provider_prop1 = mock(AssetValueProvider.class);
AssetValueProvider provider_prop2 = mock(AssetValueProvider.class);
AssetValueProvider provider_range1 = mock(AssetValueProvider.class);
when(assetConnectionManager.hasValueProvider(any())).thenReturn(true);
// mock value prop1
when(assetConnectionManager.getValueProvider(AasUtils.toReference(parentRef, prop1))).thenReturn(provider_prop1);
when(provider_prop1.getValue()).thenReturn(ElementValueMapper.toValue(prop1_new));
// mock value prop2
when(assetConnectionManager.getValueProvider(AasUtils.toReference(AasUtils.toReference(parentRef, collection), prop2))).thenReturn(provider_prop2);
when(provider_prop2.getValue()).thenReturn(ElementValueMapper.toValue(prop2_new));
// mock value range
when(assetConnectionManager.getValueProvider(AasUtils.toReference(parentRef, range))).thenReturn(provider_range1);
when(provider_range1.getValue()).thenReturn(ElementValueMapper.toValue(range_new));
requestHandler.readValueFromAssetConnectionAndUpdatePersistence(parentRef, submodelElements);
verify(persistence).put(null, AasUtils.toReference(parentRef, prop1), prop1_new);
verify(persistence).put(null, AasUtils.toReference(AasUtils.toReference(parentRef, collection), prop2), prop2_new);
Assert.assertEquals(prop1_new, prop1);
Assert.assertEquals(prop2_new, prop2);
Assert.assertEquals(range_new, range);
}
use of de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetValueProvider in project FAAAST-Service by FraunhoferIOSB.
the class RequestHandlerManagerTest method createRequestHandlerManager.
@Before
public void createRequestHandlerManager() {
environment = AASFull.createEnvironment();
coreConfig = CoreConfig.builder().build();
messageBus = mock(MessageBus.class);
persistence = mock(Persistence.class);
assetConnectionManager = mock(AssetConnectionManager.class);
manager = new RequestHandlerManager(coreConfig, persistence, messageBus, assetConnectionManager);
assetValueProvider = mock(AssetValueProvider.class);
when(assetConnectionManager.getValueProvider(any())).thenReturn(assetValueProvider);
}
use of de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetValueProvider 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);
}
}
});
}
use of de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetValueProvider in project FAAAST-Service by FraunhoferIOSB.
the class RequestHandler method writeValueToAssetConnection.
/**
* Write the given Value to an existing AssetConnection of a Reference
* otherwise does nothing
*
* @param reference of the element to check for an AssetConnection
* @param value which will be written to the AssetConnection
*/
public void writeValueToAssetConnection(Reference reference, ElementValue value) {
try {
if (this.assetConnectionManager.hasValueProvider(reference)) {
if (DataElementValue.class.isAssignableFrom(value.getClass())) {
AssetValueProvider assetValueProvider = this.assetConnectionManager.getValueProvider(reference);
assetValueProvider.setValue((DataElementValue) value);
}
}
} catch (AssetConnectionException e) {
// TODO:
e.printStackTrace();
}
}
Aggregations