Search in sources :

Example 1 with AssetValueProvider

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);
}
Also used : DeleteSubmodelByIdRequestHandler(de.fraunhofer.iosb.ilt.faaast.service.request.handler.DeleteSubmodelByIdRequestHandler) SubmodelElement(io.adminshell.aas.v3.model.SubmodelElement) RequestHandler(de.fraunhofer.iosb.ilt.faaast.service.request.handler.RequestHandler) DeleteSubmodelByIdRequestHandler(de.fraunhofer.iosb.ilt.faaast.service.request.handler.DeleteSubmodelByIdRequestHandler) DefaultRange(io.adminshell.aas.v3.model.impl.DefaultRange) Reference(io.adminshell.aas.v3.model.Reference) DefaultReference(io.adminshell.aas.v3.model.impl.DefaultReference) AtomicReference(java.util.concurrent.atomic.AtomicReference) DefaultSubmodelElementCollection(io.adminshell.aas.v3.model.impl.DefaultSubmodelElementCollection) SubmodelElementCollection(io.adminshell.aas.v3.model.SubmodelElementCollection) DefaultSubmodelElementCollection(io.adminshell.aas.v3.model.impl.DefaultSubmodelElementCollection) AssetValueProvider(de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetValueProvider) Test(org.junit.Test)

Example 2 with AssetValueProvider

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);
}
Also used : Persistence(de.fraunhofer.iosb.ilt.faaast.service.persistence.Persistence) MessageBus(de.fraunhofer.iosb.ilt.faaast.service.messagebus.MessageBus) AssetConnectionManager(de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetConnectionManager) RequestHandlerManager(de.fraunhofer.iosb.ilt.faaast.service.request.RequestHandlerManager) AssetValueProvider(de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetValueProvider) Before(org.junit.Before)

Example 3 with 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);
            }
        }
    });
}
Also used : DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) UaException(org.eclipse.milo.opcua.stack.core.UaException) ElementValueTypeInfo(de.fraunhofer.iosb.ilt.faaast.service.typing.ElementValueTypeInfo) PropertyValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.PropertyValue) TypeInfo(de.fraunhofer.iosb.ilt.faaast.service.typing.TypeInfo) ElementValueTypeInfo(de.fraunhofer.iosb.ilt.faaast.service.typing.ElementValueTypeInfo) StatusCode(org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode) Datatype(de.fraunhofer.iosb.ilt.faaast.service.model.value.primitive.Datatype) VariableNode(org.eclipse.milo.opcua.sdk.core.nodes.VariableNode) AssetConnectionException(de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetConnectionException) AssetValueProvider(de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetValueProvider) DataElementValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.DataElementValue)

Example 4 with AssetValueProvider

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();
    }
}
Also used : AssetValueProvider(de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetValueProvider) AssetConnectionException(de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetConnectionException)

Aggregations

AssetValueProvider (de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetValueProvider)4 AssetConnectionException (de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetConnectionException)2 AssetConnectionManager (de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetConnectionManager)1 MessageBus (de.fraunhofer.iosb.ilt.faaast.service.messagebus.MessageBus)1 DataElementValue (de.fraunhofer.iosb.ilt.faaast.service.model.value.DataElementValue)1 PropertyValue (de.fraunhofer.iosb.ilt.faaast.service.model.value.PropertyValue)1 Datatype (de.fraunhofer.iosb.ilt.faaast.service.model.value.primitive.Datatype)1 Persistence (de.fraunhofer.iosb.ilt.faaast.service.persistence.Persistence)1 RequestHandlerManager (de.fraunhofer.iosb.ilt.faaast.service.request.RequestHandlerManager)1 DeleteSubmodelByIdRequestHandler (de.fraunhofer.iosb.ilt.faaast.service.request.handler.DeleteSubmodelByIdRequestHandler)1 RequestHandler (de.fraunhofer.iosb.ilt.faaast.service.request.handler.RequestHandler)1 ElementValueTypeInfo (de.fraunhofer.iosb.ilt.faaast.service.typing.ElementValueTypeInfo)1 TypeInfo (de.fraunhofer.iosb.ilt.faaast.service.typing.TypeInfo)1 Reference (io.adminshell.aas.v3.model.Reference)1 SubmodelElement (io.adminshell.aas.v3.model.SubmodelElement)1 SubmodelElementCollection (io.adminshell.aas.v3.model.SubmodelElementCollection)1 DefaultRange (io.adminshell.aas.v3.model.impl.DefaultRange)1 DefaultReference (io.adminshell.aas.v3.model.impl.DefaultReference)1 DefaultSubmodelElementCollection (io.adminshell.aas.v3.model.impl.DefaultSubmodelElementCollection)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1