Created
March 24, 2016 18:56
-
-
Save erossignon/b471f709a7255c168a52 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*global require,console,setTimeout */ | |
var opcua; | |
try{ | |
opcua = require("node-opcua"); | |
} catch(err) { | |
opcua = require(".."); | |
} | |
var async = require("async"); | |
var client = new opcua.OPCUAClient(); | |
//var endpointUrl = "opc.tcp://opcua.demo-this.com:51210/UA/SampleServer"; | |
var endpointUrl = "opc.tcp://localhost:53530/OPCUA/SimulationServer"; | |
var nodeId1 = "ns=2;s=MyLevel"; | |
var nodeId2 = "ns=2;s=MySwitch"; | |
var tags = [nodeId1, nodeId2]; | |
var the_session, the_subscription; | |
var date2 = new Date(Date.now()); | |
var date1 = new Date(); | |
date1.setTime(date2.getTime() - 3000); // 3 sec history | |
console.log("start =",date1.toISOString()); | |
console.log("end =",date2.toISOString()); | |
async.series([ | |
// step 1 : connect to | |
function(callback) { | |
client.connect(endpointUrl,function (err) { | |
if(err) { | |
console.log(" cannot connect to endpoint :" , endpointUrl ); | |
} else { | |
console.log("connected !"); | |
} | |
callback(err); | |
}); | |
}, | |
// step 2 : createSession | |
function(callback) { | |
client.createSession( function(err,session) { | |
if(!err) { | |
the_session = session; | |
} | |
callback(err); | |
}); | |
}, | |
function (callback) { | |
var tags1 = tags; | |
the_session.readHistoryValue(tags1, date1,date2, function (err, dataValues, diagnostics) { | |
if (!err) { | |
console.log(tags1[0].toString(),dataValues[0].toString()); | |
console.log(tags1[1].toString(),dataValues[1].toString()); | |
} | |
callback(err); | |
}); | |
}, | |
function (callback) { | |
var tags1 = tags.reverse(); | |
the_session.readHistoryValue(tags1,date1,date2, function(err, dataValues, diagnostics) { | |
if (!err) { | |
console.log(tags1[0].toString(),dataValues[0].toString()); | |
console.log(tags1[1].toString(),dataValues[1].toString()); | |
} | |
callback(err); | |
}); | |
}, | |
// close session | |
function(callback) { | |
the_session.close(function(err){ | |
if(err) { | |
console.log("session closed failed ?"); | |
} | |
callback(); | |
}); | |
} | |
], | |
function(err) { | |
if (err) { | |
console.log(" failure ",err); | |
} else { | |
console.log("done!"); | |
} | |
client.disconnect(function(){}); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment