Search in sources :

Example 11 with PropertyValue

use of de.fraunhofer.iosb.ilt.faaast.service.model.value.PropertyValue in project FAAAST-Service by FraunhoferIOSB.

the class RequestHandlerManagerTest method testWriteValueToAssetConnection.

@Test
public void testWriteValueToAssetConnection() 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);
    requestHandler.writeValueToAssetConnection(new DefaultReference(), expected);
    verify(assetValueProvider).setValue(expected);
}
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) Test(org.junit.Test)

Example 12 with PropertyValue

use of de.fraunhofer.iosb.ilt.faaast.service.model.value.PropertyValue in project FAAAST-Service by FraunhoferIOSB.

the class RequestHandlerManagerTest method testGetSubmodelElementByPathRequest.

@Test
public void testGetSubmodelElementByPathRequest() throws ResourceNotFoundException, AssetConnectionException {
    Submodel submodel = environment.getSubmodels().get(0);
    SubmodelElement cur_submodelElement = new DefaultProperty.Builder().idShort("testIdShort").value("testValue").build();
    PropertyValue propertyValue = new PropertyValue.Builder().value(new StringValue("test")).build();
    when(persistence.get(argThat((Reference t) -> true), eq(new OutputModifier()))).thenReturn(cur_submodelElement);
    when(assetConnectionManager.hasValueProvider(any())).thenReturn(true);
    when(assetValueProvider.getValue()).thenReturn(propertyValue);
    GetSubmodelElementByPathRequest request = new GetSubmodelElementByPathRequest.Builder().id(submodel.getIdentification()).outputModifier(new OutputModifier()).path(ReferenceHelper.toKeys(SUBMODEL_ELEMENT_REF)).build();
    GetSubmodelElementByPathResponse response = manager.execute(request);
    SubmodelElement expected_submodelElement = new DefaultProperty.Builder().idShort("testIdShort").value("test").valueType("string").build();
    GetSubmodelElementByPathResponse expected = new GetSubmodelElementByPathResponse.Builder().payload(expected_submodelElement).statusCode(StatusCode.Success).build();
    Assert.assertEquals(expected, response);
}
Also used : Submodel(io.adminshell.aas.v3.model.Submodel) SubmodelElement(io.adminshell.aas.v3.model.SubmodelElement) GetSubmodelElementByPathRequest(de.fraunhofer.iosb.ilt.faaast.service.model.request.GetSubmodelElementByPathRequest) OutputModifier(de.fraunhofer.iosb.ilt.faaast.service.model.api.modifier.OutputModifier) Reference(io.adminshell.aas.v3.model.Reference) DefaultReference(io.adminshell.aas.v3.model.impl.DefaultReference) AtomicReference(java.util.concurrent.atomic.AtomicReference) PropertyValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.PropertyValue) GetSubmodelElementByPathResponse(de.fraunhofer.iosb.ilt.faaast.service.model.api.response.GetSubmodelElementByPathResponse) StringValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.primitive.StringValue) DefaultProperty(io.adminshell.aas.v3.model.impl.DefaultProperty) Test(org.junit.Test)

Example 13 with PropertyValue

use of de.fraunhofer.iosb.ilt.faaast.service.model.value.PropertyValue 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 providerConfig) 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(providerConfig.getNodeId()));
    } catch (UaException e) {
        throw new AssetConnectionException(String.format("%s - could not parse nodeId (nodeId: %s)", baseErrorMessage, providerConfig.getNodeId()), e);
    }
    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) {
                Thread.currentThread().interrupt();
                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) {
                Thread.currentThread().interrupt();
                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) 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 14 with PropertyValue

use of de.fraunhofer.iosb.ilt.faaast.service.model.value.PropertyValue in project FAAAST-Service by FraunhoferIOSB.

the class MqttAssetConnectionTest method testSubscriptionProviderJsonPropertyInvalidMessage.

@Test
public void testSubscriptionProviderJsonPropertyInvalidMessage() throws AssetConnectionException, InterruptedException, ValueFormatException, ConfigurationInitializationException, IOException {
    ListAppender<ILoggingEvent> listLogger = getListLogger(MqttSubscriptionProvider.class);
    ContentFormat contentFormat = ContentFormat.JSON;
    String message = "7";
    PropertyValue expected = PropertyValue.of(Datatype.INT, "7");
    MqttAssetConnection assetConnection = newConnection(ElementValueTypeInfo.builder().datatype(expected.getValue().getDataType()).type(expected.getClass()).build(), MqttSubscriptionProviderConfig.builder().contentFormat(contentFormat).query(null).build());
    CountDownLatch condition = new CountDownLatch(2);
    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);
    publishMqtt(DEFAULT_TOPIC, "foo");
    publishMqtt(DEFAULT_TOPIC, message);
    condition.await(DEFAULT_TIMEOUT, isDebugging() ? TimeUnit.SECONDS : TimeUnit.MILLISECONDS);
    Assert.assertEquals(expected, response.get());
    Assert.assertTrue(hasLogEvent(listLogger, LOG_MSG_DESERIALIZATION_FAILED));
}
Also used : ContentFormat(de.fraunhofer.iosb.ilt.faaast.service.assetconnection.mqtt.content.ContentFormat) PropertyValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.PropertyValue) NewDataListener(de.fraunhofer.iosb.ilt.faaast.service.assetconnection.NewDataListener) AtomicReference(java.util.concurrent.atomic.AtomicReference) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) CountDownLatch(java.util.concurrent.CountDownLatch) DataElementValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.DataElementValue) Test(org.junit.Test)

