Created
April 15, 2018 18:52
-
-
Save erossignon/2e9e7519fc16720f3eeb1cef9b6bfeac to your computer and use it in GitHub Desktop.
client reconnection tester
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 */ | |
/// issues: #237 #288 | |
const opcua = require("node-opcua"); | |
const async = require("async"); | |
const _ = require("underscore"); | |
const client = new opcua.OPCUAClient({ | |
endpoint_must_exist: false, | |
keepSessionAlive: true, | |
requestedSessionTimeout: 60000, | |
}); | |
//const endpointUrl = "opc.tcp://" + require("os").hostname() + ":26543"; | |
//const nodeId = opcua.coerceNodeId("ns=411;s=Scalar_Static_Int32"); | |
const endpointUrl = "opc.tcp://" + require("os").hostname() + ":53530/OPCUA/SimulationServer"; | |
const nodeId = opcua.coerceNodeId("ns=3;s=Int32"); | |
const doDebug = true; | |
let the_session, the_subscription,monitoredItem; | |
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); | |
}); | |
client.on("connection_reestablished",function() { | |
console.log(" !!!!!!!!!!!!!!!!!!!!!!!! CONNECTION RE-ESTABLISHED !!!!!!!!!!!!!!!!!!!".bgWhite.red); | |
}); | |
client.on("backoff", function (number, delay) { | |
console.log("backoff attempt #".bgWhite.yellow,number, " retrying in ",delay/1000.0," seconds"); | |
}); | |
}, | |
// step 2 : createSession | |
function(callback) { | |
client.createSession( function(err,session) { | |
if(!err) { | |
the_session = session; | |
} | |
console.log("session timeout = ",session.timeout); | |
the_session.on("keepalive",function(state) { | |
console.log("KeepAlive state=".yellow,state.toString()," pending request on server = ".yellow, the_subscription.publish_engine.nbPendingPublishRequests); | |
}); | |
the_session.on("session_closed" ,function(statusCode) { | |
console.log("Session has closed : statusCode = ".yellow, statusCode ? statusCode.toString() : "????"); | |
}); | |
callback(err); | |
}); | |
}, | |
function(callback) { | |
// create a subscription | |
const parameters = { | |
requestedPublishingInterval: 100, | |
requestedLifetimeCount: 1000, | |
requestedMaxKeepAliveCount: 12, | |
maxNotificationsPerPublish: 10, | |
publishingEnabled: true, | |
priority: 10 | |
}; | |
the_subscription = new opcua.ClientSubscription(the_session, parameters); | |
function getTick() { | |
return Date.now(); | |
} | |
const t = getTick(); | |
the_subscription.on("started", function () { | |
console.log("started subscription :", the_subscription.subscriptionId); | |
console.log(" revised parameters "); | |
console.log(" revised maxKeepAliveCount ", the_subscription.maxKeepAliveCount, " ( requested ", parameters.requestedMaxKeepAliveCount + ")"); | |
console.log(" revised lifetimeCount ", the_subscription.lifetimeCount, " ( requested ", parameters.requestedLifetimeCount + ")"); | |
console.log(" revised publishingInterval ", the_subscription.publishingInterval, " ( requested ", parameters.requestedPublishingInterval + ")"); | |
console.log(" suggested timeout hint ", the_subscription.publish_engine.timeoutHint); | |
callback(); | |
}).on("internal_error", function (err) { | |
console.log(" received internal error", err.message); | |
}).on("keepalive", function () { | |
const t1 = getTick(); | |
console.log("keepalive ".cyan, " pending request on server = ".cyan, the_subscription.publish_engine.nbPendingPublishRequests); | |
}).on("terminated", function (err) { | |
}); | |
}, | |
function client_create_monitoredItem(callback) { | |
const result = []; | |
const requestedParameters= { | |
samplingInterval: 250, | |
queueSize: 1, | |
discardOldest: true | |
}; | |
const item ={nodeId:nodeId, attributeId: opcua.AttributeIds.Value}; | |
monitoredItem = the_subscription.monitor(item,requestedParameters,opcua.read_service.TimestampsToReturn.Both,function(err){ | |
console.log("err",err); | |
}); | |
monitoredItem.on("err",function(errMessage) { | |
callback(new Error(errMessage)); | |
}); | |
monitoredItem.on("changed",function(dataValue){ | |
if (doDebug) { | |
console.log(" ||||||||||| VALUE CHANGED !!!!".cyan,dataValue.statusCode.toString(),dataValue.value.toString()); | |
} | |
result.push(dataValue); | |
}); | |
monitoredItem.on("initialized", function () { | |
if (doDebug) { | |
console.log(" MonitoredItem initialized"); | |
} | |
callback(); | |
}); | |
}, | |
function(callback) { | |
let counter = 0; | |
const intervalId = setInterval(function() { | |
console.log(" Session OK ? ", the_session.isChannelValid() , | |
"session will expired in ", the_session.evaluateRemainingLifetime()/1000, " seconds", | |
"subscripiton will expirer in ".red,the_subscription.evaluateRemainingLifetime()/1000, " seconds", | |
"subscripiton?".red,the_session.subscriptionCount); | |
if (!the_session.isChannelValid() && false) { | |
//xx console.log(the_session.toString()); | |
return; // ignore write as session is invalid for the time being | |
} | |
let nodeToWrite = { | |
nodeId: nodeId, | |
attributeId: opcua.AttributeIds.Value, | |
value: /* DataValue */{ | |
statusCode: opcua.StatusCodes.Good, | |
sourceTimestamp: new Date(), | |
value: /* Variant */{ | |
dataType: opcua.DataType.Int32, | |
value: counter | |
} | |
} | |
}; | |
console.log("writing ",counter); | |
the_session.write([nodeToWrite],function(err,statusCode) { | |
counter += 1; | |
if(err) { | |
console.log("result",err.message); | |
} | |
//xx tatusCode && statusCode.length===1) ? statusCode[0].toString():""); | |
}); | |
},1000); | |
setTimeout(function(){ | |
clearInterval(intervalId); | |
callback(); | |
},200000000); | |
}, | |
// 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