Last active
March 24, 2016 18:33
-
-
Save erossignon/4447bace6ba261ab0c4e 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 the_session, the_subscription; | |
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) { | |
setTimeout(callback,2000); | |
}, | |
// install a subscription and install a monitored item for 10 seconds | |
function(callback) { | |
the_subscription = new opcua.ClientSubscription(the_session,{ | |
requestedPublishingInterval: 1000, | |
requestedLifetimeCount: 1000, | |
requestedMaxKeepAliveCount: 20, | |
maxNotificationsPerPublish: 100, | |
publishingEnabled: true, | |
priority: 10 | |
}); | |
the_subscription.on("started",function(){ | |
console.log("subscription started for 10 seconds - subscriptionId=",the_subscription.subscriptionId); | |
}).on("keepalive",function(){ | |
console.log("keepalive"); | |
}).on("terminated",function(){ | |
callback(); | |
}); | |
setTimeout(function(){ | |
the_subscription.terminate(); | |
},10000); | |
var filter = new opcua.subscription_service.DataChangeFilter({ | |
trigger: opcua.subscription_service.DataChangeTrigger.StatusValueTimestamp, | |
deadbandType: opcua.subscription_service.DeadbandType.Absolute, | |
deadbandValue: 1.0 | |
}); | |
// install monitored item | |
var monitoredItem = the_subscription.monitor({ | |
// nodeId: opcua.resolveNodeId("ns=1,i=18520"), <<< ERROR HERE => , should be ; + ns=1 shall be ns=2 | |
nodeId: "ns=2;i=10854", | |
attributeId: opcua.AttributeIds.Value | |
}, | |
{ | |
clientHandle: 13, | |
samplingInterval: 100, | |
discardOldest: false, | |
queueSize: 10000, | |
filter: filter | |
}, | |
opcua.read_service.TimestampsToReturn.Both | |
,function(err){ | |
console.log(" ERR =",err); | |
}); | |
console.log("-------------------------------------"); | |
monitoredItem.on("initialized", function () { | |
console.log("monitoredItem initialized"); | |
}); | |
monitoredItem.on("changed",function(dataValue){ | |
console.log(" value = ",dataValue.value.toString()); | |
}); | |
monitoredItem.on("err", function (err_message) { | |
console.log(monitoredItem.itemToMonitor.nodeId.toString(), " ERROR".red, err_message); | |
}); | |
}, | |
// 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