Example 15 with PropertyValue

use of de.fraunhofer.iosb.ilt.faaast.service.model.value.PropertyValue in project FAAAST-Service by FraunhoferIOSB.

the class AasServiceNodeManager method setPropertyValueAndType.

/**
 * Adds the OPC UA property itself to the given Property object and sets the value.
 *
 * @param aasProperty The AAS property
 * @param submodel The corresponding Submodel as parent object of the data element
 * @param prop The UA Property object
 * @param propRef The AAS reference to the property
 */
@SuppressWarnings("java:S125")
private void setPropertyValueAndType(Property aasProperty, Submodel submodel, AASPropertyType prop, Reference propRef) {
    try {
        NodeId myPropertyId = new NodeId(getNamespaceIndex(), prop.getNodeId().getValue().toString() + "." + AASPropertyType.VALUE);
        QualifiedName browseName = UaQualifiedName.from(opc.i4aas.ObjectTypeIds.AASPropertyType.getNamespaceUri(), AASPropertyType.VALUE).toQualifiedName(getNamespaceTable());
        LocalizedText displayName = LocalizedText.english(AASPropertyType.VALUE);
        submodelElementAasMap.put(myPropertyId, new SubmodelElementData(aasProperty, submodel, SubmodelElementData.Type.PROPERTY_VALUE, propRef));
        LOG.debug("setPropertyValueAndType: NodeId {}; Property: {}", myPropertyId, aasProperty);
        if (submodel != null) {
            submodelElementOpcUAMap.put(propRef, prop);
        }
        AASValueTypeDataType valueDataType;
        PropertyValue typedValue = PropertyValue.of(aasProperty.getValueType(), aasProperty.getValue());
        if ((typedValue != null) && (typedValue.getValue() != null)) {
            valueDataType = ValueConverter.datatypeToValueType(typedValue.getValue().getDataType());
        } else {
            valueDataType = ValueConverter.stringToValueType(aasProperty.getValueType());
        }
        prop.setValueType(valueDataType);
        switch(valueDataType) {
            // 
            case Boolean:
                PlainProperty<Boolean> myBoolProperty = new PlainProperty<>(this, myPropertyId, browseName, displayName);
                myBoolProperty.setDataTypeId(Identifiers.Boolean);
                if ((typedValue != null) && (typedValue.getValue() != null) && (typedValue.getValue().getValue() != null)) {
                    myBoolProperty.setValue(typedValue.getValue().getValue());
                }
                prop.addProperty(myBoolProperty);
                break;
            // 
            case Int32:
                PlainProperty<Integer> myIntProperty = new PlainProperty<>(this, myPropertyId, browseName, displayName);
                myIntProperty.setDataTypeId(Identifiers.Int32);
                if ((typedValue != null) && (typedValue.getValue() != null) && (typedValue.getValue().getValue() != null)) {
                    myIntProperty.setValue(typedValue.getValue().getValue());
                }
                prop.addProperty(myIntProperty);
                break;
            // 
            case Int64:
                PlainProperty<Long> myLongProperty = new PlainProperty<>(this, myPropertyId, browseName, displayName);
                myLongProperty.setDataTypeId(Identifiers.Int64);
                if ((typedValue != null) && (typedValue.getValue() != null) && (typedValue.getValue().getValue() != null)) {
                    Object obj = typedValue.getValue().getValue();
                    if (!(obj instanceof Long)) {
                        obj = Long.parseLong(obj.toString());
                    }
                    myLongProperty.setValue(obj);
                }
                prop.addProperty(myLongProperty);
                break;
            // 
            case Int16:
                PlainProperty<Short> myInt16Property = new PlainProperty<>(this, myPropertyId, browseName, displayName);
                myInt16Property.setDataTypeId(Identifiers.Int16);
                if ((typedValue != null) && (typedValue.getValue() != null) && (typedValue.getValue().getValue() != null)) {
                    myInt16Property.setValue(typedValue.getValue().getValue());
                }
                prop.addProperty(myInt16Property);
                break;
            // 
            case SByte:
                PlainProperty<Byte> mySByteProperty = new PlainProperty<>(this, myPropertyId, browseName, displayName);
                mySByteProperty.setDataTypeId(Identifiers.SByte);
                if ((typedValue != null) && (typedValue.getValue() != null) && (typedValue.getValue().getValue() != null)) {
                    mySByteProperty.setValue(typedValue.getValue().getValue());
                }
                prop.addProperty(mySByteProperty);
                break;
            case Double:
                PlainProperty<Double> myDoubleProperty = new PlainProperty<>(this, myPropertyId, browseName, displayName);
                myDoubleProperty.setDataTypeId(Identifiers.Double);
                if ((typedValue != null) && (typedValue.getValue() != null) && (typedValue.getValue().getValue() != null)) {
                    myDoubleProperty.setValue(typedValue.getValue().getValue());
                }
                prop.addProperty(myDoubleProperty);
                break;
            case Float:
                PlainProperty<Float> myFloatProperty = new PlainProperty<>(this, myPropertyId, browseName, displayName);
                myFloatProperty.setDataTypeId(Identifiers.Float);
                if ((typedValue != null) && (typedValue.getValue() != null) && (typedValue.getValue().getValue() != null)) {
                    myFloatProperty.setValue(typedValue.getValue().getValue());
                }
                prop.addProperty(myFloatProperty);
                break;
            // 
            case String:
                PlainProperty<String> myStringProperty = new PlainProperty<>(this, myPropertyId, browseName, displayName);
                myStringProperty.setDataTypeId(Identifiers.String);
                if ((typedValue != null) && (typedValue.getValue() != null) && (typedValue.getValue().getValue() != null)) {
                    myStringProperty.setValue(typedValue.getValue().getValue());
                }
                prop.addProperty(myStringProperty);
                break;
            // 
            default:
                LOG.warn("setValueAndType: Property {}: Unknown type: {}; use string as default", prop.getBrowseName().getName(), aasProperty.getValueType());
                PlainProperty<String> myDefaultProperty = new PlainProperty<>(this, myPropertyId, browseName, displayName);
                myDefaultProperty.setDataTypeId(Identifiers.String);
                myDefaultProperty.setValue(aasProperty.getValue());
                prop.addProperty(myDefaultProperty);
                break;
        }
        if ((prop.getValueNode() != null) && (prop.getValueNode().getDescription() == null)) {
            prop.getValueNode().setDescription(new LocalizedText("", ""));
        }
    } catch (Exception ex) {
        LOG.error("setPropertyValueAndType Exception", ex);
    }
}
Also used : ByteString(com.prosysopc.ua.stack.builtintypes.ByteString) LangString(io.adminshell.aas.v3.model.LangString) LocalizedText(com.prosysopc.ua.stack.builtintypes.LocalizedText) AASValueTypeDataType(opc.i4aas.AASValueTypeDataType) PlainProperty(com.prosysopc.ua.server.nodes.PlainProperty) UaQualifiedName(com.prosysopc.ua.UaQualifiedName) QualifiedName(com.prosysopc.ua.stack.builtintypes.QualifiedName) PropertyValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.PropertyValue) MultiLanguagePropertyValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.MultiLanguagePropertyValue) ServiceResultException(com.prosysopc.ua.stack.common.ServiceResultException) StatusException(com.prosysopc.ua.StatusException) MessageBusException(de.fraunhofer.iosb.ilt.faaast.service.exception.MessageBusException) ServiceException(com.prosysopc.ua.ServiceException) UaNodeFactoryException(com.prosysopc.ua.nodes.UaNodeFactoryException) AddressSpaceException(com.prosysopc.ua.client.AddressSpaceException) UnsignedInteger(com.prosysopc.ua.stack.builtintypes.UnsignedInteger) NodeId(com.prosysopc.ua.stack.builtintypes.NodeId) UaObject(com.prosysopc.ua.nodes.UaObject) SubmodelElementData(de.fraunhofer.iosb.ilt.faaast.service.endpoint.opcua.data.SubmodelElementData)

