Search in sources :

Example 1 with DataElementValue

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);
}
Also used : DeleteSubmodelByIdRequestHandler(de.fraunhofer.iosb.ilt.faaast.service.request.handler.DeleteSubmodelByIdRequestHandler) RequestHandler(de.fraunhofer.iosb.ilt.faaast.service.request.handler.RequestHandler) DeleteSubmodelByIdRequestHandler(de.fraunhofer.iosb.ilt.faaast.service.request.handler.DeleteSubmodelByIdRequestHandler) PropertyValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.PropertyValue) StringValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.primitive.StringValue) DefaultReference(io.adminshell.aas.v3.model.impl.DefaultReference) DataElementValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.DataElementValue) Test(org.junit.Test)

Example 2 with DataElementValue

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);
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) Reference(io.adminshell.aas.v3.model.Reference) ServiceContext(de.fraunhofer.iosb.ilt.faaast.service.ServiceContext) DataElementValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.DataElementValue)

Example 3 with DataElementValue

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;
    }
}
Also used : Reference(io.adminshell.aas.v3.model.Reference) DefaultReference(io.adminshell.aas.v3.model.impl.DefaultReference) UaNode(com.prosysopc.ua.nodes.UaNode) NodeManagerUaNode(com.prosysopc.ua.server.NodeManagerUaNode) MethodManagerUaNode(com.prosysopc.ua.server.MethodManagerUaNode) ByteString(com.prosysopc.ua.stack.builtintypes.ByteString) LangString(io.adminshell.aas.v3.model.LangString) AASAnnotatedRelationshipElementType(opc.i4aas.AASAnnotatedRelationshipElementType) DefaultReference(io.adminshell.aas.v3.model.impl.DefaultReference) AnnotatedRelationshipElementValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.AnnotatedRelationshipElementValue) DataElementValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.DataElementValue)

Example 4 with DataElementValue

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());
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) Reference(io.adminshell.aas.v3.model.Reference) ServiceContext(de.fraunhofer.iosb.ilt.faaast.service.ServiceContext) NewDataListener(de.fraunhofer.iosb.ilt.faaast.service.assetconnection.NewDataListener) TimeUnit(java.util.concurrent.TimeUnit) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ElementValueTypeInfo(de.fraunhofer.iosb.ilt.faaast.service.typing.ElementValueTypeInfo) TypeInfo(de.fraunhofer.iosb.ilt.faaast.service.typing.TypeInfo) DataElementValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.DataElementValue)

Example 5 with DataElementValue

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);
            }
        }
    });
}
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)

Aggregations

DataElementValue (de.fraunhofer.iosb.ilt.faaast.service.model.value.DataElementValue)6 Reference (io.adminshell.aas.v3.model.Reference)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 ServiceContext (de.fraunhofer.iosb.ilt.faaast.service.ServiceContext)2 NewDataListener (de.fraunhofer.iosb.ilt.faaast.service.assetconnection.NewDataListener)2 PropertyValue (de.fraunhofer.iosb.ilt.faaast.service.model.value.PropertyValue)2 ElementValueTypeInfo (de.fraunhofer.iosb.ilt.faaast.service.typing.ElementValueTypeInfo)2 TypeInfo (de.fraunhofer.iosb.ilt.faaast.service.typing.TypeInfo)2 DefaultReference (io.adminshell.aas.v3.model.impl.DefaultReference)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 UaNode (com.prosysopc.ua.nodes.UaNode)1 MethodManagerUaNode (com.prosysopc.ua.server.MethodManagerUaNode)1 NodeManagerUaNode (com.prosysopc.ua.server.NodeManagerUaNode)1 ByteString (com.prosysopc.ua.stack.builtintypes.ByteString)1 AssetConnectionException (de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetConnectionException)1 AssetValueProvider (de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetValueProvider)1 AnnotatedRelationshipElementValue (de.fraunhofer.iosb.ilt.faaast.service.model.value.AnnotatedRelationshipElementValue)1 Datatype (de.fraunhofer.iosb.ilt.faaast.service.model.value.primitive.Datatype)1 StringValue (de.fraunhofer.iosb.ilt.faaast.service.model.value.primitive.StringValue)1 DeleteSubmodelByIdRequestHandler (de.fraunhofer.iosb.ilt.faaast.service.request.handler.DeleteSubmodelByIdRequestHandler)1