use of org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode in project milo by eclipse.
the class UaNodeTest method write.
@Test
public void write() throws UaException {
AddressSpace addressSpace = client.getAddressSpace();
UaVariableNode testNode = (UaVariableNode) addressSpace.getNode(new NodeId(2, "TestInt32"));
Integer i1 = (Integer) testNode.readValue().getValue().getValue();
testNode.writeValue(new Variant(i1 + 1));
Integer i2 = (Integer) testNode.readValue().getValue().getValue();
assertEquals(i1 + 1, i2);
StatusCode statusCode = testNode.writeAttribute(AttributeId.Value, DataValue.valueOnly(new Variant(42)));
assertTrue(statusCode.isGood());
}
use of org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode in project milo by eclipse.
the class HistoryReadExampleProsys method run.
@Override
public void run(OpcUaClient client, CompletableFuture<OpcUaClient> future) throws Exception {
client.connect().get();
HistoryReadDetails historyReadDetails = new ReadRawModifiedDetails(false, DateTime.MIN_VALUE, DateTime.now(), uint(0), true);
HistoryReadValueId historyReadValueId = new HistoryReadValueId(new NodeId(3, "Counter"), null, QualifiedName.NULL_VALUE, ByteString.NULL_VALUE);
List<HistoryReadValueId> nodesToRead = new ArrayList<>();
nodesToRead.add(historyReadValueId);
HistoryReadResponse historyReadResponse = client.historyRead(historyReadDetails, TimestampsToReturn.Both, false, nodesToRead).get();
HistoryReadResult[] historyReadResults = historyReadResponse.getResults();
if (historyReadResults != null) {
HistoryReadResult historyReadResult = historyReadResults[0];
StatusCode statusCode = historyReadResult.getStatusCode();
if (statusCode.isGood()) {
HistoryData historyData = (HistoryData) historyReadResult.getHistoryData().decode(client.getStaticSerializationContext());
List<DataValue> dataValues = l(historyData.getDataValues());
dataValues.forEach(v -> System.out.println("value=" + v));
} else {
System.out.println("History read failed: " + statusCode);
}
}
future.complete(client);
}
use of org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode in project milo by eclipse.
the class MethodExample method sqrt.
private CompletableFuture<Double> sqrt(OpcUaClient client, Double input) {
NodeId objectId = NodeId.parse("ns=2;s=HelloWorld");
NodeId methodId = NodeId.parse("ns=2;s=HelloWorld/sqrt(x)");
CallMethodRequest request = new CallMethodRequest(objectId, methodId, new Variant[] { new Variant(input) });
return client.call(request).thenCompose(result -> {
StatusCode statusCode = result.getStatusCode();
if (statusCode.isGood()) {
Double value = (Double) l(result.getOutputArguments()).get(0).getValue();
return CompletableFuture.completedFuture(value);
} else {
StatusCode[] inputArgumentResults = result.getInputArgumentResults();
for (int i = 0; i < inputArgumentResults.length; i++) {
logger.error("inputArgumentResults[{}]={}", i, inputArgumentResults[i]);
}
CompletableFuture<Double> f = new CompletableFuture<>();
f.completeExceptionally(new UaException(statusCode));
return f;
}
});
}
use of org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode in project milo by eclipse.
the class AddressSpace method createNodeFromBaseAttributes.
private CompletableFuture<? extends UaNode> createNodeFromBaseAttributes(NodeId nodeId, List<DataValue> baseAttributeValues) {
StatusCode nodeIdStatusCode = baseAttributeValues.get(0).getStatusCode();
if (nodeIdStatusCode != null && nodeIdStatusCode.isBad()) {
return failedUaFuture(nodeIdStatusCode);
}
Integer nodeClassValue = (Integer) baseAttributeValues.get(1).getValue().getValue();
if (nodeClassValue == null) {
return failedUaFuture(StatusCodes.Bad_NodeClassInvalid);
}
NodeClass nodeClass = NodeClass.from(nodeClassValue);
if (nodeClass == null) {
return failedUaFuture(StatusCodes.Bad_NodeClassInvalid);
}
switch(nodeClass) {
case DataType:
return createDataTypeNodeFromBaseAttributes(nodeId, baseAttributeValues);
case Method:
return createMethodNodeFromBaseAttributes(nodeId, baseAttributeValues);
case Object:
return createObjectNodeFromBaseAttributes(nodeId, baseAttributeValues);
case ObjectType:
return createObjectTypeNodeFromBaseAttributes(nodeId, baseAttributeValues);
case ReferenceType:
return createReferenceTypeNodeFromBaseAttributes(nodeId, baseAttributeValues);
case Variable:
return createVariableNodeFromBaseAttributes(nodeId, baseAttributeValues);
case VariableType:
return createVariableTypeNodeFromBaseAttributes(nodeId, baseAttributeValues);
case View:
return createViewNodeFromBaseAttributes(nodeId, baseAttributeValues);
default:
throw new IllegalArgumentException("NodeClass: " + nodeClass);
}
}
use of org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode in project milo by eclipse.
the class AddressSpace method newMethodNode.
private UaMethodNode newMethodNode(NodeId nodeId, List<DataValue> attributeValues) throws UaException {
DataValue nodeIdDataValue = attributeValues.get(0);
StatusCode nodeIdStatusCode = nodeIdDataValue.getStatusCode();
if (nodeIdStatusCode != null && nodeIdStatusCode.isBad()) {
throw new UaException(nodeIdStatusCode);
}
try {
NodeClass nodeClass = NodeClass.from((Integer) attributeValues.get(1).getValue().getValue());
Preconditions.checkArgument(nodeClass == NodeClass.Method, "expected NodeClass.Method, got NodeClass." + nodeClass);
QualifiedName browseName = (QualifiedName) attributeValues.get(2).getValue().getValue();
LocalizedText displayName = (LocalizedText) attributeValues.get(3).getValue().getValue();
LocalizedText description = getAttributeOrNull(attributeValues.get(4), LocalizedText.class);
UInteger writeMask = getAttributeOrNull(attributeValues.get(5), UInteger.class);
UInteger userWriteMask = getAttributeOrNull(attributeValues.get(6), UInteger.class);
Boolean executable = (Boolean) attributeValues.get(7).getValue().getValue();
Boolean userExecutable = (Boolean) attributeValues.get(8).getValue().getValue();
return new UaMethodNode(client, nodeId, nodeClass, browseName, displayName, description, writeMask, userWriteMask, executable, userExecutable);
} catch (Throwable t) {
throw UaException.extract(t).orElse(new UaException(StatusCodes.Bad_UnexpectedError, t));
}
}
Aggregations