Aggregations

PropertyValue (de.fraunhofer.iosb.ilt.faaast.service.model.value.PropertyValue)16 Test (org.junit.Test)12 StringValue (de.fraunhofer.iosb.ilt.faaast.service.model.value.primitive.StringValue)9 SubmodelElement (io.adminshell.aas.v3.model.SubmodelElement)6 DefaultProperty (io.adminshell.aas.v3.model.impl.DefaultProperty)6 DataElementValue (de.fraunhofer.iosb.ilt.faaast.service.model.value.DataElementValue)5 DefaultReference (io.adminshell.aas.v3.model.impl.DefaultReference)5 GetSubmodelElementByPathResponse (de.fraunhofer.iosb.ilt.faaast.service.model.api.response.GetSubmodelElementByPathResponse)4 ElementValue (de.fraunhofer.iosb.ilt.faaast.service.model.value.ElementValue)4 MultiLanguagePropertyValue (de.fraunhofer.iosb.ilt.faaast.service.model.value.MultiLanguagePropertyValue)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 Reference (io.adminshell.aas.v3.model.Reference)3 ServiceException (com.prosysopc.ua.ServiceException)2 StatusException (com.prosysopc.ua.StatusException)2 AddressSpaceException (com.prosysopc.ua.client.AddressSpaceException)2 ByteString (com.prosysopc.ua.stack.builtintypes.ByteString)2 LocalizedText (com.prosysopc.ua.stack.builtintypes.LocalizedText)2 NodeId (com.prosysopc.ua.stack.builtintypes.NodeId)2 QualifiedName (com.prosysopc.ua.stack.builtintypes.QualifiedName)2 ServiceResultException (com.prosysopc.ua.stack.common.ServiceResultException)2