Search in sources :

Example 6 with UaVariableNode

use of org.eclipse.milo.opcua.sdk.client.nodes.UaVariableNode in project iot-tree by bambooww.

the class ConnPtOPCUA method writeUaNodeTreeJson.

public void writeUaNodeTreeJson(Writer w, boolean b_var, boolean force_refresh) throws Exception {
    if (uaClient == null) {
        throw new Exception("no UaClient connected");
    }
    // find /Root/Objects nodes
    UaNode root = findUaNodeByPath(uaClient, new String[] { "Objects" });
    if (root == null)
        throw new Exception("no /Root/Objects/ node found");
    // UaObjectNode uaou= new UaObjectNode(
    // uaClient,
    // getNodeId(),
    // getBrowseName(),
    // getDisplayName(),
    // getDescription(),
    // getWriteMask(),
    // getUserWriteMask(),
    // getEventNotifier()
    // );
    // NodeId nid = n.getNodeId() ;
    w.write("{\"id\":\"" + UUID.randomUUID().toString() + "\"");
    w.write(",\"nc\":0");
    w.write(",\"icon\": \"fa fa-sitemap fa-lg\"");
    w.write(",\"text\":\"" + this.getOpcEndPointURI() + "\"");
    w.write(",\"state\": {\"opened\": true}");
    List<? extends UaNode> nodes = uaClient.getAddressSpace().browseNodes(root);
    if (nodes != null && nodes.size() > 0) {
        w.write(",\"children\":[");
        // 
        boolean bfirst = true;
        for (UaNode node : nodes) {
            String bn = node.getBrowseName().getName();
            if (bn.startsWith("_"))
                continue;
            if (!b_var && node instanceof UaVariableNode)
                continue;
            if (bfirst)
                bfirst = false;
            else
                w.write(',');
            writeUaNodeTreeJson(w, uaClient, node, b_var);
        }
        w.write("]");
    }
    w.write("}");
// writeUaNodeTreeJson(w,uaClient,root);
}
Also used : UaVariableNode(org.eclipse.milo.opcua.sdk.client.nodes.UaVariableNode) UaNode(org.eclipse.milo.opcua.sdk.client.nodes.UaNode) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) UaException(org.eclipse.milo.opcua.stack.core.UaException)

Example 7 with UaVariableNode

use of org.eclipse.milo.opcua.sdk.client.nodes.UaVariableNode in project iot-tree by bambooww.

the class ConnPtOPCUA method listBindBeSelectedItems.

private void listBindBeSelectedItems(List<BindItem> bis, OpcUaClient client, String ppath, UaNode n) throws Exception {
    NodeId nid = n.getNodeId();
    String bn = n.getBrowseName().getName();
    if (n instanceof UaVariableNode) {
        UaVariableNode uvn = (UaVariableNode) n;
        DataValue dataval = uvn.getValue();
        Object valob = dataval.getValue().getValue();
        if (valob != null && valob.getClass().isArray()) {
            Object[] objectArray = (Object[]) valob;
            valob = Arrays.deepToString(objectArray);
        }
        String val_dt = Convert.toFullYMDHMS(dataval.getSourceTime().getJavaDate());
        NodeId dt = uvn.getDataType();
        UaVariableTypeNode vtn = uvn.getTypeDefinition();
        UInteger[] arrdim = vtn.getArrayDimensions();
        UaDataTypeNode tpnode = (UaDataTypeNode) uaClient.getAddressSpace().getNode(dt);
        String datatp = tpnode.getBrowseName().getName();
        String arr_dim = "";
        if (arrdim != null && arrdim.length > 0) {
            arr_dim = Convert.combineWith(arrdim, ',');
        }
        BindItem bi = new BindItem(ppath + "/" + bn, datatp);
        bi.setVal(valob);
        bis.add(bi);
        return;
    }
    List<? extends UaNode> nodes = client.getAddressSpace().browseNodes(n);
    if (nodes != null && nodes.size() > 0) {
        if (Convert.isNullOrEmpty(ppath))
            ppath = bn;
        else
            ppath += "/" + bn;
        for (UaNode node : nodes) {
            String bn0 = node.getBrowseName().getName();
            if (bn0.startsWith("_"))
                continue;
            listBindBeSelectedItems(bis, client, ppath, node);
        }
    }
}
Also used : DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) BindItem(org.iottree.core.conn.ConnPtBinder.BindItem) UaNode(org.eclipse.milo.opcua.sdk.client.nodes.UaNode) UaDataTypeNode(org.eclipse.milo.opcua.sdk.client.nodes.UaDataTypeNode) UaVariableNode(org.eclipse.milo.opcua.sdk.client.nodes.UaVariableNode) UaVariableTypeNode(org.eclipse.milo.opcua.sdk.client.nodes.UaVariableTypeNode) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId) UInteger(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger) JSONObject(org.json.JSONObject)

Example 8 with UaVariableNode

use of org.eclipse.milo.opcua.sdk.client.nodes.UaVariableNode in project netxms by netxms.

the class ServerConnection method readNodeValue.

/**
 * Read value of given node from server
 *
 * @param nodeId node ID
 * @return node value
 * @throws Exception
 */
private String readNodeValue(NodeId nodeId) throws Exception {
    UaVariableNode node = session.getAddressSpace().getVariableNode(nodeId);
    DataValue value = node.readValue();
    return value.getValue().toString();
}
Also used : UaVariableNode(org.eclipse.milo.opcua.sdk.client.nodes.UaVariableNode) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue)

Example 9 with UaVariableNode

use of org.eclipse.milo.opcua.sdk.client.nodes.UaVariableNode in project netxms by netxms.

the class ServerConnection method writeNodeValue.

/**
 * @param nodeId
 * @param value
 * @return
 * @throws Exception
 */
private boolean writeNodeValue(NodeId nodeId, String newValue) throws Exception {
    UaVariableNode node = session.getAddressSpace().getVariableNode(nodeId);
    // Determine data type
    ExpandedNodeId dataType = dataTypeCache.get(nodeId);
    if (dataType == null) {
        Variant value = node.readValue().getValue();
        dataType = value.getDataType().get();
        if (dataType == null) {
            Platform.writeDebugLog(6, String.format("OPCUA: writeNodeValue(%s, \"%s\") for %s: cannot read data type", nodeId.toString(), value, url));
            return false;
        }
        Platform.writeDebugLog(6, String.format("OPCUA: writeNodeValue(%s, \"%s\") for %s: type %s", nodeId.toString(), value, url, dataType.toString()));
        dataTypeCache.put(nodeId, dataType);
    }
    Variant v;
    if (Identifiers.Boolean.equals(dataType)) {
        v = new Variant(Boolean.parseBoolean(newValue));
    } else if (Identifiers.Int32.equals(dataType) || Identifiers.UInt32.equals(dataType)) {
        v = new Variant(Integer.parseInt(newValue));
    } else if (Identifiers.Int64.equals(dataType) || Identifiers.UInt64.equals(dataType)) {
        v = new Variant(Long.parseLong(newValue));
    } else {
        v = new Variant(newValue);
    }
    CompletableFuture<List<StatusCode>> f = session.writeValues(ImmutableList.of(nodeId), ImmutableList.of(new DataValue(v)));
    StatusCode status = f.get().get(0);
    Platform.writeDebugLog(6, String.format("OPCUA: writeNodeValue(%s, \"%s\") for %s: status %s", nodeId.toString(), newValue, url, status.toString()));
    return status.isGood();
}
Also used : Variant(org.eclipse.milo.opcua.stack.core.types.builtin.Variant) ExpandedNodeId(org.eclipse.milo.opcua.stack.core.types.builtin.ExpandedNodeId) UaVariableNode(org.eclipse.milo.opcua.sdk.client.nodes.UaVariableNode) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) StatusCode(org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode)

Example 10 with UaVariableNode

use of org.eclipse.milo.opcua.sdk.client.nodes.UaVariableNode in project iot-tree by bambooww.

the class ConnPtOPCUA method writeBindBeSelectTreeNode.

