use of de.fraunhofer.iosb.ilt.faaast.service.model.exception.ValueMappingException in project FAAAST-Service by FraunhoferIOSB.
the class GetAllSubmodelElementsRequestHandler method process.
@Override
public GetAllSubmodelElementsResponse process(GetAllSubmodelElementsRequest request) throws AssetConnectionException, ValueMappingException, ResourceNotFoundException, MessageBusException {
GetAllSubmodelElementsResponse response = new GetAllSubmodelElementsResponse();
Reference reference = ReferenceHelper.toReference(request.getId(), Submodel.class);
List<SubmodelElement> submodelElements = persistence.getSubmodelElements(reference, null, request.getOutputModifier());
syncWithAsset(reference, submodelElements);
response.setPayload(submodelElements);
response.setStatusCode(StatusCode.SUCCESS);
if (submodelElements != null) {
submodelElements.forEach(LambdaExceptionHelper.rethrowConsumer(x -> messageBus.publish(ElementReadEventMessage.builder().element(AasUtils.toReference(reference, x)).value(x).build())));
}
return response;
}
use of de.fraunhofer.iosb.ilt.faaast.service.model.exception.ValueMappingException in project FAAAST-Service by FraunhoferIOSB.
the class OpcUaAssetConnection method registerOperationProvider.
/**
* {@inheritDoc}
*
* @throws AssetConnectionException if nodeId could not be parsed
* @throws AssetConnectionException if nodeId does not refer to a method
* node
* @throws AssetConnectionException if parent node of nodeId could not be
* resolved
* @throws AssetConnectionException if output variables are null or do
* contain any other type than
* {@link de.fraunhofer.iosb.ilt.faaast.service.model.value.PropertyValue}
*/
@Override
public void registerOperationProvider(Reference reference, OpcUaOperationProviderConfig providerConfig) throws AssetConnectionException {
String baseErrorMessage = "error registering operation provider";
final NodeId nodeId = parseNodeId(providerConfig.getNodeId());
final UaNode node;
try {
node = client.getAddressSpace().getNode(nodeId);
} catch (UaException e) {
throw new AssetConnectionException(String.format("%s - could not resolve nodeId (nodeId: %s)", baseErrorMessage, providerConfig.getNodeId()), e);
}
if (!UaMethodNode.class.isAssignableFrom(node.getClass())) {
throw new AssetConnectionException(String.format("%s - provided node must be a method (nodeId: %s", baseErrorMessage, providerConfig.getNodeId()));
}
final UaMethodNode methodNode = (UaMethodNode) node;
final NodeId parentNodeId;
try {
parentNodeId = client.getAddressSpace().getNode(nodeId).browseNodes(AddressSpace.BrowseOptions.builder().setBrowseDirection(BrowseDirection.Inverse).build()).get(0).getNodeId();
} catch (UaException e) {
throw new AssetConnectionException(String.format("%s - could not resolve parent node (nodeId: %s)", baseErrorMessage, providerConfig.getNodeId()), e);
}
final Argument[] methodArguments;
try {
methodArguments = methodNode.readInputArgumentsAsync().get() != null ? methodNode.readInputArgumentsAsync().get() : new Argument[0];
} catch (InterruptedException | ExecutionException e) {
Thread.currentThread().interrupt();
throw new AssetConnectionException(String.format("%s - could not read input arguments (nodeId: %s)", baseErrorMessage, providerConfig.getNodeId()), e);
}
final Argument[] methodOutputArguments;
try {
methodOutputArguments = methodNode.readOutputArgumentsAsync().get() != null ? methodNode.readOutputArgumentsAsync().get() : new Argument[0];
} catch (InterruptedException | ExecutionException e) {
Thread.currentThread().interrupt();
throw new AssetConnectionException(String.format("%s - could not read ouput arguments (nodeId: %s)", baseErrorMessage, providerConfig.getNodeId()), e);
}
final OperationVariable[] outputVariables = serviceContext.getOperationOutputVariables(reference) != null ? serviceContext.getOperationOutputVariables(reference) : new OperationVariable[0];
for (var outputVariable : outputVariables) {
if (outputVariable == null) {
throw new AssetConnectionException(String.format("%s - output variable must be non-null (nodeId: %s)", baseErrorMessage, providerConfig.getNodeId()));
}
SubmodelElement submodelElement = outputVariable.getValue();
if (submodelElement == null) {
throw new AssetConnectionException(String.format("%s - output variable must contain non-null submodel element (nodeId: %s)", baseErrorMessage, providerConfig.getNodeId()));
}
if (!Property.class.isAssignableFrom(submodelElement.getClass())) {
throw new AssetConnectionException(String.format("%s - unsupported element type (nodeId: %s, element type: %s)", baseErrorMessage, submodelElement.getClass(), providerConfig.getNodeId()));
}
}
this.operationProviders.put(reference, new AssetOperationProvider() {
@Override
public OperationVariable[] invoke(OperationVariable[] input, OperationVariable[] inoutput) throws AssetConnectionException {
String baseErrorMessage = "error invoking operation on asset connection";
Map<String, ElementValue> inputParameter = new HashMap<>();
if (input != null) {
try {
inputParameter = Stream.of(input).collect(Collectors.toMap(x -> x.getValue().getIdShort(), LambdaExceptionHelper.rethrowFunction(x -> ElementValueMapper.toValue(x.getValue()))));
} catch (ValueMappingException e) {
throw new AssetConnectionException(String.format("%s - could not exract value of input parameters", baseErrorMessage), e);
}
}
Map<String, ElementValue> inoutputParameter = new HashMap<>();
if (inoutput != null) {
try {
inoutputParameter = Stream.of(inoutput).collect(Collectors.toMap(x -> x.getValue().getIdShort(), LambdaExceptionHelper.rethrowFunction(x -> ElementValueMapper.toValue(x.getValue()))));
} catch (ValueMappingException e) {
throw new AssetConnectionException(String.format("%s - could not exract value of inoutput parameters", baseErrorMessage), e);
}
}
if (methodArguments.length != (inputParameter.size() + inoutputParameter.size())) {
throw new AssetConnectionException(String.format("%s - argument count mismatch (expected: %d, provided input arguments: %d, provided inoutput arguments: %d)", baseErrorMessage, methodArguments.length, inputParameter.size(), inoutputParameter.size()));
}
Variant[] actualParameters = new Variant[methodArguments.length];
for (int i = 0; i < methodArguments.length; i++) {
String argumentName = methodArguments[i].getName();
ElementValue parameterValue;
if (inputParameter.containsKey(argumentName)) {
parameterValue = inputParameter.get(argumentName);
} else if (inoutputParameter.containsKey(argumentName)) {
parameterValue = inoutputParameter.get(argumentName);
} else {
throw new AssetConnectionException(String.format("%s - missing argument (argument name: %s)", baseErrorMessage, argumentName));
}
if (parameterValue == null) {
throw new AssetConnectionException(String.format("%s - parameter value must be non-null (argument name: %s)", baseErrorMessage, argumentName));
}
if (!PropertyValue.class.isAssignableFrom(parameterValue.getClass())) {
throw new AssetConnectionException(String.format("%s - currently only parameters of the Property are supported (argument name: %s, provided type: %s)", baseErrorMessage, argumentName, parameterValue.getClass()));
}
actualParameters[i] = valueConverter.convert(((PropertyValue) parameterValue).getValue(), methodArguments[i].getDataType());
}
CallMethodResult methodResult;
try {
methodResult = client.call(new CallMethodRequest(parentNodeId, nodeId, actualParameters)).get();
} catch (InterruptedException | ExecutionException e) {
Thread.currentThread().interrupt();
throw new AssetConnectionException(String.format("%s - executing OPC UA method failed (nodeId: %s)", baseErrorMessage, providerConfig.getNodeId()), e);
}
OperationVariable[] result = new OperationVariable[outputVariables.length];
for (int i = 0; i < methodOutputArguments.length; i++) {
String argumentName = methodArguments[i].getName();
for (int j = 0; j < outputVariables.length; j++) {
if (Objects.equals(argumentName, outputVariables[j].getValue().getIdShort())) {
SubmodelElement element = outputVariables[j].getValue();
Datatype targetType;
try {
targetType = ((PropertyValue) ElementValueMapper.toValue(element)).getValue().getDataType();
} catch (ValueMappingException e) {
throw new AssetConnectionException(String.format("%s - could not exract value of results variable with idShort '%s'", baseErrorMessage, element.getIdShort()), e);
}
TypedValue<?> newValue = valueConverter.convert(methodResult.getOutputArguments()[i], targetType);
Property newProperty = DeepCopyHelper.deepCopy(element, Property.class);
ElementValueMapper.setValue(newProperty, PropertyValue.builder().value(newValue).build());
result[j] = new DefaultOperationVariable.Builder().value(newProperty).build();
}
}
// update inoutput variable values
if (inoutputParameter.containsKey(argumentName) && inoutput != null) {
// find in original array and set there
for (int j = 0; j < inoutput.length; j++) {
if (Objects.equals(argumentName, inoutput[j].getValue().getIdShort())) {
ElementValueMapper.setValue(inoutput[j].getValue(), new PropertyValue(valueConverter.convert(methodResult.getOutputArguments()[i], ((PropertyValue) inoutputParameter.get(argumentName)).getValue().getDataType())));
}
}
}
}
return result;
}
});
}
use of de.fraunhofer.iosb.ilt.faaast.service.model.exception.ValueMappingException in project FAAAST-Service by FraunhoferIOSB.
the class AnnotatedRelationshipElementValueMapper method toValue.
@Override
public AnnotatedRelationshipElementValue toValue(AnnotatedRelationshipElement submodelElement) throws ValueMappingException {
if (submodelElement == null) {
return null;
}
AnnotatedRelationshipElementValue value = new AnnotatedRelationshipElementValue();
value.setAnnotations(submodelElement.getAnnotations().stream().collect(Collectors.toMap(x -> x.getIdShort(), LambdaExceptionHelper.rethrowFunction(x -> ElementValueMapper.toValue(x)))));
value.setFirst(submodelElement.getFirst().getKeys());
value.setSecond(submodelElement.getSecond().getKeys());
return value;
}
use of de.fraunhofer.iosb.ilt.faaast.service.model.exception.ValueMappingException in project FAAAST-Service by FraunhoferIOSB.
the class RangeValueMapper method toValue.
@Override
public RangeValue toValue(Range submodelElement) throws ValueMappingException {
if (submodelElement == null) {
return null;
}
RangeValue rangeValue = new RangeValue();
try {
rangeValue.setMin(TypedValueFactory.create(submodelElement.getValueType(), submodelElement.getMin()));
rangeValue.setMax(TypedValueFactory.create(submodelElement.getValueType(), submodelElement.getMax()));
} catch (ValueFormatException e) {
throw new ValueMappingException("invalid data value", e);
}
return rangeValue;
}
use of de.fraunhofer.iosb.ilt.faaast.service.model.exception.ValueMappingException in project FAAAST-Service by FraunhoferIOSB.
the class InvokeOperationAsyncRequestHandler method executeOperationAsync.
public OperationHandle executeOperationAsync(Reference reference, InvokeOperationAsyncRequest request) throws MessageBusException, Exception {
if (!assetConnectionManager.hasOperationProvider(reference)) {
throw new IllegalArgumentException(String.format("error executing operation - no operation provider defined for reference '%s' (requestId: %s)", AasUtils.asString(reference), request.getRequestId()));
}
OperationHandle operationHandle = this.persistence.putOperationContext(null, request.getRequestId(), new OperationResult.Builder().requestId(request.getRequestId()).inoutputArguments(request.getInoutputArguments()).executionState(ExecutionState.RUNNING).build());
try {
BiConsumer<OperationVariable[], OperationVariable[]> callback = LambdaExceptionHelper.rethrowBiConsumer((x, y) -> {
OperationResult operationResult = persistence.getOperationResult(operationHandle.getHandleId());
operationResult.setExecutionState(ExecutionState.COMPLETED);
operationResult.setOutputArguments(Arrays.asList(x));
operationResult.setInoutputArguments(Arrays.asList(y));
persistence.putOperationContext(operationHandle.getHandleId(), operationHandle.getRequestId(), operationResult);
messageBus.publish(OperationFinishEventMessage.builder().element(reference).inoutput(ElementValueHelper.toValues(Arrays.asList(x))).output(ElementValueHelper.toValues(Arrays.asList(y))).build());
});
AssetOperationProvider assetOperationProvider = assetConnectionManager.getOperationProvider(reference);
assetOperationProvider.invokeAsync(request.getInputArguments().toArray(new OperationVariable[0]), request.getInoutputArguments().toArray(new OperationVariable[0]), callback);
} catch (AssetConnectionException | ValueMappingException e) {
OperationResult operationResult = persistence.getOperationResult(operationHandle.getHandleId());
operationResult.setExecutionState(ExecutionState.FAILED);
operationResult.setInoutputArguments(request.getInoutputArguments());
persistence.putOperationContext(operationHandle.getHandleId(), operationHandle.getRequestId(), operationResult);
try {
messageBus.publish(OperationFinishEventMessage.builder().element(reference).inoutput(ElementValueHelper.toValues(operationResult.getInoutputArguments())).build());
} catch (ValueMappingException e2) {
LOGGER.warn("could not publish operation finished event message because mapping result to value objects failed", e2);
}
}
return operationHandle;
}
Aggregations