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);
}
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);
}
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);
}
}
});
}
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));
}
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);
}
}
Aggregations