use of com.prosysopc.ua.StatusException in project FAAAST-Service by FraunhoferIOSB.
the class AasServiceNodeManager method elementDeleted.
/**
* Handles an elementDeleted event.
*
* @param element Reference to the deleted element.
* @throws StatusException If the operation fails
*/
@SuppressWarnings("java:S2629")
private void elementDeleted(Reference element) throws StatusException {
if (element == null) {
throw new IllegalArgumentException(ELEMENT_NULL);
}
try {
LOG.debug("elementDeleted called. Reference {}", AasUtils.asString(element));
// The element is the object that should be deleted
ObjectData data = null;
if (referableMap.containsKey(element)) {
data = referableMap.get(element);
// remove element from the map
referableMap.remove(element);
} else {
LOG.info("elementDeleted: element not found in referableMap: {}", AasUtils.asString(element));
}
if (data != null) {
removeFromMaps(data.getNode(), element, data.getReferable());
deleteNode(data.getNode(), true, true);
}
} catch (Exception ex) {
LOG.error("elementDeleted Exception", ex);
throw ex;
}
}
use of com.prosysopc.ua.StatusException in project FAAAST-Service by FraunhoferIOSB.
the class AasServiceNodeManager method setReferenceElementValue.
/**
* Sets the value for the given ReferenceElement.
*
* @param refElement The desired ReferenceElement.
* @param value The new value.
* @throws StatusException If the operation fails
*/
private void setReferenceElementValue(AASReferenceElementType refElement, ReferenceElementValue value) throws StatusException {
if (refElement == null) {
throw new IllegalArgumentException("refElement is null");
} else if (value == null) {
throw new IllegalArgumentException(VALUE_NULL);
}
try {
DefaultReference ref = new DefaultReference.Builder().keys(value.getKeys()).build();
setAasReferenceData(ref, refElement.getValueNode());
} catch (Exception ex) {
LOG.error("setReferenceElementValue Exception", ex);
throw ex;
}
}
use of com.prosysopc.ua.StatusException in project FAAAST-Service by FraunhoferIOSB.
the class OpcUaEndpointTest method testUpdatePropertyValue.
/**
* Test method for changing a property based on an event from the
* MessageBus. Sets an event on the MessageBus and checks the new value in
* the server.
*
* @throws SecureIdentityException If the operation fails
* @throws ServiceException If the operation fails
* @throws IOException If the operation fails
* @throws StatusException If the operation fails
*/
@Test
public void testUpdatePropertyValue() throws SecureIdentityException, ServiceException, IOException, StatusException, Exception {
UaClient client = new UaClient(ENDPOINT_URL);
client.setSecurityMode(SecurityMode.NONE);
TestUtils.initialize(client);
client.connect();
System.out.println("testUpdatePropertyValue: client connected");
aasns = client.getAddressSpace().getNamespaceTable().getIndex(VariableIds.AASAssetAdministrationShellType_AssetInformation_AssetKind.getNamespaceUri());
List<RelativePath> relPath = new ArrayList<>();
List<RelativePathElement> browsePath = new ArrayList<>();
browsePath.add(new RelativePathElement(Identifiers.HierarchicalReferences, false, true, new QualifiedName(aasns, TestDefines.AAS_ENVIRONMENT_NAME)));
browsePath.add(new RelativePathElement(Identifiers.HierarchicalReferences, false, true, new QualifiedName(aasns, TestDefines.SUBMODEL_OPER_DATA_NODE_NAME)));
browsePath.add(new RelativePathElement(Identifiers.HierarchicalReferences, false, true, new QualifiedName(aasns, TestDefines.ROTATION_SPEED_NAME)));
browsePath.add(new RelativePathElement(Identifiers.HasProperty, false, true, new QualifiedName(aasns, TestDefines.PROPERTY_VALUE_NAME)));
relPath.add(new RelativePath(browsePath.toArray(RelativePathElement[]::new)));
BrowsePathResult[] bpres = client.getAddressSpace().translateBrowsePathsToNodeIds(Identifiers.ObjectsFolder, relPath.toArray(RelativePath[]::new));
Assert.assertNotNull("testWriteProperty Browse Result Null", bpres);
Assert.assertTrue("testWriteProperty Browse Result: size doesn't match", bpres.length == 1);
Assert.assertTrue("testWriteProperty Browse Result Good", bpres[0].getStatusCode().isGood());
BrowsePathTarget[] targets = bpres[0].getTargets();
Assert.assertNotNull("testWriteProperty ValueType Null", targets);
Assert.assertTrue("testWriteProperty ValueType empty", targets.length > 0);
DataValue value = client.readValue(targets[0].getTargetId());
Assert.assertEquals(StatusCode.GOOD, value.getStatusCode());
Integer oldValue = 4370;
Assert.assertEquals("intial value not equal", oldValue, value.getValue().getValue());
CountDownLatch condition = new CountDownLatch(1);
final AtomicReference<EventMessage> response = new AtomicReference<>();
service.getMessageBus().subscribe(SubscriptionInfo.create(ValueChangeEventMessage.class, x -> {
response.set(x);
condition.countDown();
}));
Integer newValue = 9999;
List<Key> keys = new ArrayList<>();
keys.add(new DefaultKey.Builder().idType(KeyType.IRI).type(KeyElements.SUBMODEL).value(TestDefines.SUBMODEL_OPER_DATA_NAME).build());
keys.add(new DefaultKey.Builder().idType(KeyType.ID_SHORT).type(KeyElements.PROPERTY).value(TestDefines.ROTATION_SPEED_NAME).build());
Reference propRef = new DefaultReference.Builder().keys(keys).build();
ValueChangeEventMessage valueChangeMessage = new ValueChangeEventMessage();
valueChangeMessage.setElement(propRef);
// PropertyValue propertyValue = new PropertyValue();
// propertyValue.setValue(new IntValue(oldValue));
valueChangeMessage.setOldValue(PropertyValue.of(Datatype.INT, oldValue.toString()));
// propertyValue.setValue(new IntValue(newValue));
valueChangeMessage.setNewValue(PropertyValue.of(Datatype.INT, newValue.toString()));
service.getMessageBus().publish(valueChangeMessage);
Thread.sleep(100);
// check MessageBus
condition.await(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS);
Assert.assertEquals(valueChangeMessage, response.get());
// read new value
value = client.readValue(targets[0].getTargetId());
Assert.assertEquals(StatusCode.GOOD, value.getStatusCode());
Assert.assertEquals("new value not equal", newValue, value.getValue().getValue());
System.out.println("disconnect client");
client.disconnect();
}
use of com.prosysopc.ua.StatusException in project FAAAST-Service by FraunhoferIOSB.
the class OpcUaEndpoint method callOperation.
/**
* Calls the desired operation in the service.
*
* @param operation The desired operation
* @param inputVariables The input arguments
* @param submodel The corresponding submodel
* @param refElement The reference to the SubmodelElement
* @return The OutputArguments The output arguments returned from the operation call
* @throws StatusException If the operation fails
*/
public List<OperationVariable> callOperation(Operation operation, List<OperationVariable> inputVariables, Submodel submodel, Reference refElement) throws StatusException {
List<OperationVariable> outputArguments;
try {
InvokeOperationSyncRequest request = new InvokeOperationSyncRequest();
List<Key> path = new ArrayList<>();
path.addAll(refElement.getKeys());
request.setId(submodel.getIdentification());
request.setPath(path);
request.setInputArguments(inputVariables);
request.setContent(Content.NORMAL);
requestCounter++;
request.setRequestId(Integer.toString(requestCounter));
// execute method
InvokeOperationSyncResponse response = (InvokeOperationSyncResponse) service.execute(request);
if (isSuccess(response.getStatusCode())) {
LOGGER.info("callOperation: Operation {} executed successfully", operation.getIdShort());
} else if (response.getStatusCode() == StatusCode.CLIENT_METHOD_NOT_ALLOWED) {
LOGGER.warn("callOperation: Operation {} error executing operation: {}", operation.getIdShort(), response.getStatusCode());
throw new StatusException(StatusCodes.Bad_NotExecutable);
} else {
LOGGER.warn("callOperation: Operation {} error executing operation: {}", operation.getIdShort(), response.getStatusCode());
throw new StatusException(StatusCodes.Bad_UnexpectedError);
}
outputArguments = response.getPayload().getOutputArguments();
} catch (Exception e) {
LOGGER.error("callOperation error", e);
throw e;
}
return outputArguments;
}
use of com.prosysopc.ua.StatusException in project FAAAST-Service by FraunhoferIOSB.
the class AasServiceMethodManagerListener method onCall.
/**
* Callback method when a method was called
*
* @param serviceContext the current service context
* @param objectId the ID of the node whose method is being called
* @param object the object node whose method is being called, if available
* @param methodId the ID of the method being called
* @param method the method node being called, if available
* @param inputArguments input argument values
* @param inputArgumentResults argument errors. If errors in the values are
* encountered.
* @param inputArgumentDiagnosticInfos diagnostic info, in case of errors.
* @param outputs output values. The array is pre-created, just fill in the
* values.
* @return true if you handle the call, which prevents any other handler
* being called.
* @throws StatusException if there are errors in the method handling. For
* example, if you set inputArgumentResults, you should throw a
* StatusException with StatusCodes.Bad_InvalidArgument
*/
@Override
public boolean onCall(ServiceContext serviceContext, NodeId objectId, UaNode object, NodeId methodId, UaMethod method, Variant[] inputArguments, StatusCode[] inputArgumentResults, DiagnosticInfo[] inputArgumentDiagnosticInfos, Variant[] outputs) throws StatusException {
boolean retval = false;
// Handle method calls
// Note that the outputs array is already allocated
LOGGER.info("onCall: method {}: called. InputArguments: {}", methodId, inputArguments);
try {
if (endpoint == null) {
LOGGER.warn("onCall: no Endpoint available");
} else {
SubmodelElementData data = nodeManager.getAasData(objectId);
Operation aasOper = (Operation) data.getSubmodelElement();
if (aasOper != null) {
List<OperationVariable> inputVariables = aasOper.getInputVariables();
ValueConverter.setOperationValues(inputVariables, inputArguments);
List<OperationVariable> outputVariables = endpoint.callOperation(aasOper, inputVariables, data.getSubmodel(), data.getReference());
ValueConverter.setOutputArguments(outputVariables, outputs);
retval = true;
} else {
LOGGER.info("onCall: Property for {} not found", objectId);
}
}
} catch (StatusException se) {
LOGGER.error("onCall StatusException", se);
throw se;
} catch (Exception ex) {
LOGGER.error("onCall Exception", ex);
throw new StatusException(ex.getMessage(), StatusCodes.Bad_UnexpectedError);
}
return retval;
}
Aggregations