Search in sources :

Example 1 with ElementValueTypeInfo

use of de.fraunhofer.iosb.ilt.faaast.service.typing.ElementValueTypeInfo in project FAAAST-Service by FraunhoferIOSB.

the class OpcUaAssetConnection method registerSubscriptionProvider.

/**
 * {@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
 */
@Override
public void registerSubscriptionProvider(Reference reference, OpcUaSubscriptionProviderConfig subscriptionProvider) throws AssetConnectionException {
    final String baseErrorMessage = "error registering subscription 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)));
    }
    this.subscriptionProviders.put(reference, new AssetSubscriptionProvider() {

        @Override
        public void addNewDataListener(NewDataListener listener) throws AssetConnectionException {
            if (!subscriptions.containsKey(subscriptionProvider.getNodeId())) {
                SubscriptionHandler handler = new SubscriptionHandler();
                handler.datatype = datatype;
                try {
                    handler.originalValue = client.readValue(0, TimestampsToReturn.Neither, client.getAddressSpace().getVariableNode(parseNodeId(subscriptionProvider.getNodeId())).getNodeId()).get();
                } catch (UaException | InterruptedException | ExecutionException ex) {
                    logger.warn("{} - reading initial value of subscribed node failed (reference: {}, nodeId: {})", baseErrorMessage, AasUtils.asString(reference), subscriptionProvider.getNodeId());
                }
                try {
                    handler.dataItem = opcUaSubscription.createDataItem(parseNodeId(subscriptionProvider.getNodeId()), LambdaExceptionHelper.rethrowConsumer(x -> {
                        x.addDataValueListener(LambdaExceptionHelper.rethrowConsumer(v -> handler.notify(v)));
                    }));
                } catch (UaException ex) {
                    logger.warn("{} - could not create subscrption item (reference: {}, nodeId: {})", baseErrorMessage, AasUtils.asString(reference), subscriptionProvider.getNodeId());
                }
                subscriptions.put(subscriptionProvider.getNodeId(), handler);
            }
            List<NewDataListener> listeners = subscriptions.get(subscriptionProvider.getNodeId()).listeners;
            if (!listeners.contains(listener)) {
                listeners.add(listener);
            }
        }

        @Override
        public void removeNewDataListener(NewDataListener listener) throws AssetConnectionException {
            if (subscriptions.containsKey(subscriptionProvider.getNodeId())) {
                SubscriptionHandler handler = subscriptions.get(subscriptionProvider.getNodeId());
                if (handler.listeners.contains(listener)) {
                    handler.listeners.remove(listener);
                }
                if (handler.listeners.isEmpty()) {
                    try {
                        handler.dataItem.delete();
                        subscriptions.remove(subscriptionProvider.getNodeId());
                    } catch (UaException ex) {
                        throw new AssetConnectionException(String.format("%s - removing subscription failed (reference: %s, nodeId: %s)", baseErrorMessage, AasUtils.asString(reference), subscriptionProvider.getNodeId()), ex);
                    }
                }
            }
        }
    });
}
Also used : SubmodelElement(io.adminshell.aas.v3.model.SubmodelElement) ValueConversionException(de.fraunhofer.iosb.ilt.faaast.service.assetconnection.opcua.conversion.ValueConversionException) DataElementValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.DataElementValue) VariableNode(org.eclipse.milo.opcua.sdk.core.nodes.VariableNode) LoggerFactory(org.slf4j.LoggerFactory) CallMethodResult(org.eclipse.milo.opcua.stack.core.types.structured.CallMethodResult) AddressSpace(org.eclipse.milo.opcua.sdk.client.AddressSpace) DefaultProperty(io.adminshell.aas.v3.model.impl.DefaultProperty) NewDataListener(de.fraunhofer.iosb.ilt.faaast.service.assetconnection.NewDataListener) Argument(org.eclipse.milo.opcua.stack.core.types.structured.Argument) ManagedDataItem(org.eclipse.milo.opcua.sdk.client.subscriptions.ManagedDataItem) Unsigned.uint(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint) Property(io.adminshell.aas.v3.model.Property) Map(java.util.Map) AssetConnection(de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetConnection) UaMethodNode(org.eclipse.milo.opcua.sdk.client.nodes.UaMethodNode) ManagedSubscription(org.eclipse.milo.opcua.sdk.client.subscriptions.ManagedSubscription) AnonymousProvider(org.eclipse.milo.opcua.sdk.client.api.identity.AnonymousProvider) TimestampsToReturn(org.eclipse.milo.opcua.stack.core.types.enumerated.TimestampsToReturn) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId) BrowseDirection(org.eclipse.milo.opcua.stack.core.types.enumerated.BrowseDirection) Reference(io.adminshell.aas.v3.model.Reference) Collectors(java.util.stream.Collectors) AssetConnectionException(de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetConnectionException) AasUtils(io.adminshell.aas.v3.dataformat.core.util.AasUtils) TypedValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.primitive.TypedValue) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) Variant(org.eclipse.milo.opcua.stack.core.types.builtin.Variant) CoreConfig(de.fraunhofer.iosb.ilt.faaast.service.config.CoreConfig) ElementValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.ElementValue) UaNode(org.eclipse.milo.opcua.sdk.client.nodes.UaNode) StatusCode(org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode) TypeInfo(de.fraunhofer.iosb.ilt.faaast.service.typing.TypeInfo) Optional(java.util.Optional) ElementValueTypeInfo(de.fraunhofer.iosb.ilt.faaast.service.typing.ElementValueTypeInfo) CallMethodRequest(org.eclipse.milo.opcua.stack.core.types.structured.CallMethodRequest) DefaultOperationVariable(io.adminshell.aas.v3.model.impl.DefaultOperationVariable) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) OpcUaClient(org.eclipse.milo.opcua.sdk.client.OpcUaClient) OperationVariable(io.adminshell.aas.v3.model.OperationVariable) ServiceContext(de.fraunhofer.iosb.ilt.faaast.service.ServiceContext) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) UShort(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UShort) SecurityPolicy(org.eclipse.milo.opcua.stack.core.security.SecurityPolicy) AssetOperationProvider(de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetOperationProvider) StatusCodes(org.eclipse.milo.opcua.stack.core.StatusCodes) Logger(org.slf4j.Logger) ElementValueMapper(de.fraunhofer.iosb.ilt.faaast.service.model.value.mapper.ElementValueMapper) Datatype(de.fraunhofer.iosb.ilt.faaast.service.model.value.primitive.Datatype) LocalizedText(org.eclipse.milo.opcua.stack.core.types.builtin.LocalizedText) ValueConverter(de.fraunhofer.iosb.ilt.faaast.service.assetconnection.opcua.conversion.ValueConverter) AssetSubscriptionProvider(de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetSubscriptionProvider) ExecutionException(java.util.concurrent.ExecutionException) UaException(org.eclipse.milo.opcua.stack.core.UaException) LambdaExceptionHelper(de.fraunhofer.iosb.ilt.faaast.service.util.LambdaExceptionHelper) AssetValueProvider(de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetValueProvider) PropertyValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.PropertyValue) 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) Datatype(de.fraunhofer.iosb.ilt.faaast.service.model.value.primitive.Datatype) NewDataListener(de.fraunhofer.iosb.ilt.faaast.service.assetconnection.NewDataListener) List(java.util.List) ArrayList(java.util.ArrayList) AssetConnectionException(de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetConnectionException) AssetSubscriptionProvider(de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetSubscriptionProvider)

