Skip to content

Instantly share code, notes, and snippets.

@mstfldmr
Created June 9, 2020 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mstfldmr/7353695f5b388077d626f9d8601d7074 to your computer and use it in GitHub Desktop.
Save mstfldmr/7353695f5b388077d626f9d8601d7074 to your computer and use it in GitHub Desktop.
Subscribe to Kepware with node-opcua
//Source: https://github.com/node-opcua/node-opcua/wiki/2.0.0-breaking-changes
const {
OPCUAClient,
resolveNodeId,
AttributeIds,
ClientSubscription,
ClientMonitoredItem,
TimestampsToReturn
} = require("node-opcua");
const async = require("async");
const opcua_username = "user";
const opcua_password = "password";
const endpointUrl = "opc.tcp://:49320";
const nodeId = "ns=2;s=Simulation Examples.Functions.Ramp8"
let theSession = null;
let theSubscription = null;
const client = OPCUAClient.create({
endpoint_must_exist: false
});
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({
userName: opcua_username,
password: opcua_password
}, function(err, session) {
if (!err) {
theSession = session;
}
callback(err);
});
},
// step 4 : read a variable
/*
function(callback) {
theSession.readVariableValue(nodeId, function(err, dataValue) {
if (!err) {
console.log(" temperature = ", dataValue.toString());
}
callback(err);
})
},
*/
// step 5: install a subscription and monitored item
function(callback) {
// create subscription
theSubscription = ClientSubscription.create(theSession, {
requestedPublishingInterval: 500,
requestedLifetimeCount: 1000,
requestedMaxKeepAliveCount: 20,
maxNotificationsPerPublish: 10,
publishingEnabled: true,
priority: 10
});
theSubscription.on("started", function() {
console.log("subscription started - subscriptionId=", theSubscription.subscriptionId);
}).on("keepalive", function() {
console.log("keepalive");
}).on("terminated", function() {
callback();
});
// install monitored item
//
const monitoredItem = ClientMonitoredItem.create(theSubscription, {
nodeId: nodeId,
attributeId: AttributeIds.Value
}, {
samplingInterval: 500,
discardOldest: false,
queueSize: 10
},
TimestampsToReturn.Source
);
monitoredItem.on("initialized", () => {
console.log("initialized");
});
monitoredItem.on("changed", function(dataValue) {
//console.log("New Value = ", dataValue.value.value.toString());
console.log(dataValue);
});
setTimeout(function() {
theSubscription.terminate();
}, 10000);
},
// closing session
//
function(callback) {
console.log(" closing session");
theSession.close(function(err) {
console.log(" session closed");
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