Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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);
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement