Skip to content

Instantly share code, notes, and snippets.

@erossignon
Last active August 10, 2019 09:59
Show Gist options
  • Save erossignon/84e9ec090909da037401d4c29b51b85b to your computer and use it in GitHub Desktop.
Save erossignon/84e9ec090909da037401d4c29b51b85b to your computer and use it in GitHub Desktop.

Revisions

  1. erossignon revised this gist Aug 10, 2019. 1 changed file with 15 additions and 0 deletions.
    15 changes: 15 additions & 0 deletions package.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    {
    "name": "655",
    "version": "1.0.0",
    "description": "",
    "main": "issue65.js",
    "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
    },
    "author": "",
    "license": "ISC",
    "dependencies": {
    "async": "^3.1.0",
    "node-opcua": "^2.1.2"
    }
    }
  2. erossignon renamed this gist Aug 10, 2019. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. erossignon revised this gist Aug 10, 2019. 1 changed file with 3560 additions and 0 deletions.
    3,560 changes: 3,560 additions & 0 deletions Result
    3,560 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
  4. erossignon renamed this gist Aug 10, 2019. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. erossignon created this gist Aug 10, 2019.
    91 changes: 91 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,91 @@
    const { OPCUAClient , NodeCrawler} = require("node-opcua");
    const async = require("async");
    const util = require("util");

    const client = OPCUAClient.create({
    endpoint_must_exist: false
    });

    // UA Automation Ansi Demo Server
    const endpointUrl = "opc.tcp://localhost:48020";
    const nodeToExplore = "ns=4;s=Demo.Massfolder_Dynamic";
    const tree = {

    }


    function main(callback) {

    async.series([
    function (cb3) {

    client.connect(endpointUrl, function (err) {

    if (err) {
    console.log(" cannot connect to endpoint :", endpointUrl);
    } else {
    console.log("connected !");
    }
    cb3(err);

    });

    },
    function (cb3) {
    client.createSession(function (err, session) {
    if (err) {
    console.log("Failed to Create the Browse Session");
    }
    else {
    console.log("Browse Session Creates Session Successfully!");
    the_session = session;
    }
    cb3(err);
    });

    },
    function (cb3) {
    var crawler = new NodeCrawler(the_session);
    var nodeId = nodeToExplore
    console.log("Browsing Data in the server from "+ nodeToExplore.toString());
    crawler.read(nodeId, function (err, obj) {
    if (err) {
    console.log("Failed to Read Data");
    }
    else {
    console.log("Data read from PLC is Successful");
    }
    cb3(err);
    });

    crawler.on("browsed", function (element) {

    const parentNode = (element.parent && element.parent.$node )? element.parent.$node : tree;
    const key = element.browseName.toString() + " " + element.nodeId.toString();
    // xxelement.nodeId.toString();
    if (!parentNode[key]) {
    parentNode[key] = { };
    }
    const n = parentNode[key];
    element.$node = n;
    console.log("element name:" + element.browseName.toString());
    });
    }
    ],
    function (err) {
    console.log(util.inspect(tree,{ depth: 100}));

    if (err) {
    console.log("Failure in opcua browsing!", err);
    callback(err)
    } else {
    console.log("No Error :)");
    callback();
    }
    console.log("disconnected");
    client.disconnect(function () { });
    });
    }
    main((err)=>{
    console.log("done",err);
    });