Search in sources :

Example 1 with StatusCode

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());
}
Also used : Variant(org.eclipse.milo.opcua.stack.core.types.builtin.Variant) UaVariableNode(org.eclipse.milo.opcua.sdk.client.nodes.UaVariableNode) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId) StatusCode(org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode) Test(org.junit.jupiter.api.Test) AbstractClientServerTest(org.eclipse.milo.opcua.sdk.test.AbstractClientServerTest)

Example 2 with StatusCode

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);
}
Also used : HistoryData(org.eclipse.milo.opcua.stack.core.types.structured.HistoryData) HistoryReadValueId(org.eclipse.milo.opcua.stack.core.types.structured.HistoryReadValueId) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) HistoryReadDetails(org.eclipse.milo.opcua.stack.core.types.structured.HistoryReadDetails) ArrayList(java.util.ArrayList) ReadRawModifiedDetails(org.eclipse.milo.opcua.stack.core.types.structured.ReadRawModifiedDetails) StatusCode(org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode) HistoryReadResult(org.eclipse.milo.opcua.stack.core.types.structured.HistoryReadResult) HistoryReadResponse(org.eclipse.milo.opcua.stack.core.types.structured.HistoryReadResponse) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId)

Example 3 with StatusCode

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;
        }
    });
}
Also used : Variant(org.eclipse.milo.opcua.stack.core.types.builtin.Variant) CompletableFuture(java.util.concurrent.CompletableFuture) UaException(org.eclipse.milo.opcua.stack.core.UaException) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId) CallMethodRequest(org.eclipse.milo.opcua.stack.core.types.structured.CallMethodRequest) StatusCode(org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode)

Example 4 with StatusCode

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);
    }
}
Also used : UInteger(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger) NodeClass(org.eclipse.milo.opcua.stack.core.types.enumerated.NodeClass) StatusCode(org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode)

Example 5 with StatusCode

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));
    }
}
Also used : NodeClass(org.eclipse.milo.opcua.stack.core.types.enumerated.NodeClass) UaMethodNode(org.eclipse.milo.opcua.sdk.client.nodes.UaMethodNode) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) UaException(org.eclipse.milo.opcua.stack.core.UaException) QualifiedName(org.eclipse.milo.opcua.stack.core.types.builtin.QualifiedName) UInteger(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger) StatusCode(org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode) LocalizedText(org.eclipse.milo.opcua.stack.core.types.builtin.LocalizedText)

Aggregations

StatusCode (org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode)130 UaException (org.eclipse.milo.opcua.stack.core.UaException)88 DataValue (org.eclipse.milo.opcua.stack.core.types.builtin.DataValue)78 Variant (org.eclipse.milo.opcua.stack.core.types.builtin.Variant)41 UInteger (org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger)36 NodeId (org.eclipse.milo.opcua.stack.core.types.builtin.NodeId)21 Unsigned.uint (org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint)19 List (java.util.List)15 LocalizedText (org.eclipse.milo.opcua.stack.core.types.builtin.LocalizedText)15 QualifiedName (org.eclipse.milo.opcua.stack.core.types.builtin.QualifiedName)15 NodeClass (org.eclipse.milo.opcua.stack.core.types.enumerated.NodeClass)12 CompletableFuture (java.util.concurrent.CompletableFuture)11 OpcUaClient (org.eclipse.milo.opcua.sdk.client.OpcUaClient)10 StatusCodes (org.eclipse.milo.opcua.stack.core.StatusCodes)10 ByteString (org.eclipse.milo.opcua.stack.core.types.builtin.ByteString)10 UByte (org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UByte)10 ArrayList (java.util.ArrayList)9 ResponseHeader (org.eclipse.milo.opcua.stack.core.types.structured.ResponseHeader)9 ExecutionException (java.util.concurrent.ExecutionException)8 ExpandedNodeId (org.eclipse.milo.opcua.stack.core.types.builtin.ExpandedNodeId)8