Created
March 24, 2016 20:21
-
-
Save coussej/49205e23c4d764d0380f to your computer and use it in GitHub Desktop.
Example with deadband on timestamp not working.
This file contains hidden or 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 = require("node-opcua"); | |
var async = require("async"); | |
var config = { | |
"server": "opc.tcp://opcua.demo-this.com:51210/UA/SampleServer", | |
"tags": [{ | |
// "name": "An Int32", | |
// "nodeId": "ns=2;i=10849", | |
// "updateInterval": 100 | |
// }, { | |
// "name": "A String",c | |
// "nodeId": "ns=2;i=10855", | |
// "updateInterval": 3000 | |
// }, { | |
"name": "An Int16", | |
"nodeId": "ns=2;i=10219", | |
"updateInterval": 1000 | |
}] | |
}; | |
var client = new opcua.OPCUAClient(); | |
var the_session, the_subscription; | |
async.series([ | |
// step 1 : connect to | |
function(callback) { | |
var endpointURL = config.server; | |
client.connect(endpointURL, function(err) { | |
if (err) { | |
console.log("Cannot connect to endpoint [", endpointURL, "]."); | |
} else { | |
console.log("Succesfully connected to server [", endpointURL, "]!"); | |
} | |
callback(err); | |
}); | |
}, | |
// step 2 : createSession | |
function(callback) { | |
client.createSession(function(err, session) { | |
if (!err) { | |
the_session = session; | |
} | |
callback(err); | |
}); | |
}, | |
// step 4' : check if all the specified nodes are readable. | |
function(callback) { | |
var max_age = 0; | |
var nodes_to_read = []; | |
config.tags.forEach(function(tag) { | |
var node = { | |
nodeId: tag.nodeId, | |
attributeId: opcua.AttributeIds.Value | |
}; | |
nodes_to_read.push(node); | |
}); | |
the_session.read(nodes_to_read, max_age, function(err, nodes_to_read, dataValues) { | |
if (!err) { | |
dataValues.forEach(function(datavalue, i) { | |
var sc = datavalue.statusCode | |
if (sc.value == 0) { | |
console.log("Tag [", config.tags[i].name , "] verified. Value = [", datavalue.value.value, "]."); | |
} else { | |
console.log("Tag [", config.tags[i].name , "] could not be read. Status = [", sc.name, "], Description = [", sc.description, "]."); | |
} | |
}) | |
} | |
callback(err); | |
}); | |
}, | |
// step 5: install a subscription and install a monitored item for X seconds | |
function(callback) { | |
the_subscription = new opcua.ClientSubscription(the_session, { | |
requestedPublishingInterval: 1000, | |
requestedLifetimeCount: 10, | |
requestedMaxKeepAliveCount: 2, | |
maxNotificationsPerPublish: 1, | |
publishingEnabled: true, | |
priority: 10 | |
}); | |
the_subscription.on("started", function() { | |
console.log("subscription started for 2 seconds - subscriptionId=", the_subscription.subscriptionId); | |
}).on("keepalive", function() { | |
console.log("keepalive"); | |
}).on("terminated", function() { | |
callback(); | |
}); | |
setTimeout(function() { | |
the_subscription.terminate(); | |
}, 7200000); | |
config.tags.forEach(function(tag) { | |
// install monitored item | |
var monitoredItem = the_subscription.monitor({ | |
nodeId: opcua.resolveNodeId(tag.nodeId), | |
attributeId: opcua.AttributeIds.Value | |
}, { | |
clienthandle: 13, | |
samplingInterval: tag.updateInterval, | |
filter: new opcua.subscription_service.DataChangeFilter({ | |
trigger: opcua.subscription_service.DataChangeTrigger.StatusValueTimestamp, | |
deadbandType: opcua.subscription_service.DeadbandType.Absolute, | |
deadbandValue: 5000 | |
}), | |
discardOldest: true, | |
queueSize: 10 | |
}, | |
opcua.read_service.TimestampsToReturn.Both | |
, function(err){ | |
console.log("ERR ", err); | |
} | |
); | |
monitoredItem.on("changed", function(dataValue) { | |
var value = { | |
"value": dataValue.value.value, | |
"time": dataValue.sourceTimestamp.getTime() | |
}; | |
var tags = { | |
"opcstatus": dataValue.statusCode.value | |
}; | |
console.log(tag.name, value); | |
}); | |
monitoredItem.on("err", function (err_message) { | |
console.log(monitoredItem.itemToMonitor.nodeId.toString(), " ERROR".red, err_message); | |
}); | |
tag.monitoredItem = monitoredItem; | |
}); | |
}, | |
// 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