use of de.fraunhofer.iosb.ilt.faaast.service.model.value.ElementValue in project FAAAST-Service by FraunhoferIOSB.
the class ElementValueMapperTest method testPropertyToValueMapping.
@Test
public void testPropertyToValueMapping() throws ValueFormatException {
PropertyValue expected = PropertyValue.of(Datatype.String, "foo");
SubmodelElement input = new DefaultProperty.Builder().category("Test").idShort("TestProperty").valueType(expected.getValue().getDataType().getName()).value(expected.getValue().asString()).build();
ElementValue actual = ElementValueMapper.toValue(input);
Assert.assertEquals(expected, actual);
}
use of de.fraunhofer.iosb.ilt.faaast.service.model.value.ElementValue in project FAAAST-Service by FraunhoferIOSB.
the class PutSubmodelElementByPathRequestHandler method process.
@Override
public PutSubmodelElementByPathResponse process(PutSubmodelElementByPathRequest request) {
PutSubmodelElementByPathResponse response = new PutSubmodelElementByPathResponse();
try {
Reference reference = ReferenceHelper.toReference(request.getPath(), request.getId(), Submodel.class);
// Check if submodelelement does exist
SubmodelElement currentSubmodelElement = persistence.get(reference, new QueryModifier.Builder().extend(Extend.WithoutBLOBValue).level(Level.Core).build());
SubmodelElement newSubmodelElement = request.getSubmodelElement();
ElementValue oldValue = ElementValueMapper.toValue(currentSubmodelElement);
ElementValue newValue = ElementValueMapper.toValue(newSubmodelElement);
currentSubmodelElement = persistence.put(null, reference, newSubmodelElement);
response.setPayload(currentSubmodelElement);
response.setStatusCode(StatusCode.Success);
if (!Objects.equals(oldValue, newValue)) {
writeValueToAssetConnection(reference, ElementValueMapper.toValue(currentSubmodelElement));
}
publishElementUpdateEventMessage(reference, currentSubmodelElement);
} catch (ResourceNotFoundException ex) {
response.setStatusCode(StatusCode.ClientErrorResourceNotFound);
} catch (Exception ex) {
response.setStatusCode(StatusCode.ServerInternalError);
}
return response;
}
use of de.fraunhofer.iosb.ilt.faaast.service.model.value.ElementValue in project FAAAST-Service by FraunhoferIOSB.
the class ElementValueMapperTest method testAnnotatedRelationshipElementToValueMapping.
@Test
public void testAnnotatedRelationshipElementToValueMapping() throws ValueFormatException {
AnnotatedRelationshipElementValue expected = createAnnotatedRelationshipElementValue();
SubmodelElement input = new DefaultAnnotatedRelationshipElement.Builder().first(new DefaultReference.Builder().keys(expected.getFirst()).build()).second(new DefaultReference.Builder().keys(expected.getSecond()).build()).annotation(new DefaultProperty.Builder().idShort(expected.getAnnotations().keySet().iterator().next()).valueType(Datatype.String.getName()).value("foo").build()).build();
ElementValue actual = ElementValueMapper.toValue(input);
Assert.assertEquals(expected, actual);
}
use of de.fraunhofer.iosb.ilt.faaast.service.model.value.ElementValue 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.model.value.ElementValue in project FAAAST-Service by FraunhoferIOSB.
the class GetSubmodelElementByPathRequestHandler method process.
@Override
public GetSubmodelElementByPathResponse process(GetSubmodelElementByPathRequest request) {
GetSubmodelElementByPathResponse response = new GetSubmodelElementByPathResponse();
try {
Reference reference = ReferenceHelper.toReference(request.getPath(), request.getId(), Submodel.class);
SubmodelElement submodelElement = persistence.get(reference, request.getOutputModifier());
ElementValue oldValue = ElementValueMapper.toValue(submodelElement);
// read value from AssetConnection if one exist
// and update value in persistence if differs
ElementValue valueFromAssetConnection = readDataElementValueFromAssetConnection(reference);
if (valueFromAssetConnection != null && !Objects.equals(valueFromAssetConnection, oldValue)) {
submodelElement = ElementValueMapper.setValue(submodelElement, valueFromAssetConnection);
persistence.put(null, reference, submodelElement);
// TODO @Jens
// better publishValueChangeEventMessage(reference, oldValue, oldValue) ???
publishElementUpdateEventMessage(reference, submodelElement);
}
response.setPayload(submodelElement);
response.setStatusCode(StatusCode.Success);
publishElementReadEventMessage(reference, submodelElement);
} catch (ResourceNotFoundException ex) {
response.setStatusCode(StatusCode.ClientErrorResourceNotFound);
} catch (Exception ex) {
response.setStatusCode(StatusCode.ServerInternalError);
}
return response;
}
Aggregations