private void writeBindBeSelectTreeNode(Writer w, UaNode n) throws Exception {
    // boolean bvar = n instanceof UaVariableNode;
    NodeId nid = n.getNodeId();
    w.write("{\"id\":\"" + nid.toParseableString() + "\"");
    w.write(",\"nc\":" + n.getNodeClass().getValue());
    if (n instanceof UaVariableNode) {
        w.write(",\"tp\": \"tag\"");
        w.write(",\"icon\": \"fa fa-tag fa-lg\"");
        // if("BooleanArray".contentEquals(node.getBrowseName().getName()))
        // {
        // System.out.println("1");
        // }
        UaVariableNode uvn = (UaVariableNode) n;
        DataValue dataval = uvn.getValue();
        Object valob = dataval.getValue().getValue();
        if (valob != null && valob.getClass().isArray()) {
            Object[] objectArray = (Object[]) valob;
            valob = Arrays.deepToString(objectArray);
        }
        String serverdt = Convert.toFullYMDHMS(dataval.getSourceTime().getJavaDate());
        w.write(",\"val\":\"" + valob + "\"");
        w.write(",\"val_dt\":\"" + serverdt + "\"");
        NodeId dt = uvn.getDataType();
        UaVariableTypeNode vtn = uvn.getTypeDefinition();
        UInteger[] arrdim = vtn.getArrayDimensions();
        UaDataTypeNode tpnode = (UaDataTypeNode) uaClient.getAddressSpace().getNode(dt);
        String vt = tpnode.getBrowseName().getName();
        w.write(",\"vt\":\"" + vt + "\"");
        if (arrdim != null && arrdim.length > 0) {
            w.write(",\"arr_dim\":\"" + Convert.combineWith(arrdim, ',') + "\"");
        }
        w.write(",\"text\":\"" + n.getDisplayName().getText() + ":" + vt + "\"}");
    } else {
        w.write(",\"tp\": \"tagg\"");
        w.write(",\"icon\": \"fa fa-folder fa-lg\"");
        w.write(",\"children\": true ");
        w.write(",\"text\":\"" + n.getDisplayName().getText() + "\"}");
    }
}
Also used : UaVariableNode(org.eclipse.milo.opcua.sdk.client.nodes.UaVariableNode) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) UaVariableTypeNode(org.eclipse.milo.opcua.sdk.client.nodes.UaVariableTypeNode) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId) UInteger(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger) JSONObject(org.json.JSONObject) UaDataTypeNode(org.eclipse.milo.opcua.sdk.client.nodes.UaDataTypeNode)

Aggregations

UaVariableNode (org.eclipse.milo.opcua.sdk.client.nodes.UaVariableNode)20 DataValue (org.eclipse.milo.opcua.stack.core.types.builtin.DataValue)12 NodeId (org.eclipse.milo.opcua.stack.core.types.builtin.NodeId)11 AbstractClientServerTest (org.eclipse.milo.opcua.sdk.test.AbstractClientServerTest)7 Test (org.junit.jupiter.api.Test)7 UaNode (org.eclipse.milo.opcua.sdk.client.nodes.UaNode)6 UaException (org.eclipse.milo.opcua.stack.core.UaException)5 Variant (org.eclipse.milo.opcua.stack.core.types.builtin.Variant)5 UaVariableTypeNode (org.eclipse.milo.opcua.sdk.client.nodes.UaVariableTypeNode)4 UInteger (org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger)4 UaDataTypeNode (org.eclipse.milo.opcua.sdk.client.nodes.UaDataTypeNode)3 ExpandedNodeId (org.eclipse.milo.opcua.stack.core.types.builtin.ExpandedNodeId)3 QualifiedName (org.eclipse.milo.opcua.stack.core.types.builtin.QualifiedName)3 StatusCode (org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode)3 JSONObject (org.json.JSONObject)3 IOException (java.io.IOException)2 ExecutionException (java.util.concurrent.ExecutionException)2 AttributeId (org.eclipse.milo.opcua.stack.core.AttributeId)2 ExtensionObject (org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject)2 ReadResponse (org.eclipse.milo.opcua.stack.core.types.structured.ReadResponse)2