Example 2 with ElementValueTypeInfo

use of de.fraunhofer.iosb.ilt.faaast.service.typing.ElementValueTypeInfo 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 3 with ElementValueTypeInfo

use of de.fraunhofer.iosb.ilt.faaast.service.typing.ElementValueTypeInfo in project FAAAST-Service by FraunhoferIOSB.

the class TypedValueDeserializer method deserialize.

@Override
public TypedValue deserialize(JsonParser parser, DeserializationContext context) throws IOException, JacksonException {
    TypeInfo typeInfo = ContextAwareElementValueDeserializer.getTypeInfo(context);
    if (typeInfo == null || !ElementValueTypeInfo.class.isAssignableFrom(typeInfo.getClass())) {
        throw new RuntimeException("missing datatype information");
    }
    Datatype datatype = ((ElementValueTypeInfo) typeInfo).getDatatype();
    try {
        return TypedValueFactory.create(datatype, parser.getValueAsString());
    } catch (ValueFormatException ex) {
        throw new IOException(String.format("error deserializing typed value (datatype: %s, value %s", datatype.getName(), parser.getValueAsString()), ex);
    }
}
Also used : ElementValueTypeInfo(de.fraunhofer.iosb.ilt.faaast.service.typing.ElementValueTypeInfo) ValueFormatException(de.fraunhofer.iosb.ilt.faaast.service.model.value.primitive.ValueFormatException) IOException(java.io.IOException) ElementValueTypeInfo(de.fraunhofer.iosb.ilt.faaast.service.typing.ElementValueTypeInfo) TypeInfo(de.fraunhofer.iosb.ilt.faaast.service.typing.TypeInfo) Datatype(de.fraunhofer.iosb.ilt.faaast.service.model.value.primitive.Datatype)

Aggregations

Datatype (de.fraunhofer.iosb.ilt.faaast.service.model.value.primitive.Datatype)3 ElementValueTypeInfo (de.fraunhofer.iosb.ilt.faaast.service.typing.ElementValueTypeInfo)3 TypeInfo (de.fraunhofer.iosb.ilt.faaast.service.typing.TypeInfo)3 AssetConnectionException (de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetConnectionException)2 AssetValueProvider (de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetValueProvider)2 DataElementValue (de.fraunhofer.iosb.ilt.faaast.service.model.value.DataElementValue)2 PropertyValue (de.fraunhofer.iosb.ilt.faaast.service.model.value.PropertyValue)2 VariableNode (org.eclipse.milo.opcua.sdk.core.nodes.VariableNode)2 UaException (org.eclipse.milo.opcua.stack.core.UaException)2 DataValue (org.eclipse.milo.opcua.stack.core.types.builtin.DataValue)2 StatusCode (org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode)2 ServiceContext (de.fraunhofer.iosb.ilt.faaast.service.ServiceContext)1 AssetConnection (de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetConnection)1 AssetOperationProvider (de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetOperationProvider)1 AssetSubscriptionProvider (de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetSubscriptionProvider)1 NewDataListener (de.fraunhofer.iosb.ilt.faaast.service.assetconnection.NewDataListener)1 ValueConversionException (de.fraunhofer.iosb.ilt.faaast.service.assetconnection.opcua.conversion.ValueConversionException)1 ValueConverter (de.fraunhofer.iosb.ilt.faaast.service.assetconnection.opcua.conversion.ValueConverter)1 CoreConfig (de.fraunhofer.iosb.ilt.faaast.service.config.CoreConfig)1 ElementValue (de.fraunhofer.iosb.ilt.faaast.service.model.value.ElementValue)1