use of de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetConnectionException in project FAAAST-Service by FraunhoferIOSB.
the class OpcUaAssetConnection method connect.
private void connect() throws AssetConnectionException {
try {
client = OpcUaClient.create(config.getHost(), endpoints -> endpoints.stream().filter(e -> e.getSecurityPolicyUri().equals(SecurityPolicy.None.getUri())).findFirst(), configBuilder -> configBuilder.setApplicationName(LocalizedText.english("AAS-Service")).setApplicationUri("urn:de:fraunhofer:iosb:aas:service").setIdentityProvider(AnonymousProvider.INSTANCE).setRequestTimeout(uint(1000)).setAcknowledgeTimeout(uint(1000)).build());
client.connect().get();
// without sleep bad timeout while waiting for acknowledge appears from time to time
Thread.sleep(200);
opcUaSubscription = ManagedSubscription.create(client);
} catch (InterruptedException | ExecutionException | UaException ex) {
throw new AssetConnectionException(String.format("error opening OPC UA connection (endpoint: %s)", config.getHost()), ex);
}
}
use of de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetConnectionException 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 operationProvider) throws AssetConnectionException {
String baseErrorMessage = "error registering operation provider";
final NodeId nodeId = parseNodeId(operationProvider.getNodeId());
final UaNode node;
try {
node = client.getAddressSpace().getNode(nodeId);
} catch (UaException ex) {
throw new AssetConnectionException(String.format("%s - could not resolve nodeId (nodeId: %s)", baseErrorMessage, operationProvider.getNodeId()), ex);
}
if (!UaMethodNode.class.isAssignableFrom(node.getClass())) {
throw new AssetConnectionException(String.format("%s - provided node must be a method (nodeId: %s", baseErrorMessage, operationProvider.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 ex) {
throw new AssetConnectionException(String.format("%s - could not resolve parent node (nodeId: %s)", baseErrorMessage, operationProvider.getNodeId()), ex);
}
final Argument[] methodArguments;
try {
methodArguments = methodNode.readInputArgumentsAsync().get() != null ? methodNode.readInputArgumentsAsync().get() : new Argument[0];
} catch (InterruptedException | ExecutionException ex) {
throw new AssetConnectionException(String.format("%s - could not read input arguments (nodeId: %s)", baseErrorMessage, operationProvider.getNodeId()), ex);
}
final Argument[] methodOutputArguments;
try {
methodOutputArguments = methodNode.readOutputArgumentsAsync().get() != null ? methodNode.readOutputArgumentsAsync().get() : new Argument[0];
} catch (InterruptedException | ExecutionException ex) {
throw new AssetConnectionException(String.format("%s - could not read ouput arguments (nodeId: %s)", baseErrorMessage, operationProvider.getNodeId()), ex);
}
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, operationProvider.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, operationProvider.getNodeId()));
}
if (!Property.class.isAssignableFrom(submodelElement.getClass())) {
throw new AssetConnectionException(String.format("%s - unsupported element type (nodeId: %s, element type: %s)", baseErrorMessage, submodelElement.getClass(), operationProvider.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 = input == null ? new HashMap<>() : Stream.of(input).collect(Collectors.toMap(x -> x.getValue().getIdShort(), x -> ElementValueMapper.toValue(x.getValue())));
Map<String, ElementValue> inoutputParameter = inoutput == null ? new HashMap<>() : Stream.of(inoutput).collect(Collectors.toMap(x -> x.getValue().getIdShort(), x -> ElementValueMapper.toValue(x.getValue())));
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 ex) {
throw new AssetConnectionException(String.format("%s - executing OPC UA method failed (nodeId: %s)", baseErrorMessage, operationProvider.getNodeId()));
}
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 = ((PropertyValue) ElementValueMapper.toValue(element)).getValue().getDataType();
TypedValue<?> newValue = valueConverter.convert(methodResult.getOutputArguments()[i], targetType);
// TODO better use deep copy?
DefaultProperty newProperty = new DefaultProperty.Builder().idShort(element.getIdShort()).build();
ElementValueMapper.setValue(newProperty, PropertyValue.builder().value(newValue).build());
result[j] = new DefaultOperationVariable.Builder().value(newProperty).build();
}
}
// update inoutput variable values
if (inoutputParameter.containsKey(argumentName)) {
// 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.assetconnection.AssetConnectionException in project FAAAST-Service by FraunhoferIOSB.
the class MqttAssetConnectionTest method invokeValueProvider.
public String invokeValueProvider(ContentFormat contentFormat, DataElementValue newValue, String query) throws AssetConnectionException, InterruptedException, MqttException {
MqttAssetConnection assetConnection = newConnection(MqttValueProviderConfig.builder().contentFormat(contentFormat).query(query).build());
MqttClient client = newMqttClient();
final AtomicReference<String> response = new AtomicReference<>();
CountDownLatch condition = new CountDownLatch(1);
client.setCallback(new MqttCallback() {
@Override
public void connectionLost(Throwable throwable) {
}
@Override
public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {
response.set(new String(mqttMessage.getPayload()));
condition.countDown();
}
@Override
public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
}
});
client.subscribe(DEFAULT_TOPIC);
assetConnection.getValueProviders().get(DEFAULT_REFERENCE).setValue(newValue);
condition.await(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS);
return response.get();
}
use of de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetConnectionException in project FAAAST-Service by FraunhoferIOSB.
the class InvokeOperationAsyncRequestHandler method executeOperationAsync.
public OperationHandle executeOperationAsync(Reference reference, InvokeOperationAsyncRequest request) {
if (assetConnectionManager.hasOperationProvider(reference)) {
OperationHandle operationHandle = this.persistence.putOperationContext(null, request.getRequestId(), new OperationResult.Builder().requestId(request.getRequestId()).inoutputArguments(request.getInoutputArguments()).executionState(ExecutionState.Running).build());
BiConsumer<OperationVariable[], OperationVariable[]> callback = (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);
publishOperationFinishEventMessage(reference, toValues(Arrays.asList(x)), toValues(Arrays.asList(y)));
};
AssetOperationProvider assetOperationProvider = assetConnectionManager.getOperationProvider(reference);
try {
assetOperationProvider.invokeAsync(request.getInputArguments().toArray(new OperationVariable[0]), request.getInoutputArguments().toArray(new OperationVariable[0]), callback);
} catch (AssetConnectionException ex) {
OperationResult operationResult = persistence.getOperationResult(operationHandle.getHandleId());
operationResult.setExecutionState(ExecutionState.Failed);
operationResult.setInoutputArguments(request.getInoutputArguments());
persistence.putOperationContext(operationHandle.getHandleId(), operationHandle.getRequestId(), operationResult);
publishOperationFinishEventMessage(reference, List.of(), toValues(operationResult.getInoutputArguments()));
}
return operationHandle;
} else {
throw new RuntimeException("No assetconnection available for running operation with request id" + request.getRequestId());
}
}
use of de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetConnectionException 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);
}
}
}
}
});
}
Aggregations