Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
var opcua = require("node-opcua");
var async = require("async");
var request = require("request");
var session, subscription;
var client = new opcua.OPCUAClient();
var sensor = "opc.tcp://REMOTE-SENSOR-ADDRESS:4334/UA/resourcePath";
var receiver = "http://REMOTE-SERVER-ADDRESS.com:8080";
async.series(
[
// establishing connection
function (cb) {
client.connect(sensor, function (err) {
if (err) {
console.log("Connection to " + sensor + "failed");
} else {
console.log("Connection successful");
}
cb(err);
});
},
// Adapted from node-opcua sample client
// https://github.com/node-opcua/node-opcua/blob/master/documentation/sample_client.js
var opcua = require("node-opcua");
var async = require("async");
var client = new opcua.OPCUAClient();
var endpointUrl = "opc.tcp://127.0.0.1:4855/";
var the_session, the_subscription;
var node_id = "ns=2;s=v1";
async.series([
// step 1 : connect to
callback => {
client.connect(endpointUrl, err => {
if (err) {
console.log(" cannot connect to endpoint :", endpointUrl);
} else {
console.log("connected !");
}
callback(err);
function OpcUAclient(_data, _logger, _events) {
var data = _data; // Current Device data { id, name, tags, enabled, ... }
var logger = _logger; // Logger
var working = false; // Working flag to manage overloading polling and connection
var connected = false; // Connected flag
var monitored = false; // Monitored flag
var lastStatus = ''; // Last Device status
var events = _events; // Events to commit change to runtime
var the_session;
var the_subscription = null;
var options = { connectionStrategy: { maxRetry: 1 }, keepSessionAlive: true }; // Connections options
var client = new opcua.OPCUAClient(options);
const attributeKeys = Object.keys(opcua.AttributeIds).filter((x) => x === 'DataType' || x === 'AccessLevel' || x === 'UserAccessLevel');//x !== "INVALID" && x[0].match(/[a-zA-Z]/));
var varsValue = []; // Signale to send to frontend { id, type, value }
var daqInterval = 0; // To manage minimum interval to save a DAQ value
var lastDaqInterval = 0; // To manage minimum interval to save a DAQ value
var getProperty = null; // Function to ask property (security)
/**
* Connect the client to OPC UA server
* Open Session, Create Subscription, Emit connection status, Clear the memory Tags value
*/
this.connect = function () {
return new Promise(function (resolve, reject) {
if (!_checkWorking(true)) {
reject();
} else {
const options = {
endpoint_must_exist: false,
keepSessionAlive: true,
connectionStrategy: {
maxRetry: 10,
initialDelay: 2000,
maxDelay: 10*1000
},
certificateFile: certificate_file,
privateKeyFile: certificate_privatekey_file
};
client = new opcua.OPCUAClient(options);
console.log(" connecting to = ".cyan, endpointUrl.cyan.bold);
console.log(" strategy = ".cyan, client.connectionStrategy);
console.log("client certificate = ".cyan, client.certificateFile);
console.log("client private key = ".cyan, client.privateKeyFile);
client.connect(endpointUrl, callback);
client.on("backoff", function (number, delay) {
console.log("backoff attempt #".bgWhite.yellow,number, " retrying in ",delay/1000.0," seconds");
});
},
defaultSecureTokenLifetime: 40000,
endpoint_must_exist:false,
connectionStrategy: {
maxRetry: 10,
initialDelay: 2000,
maxDelay: 10*1000
},
certificateFile: certificate_file,
privateKeyFile: certificate_privatekey_file
};
console.log("Options = ", options.securityMode.toString(), options.securityPolicy.toString());
client = new opcua.OPCUAClient(options);
console.log(" reconnecting to ", endpointUrl.cyan.bold);
client.connect(endpointUrl, callback);
},
var opts = {
endpoint_must_exist: false,
keepSessionAlive: true,
connectionStrategy: { maxRetry: 1 } };
if (property.mode) {
if (property.mode.securityMode) {
opts['securityMode'] = property.mode.securityMode;
}
if (property.mode.securityPolicy) {
opts['securityPolicy'] = property.mode.securityPolicy;
}
if (property.mode.securityPolicy) {
opts['securityPolicy'] = property.mode.securityPolicy;
}
}
client = new opcua.OPCUAClient(opts);
}
callback();
}).catch(function (err) {
callback(err);
function ReadPump(config, measurements, writepump) {
this.uaServerUrl = config.url;
this.uaClient = new opcua.OPCUAClient();
this.uaSession;
this.uaSubscription;
this.measurements = measurements;
this.polledMeasurements = [];
this.monitoredMeasurements = [];
this.writepump = writepump;
this.poller;
}
return new Promise(function (resolve, reject) {
let opts = { connectionStrategy: { maxRetry: 1 } };
let client = new opcua.OPCUAClient(opts);
try {
client.connect(endpointUrl, function (err) {
if (err) {
reject('getendpoints-connect-error: ' + err.message);
} else {
const endpoints = client.getEndpoints().then(endpoints => {
const reducedEndpoints = endpoints.map(endpoint => ({
endpointUrl: endpoint.endpointUrl,
securityMode: endpoint.securityMode.toString(),
securityPolicy: endpoint.securityPolicyUri.toString(),
}));
resolve( reducedEndpoints);
client.disconnect();
}, reason => {
reject('getendpoints-error: ' + reason);
client.disconnect();