Skip to content

Commit

Permalink
Use ES6 arrow functions
Browse files Browse the repository at this point in the history
  • Loading branch information
locka99 committed Apr 18, 2019
1 parent 11fe2f0 commit 3d750cf
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 37 deletions.
42 changes: 21 additions & 21 deletions 3rd-party/node-opcua/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ var node_id = "ns=2;s=v1";

async.series([
// step 1 : connect to
function (callback) {
client.connect(endpointUrl, function (err) {
callback => {
client.connect(endpointUrl, err => {
if (err) {
console.log(" cannot connect to endpoint :", endpointUrl);
} else {
Expand All @@ -25,8 +25,8 @@ async.series([
},

// step 2 : createSession
function (callback) {
client.createSession(function (err, session) {
callback => {
client.createSession((err, session) => {
if (!err) {
the_session = session;
}
Expand All @@ -35,10 +35,10 @@ async.series([
},

// step 3 : browse
function (callback) {
the_session.browse("RootFolder", function (err, browse_result) {
callback => {
the_session.browse("RootFolder", (err, browse_result) => {
if (!err) {
browse_result[0].references.forEach(function (reference) {
browse_result[0].references.forEach(reference => {
console.log(reference.browseName.toString());
});
}
Expand All @@ -47,8 +47,8 @@ async.series([
},

// step 4 : read a variable with readVariableValue
function (callback) {
the_session.readVariableValue(node_id, function (err, dataValue) {
callback => {
the_session.readVariableValue(node_id, (err, dataValue) => {
if (!err) {
console.log(" free mem % = ", dataValue.toString());
}
Expand All @@ -59,12 +59,12 @@ async.series([
},

// step 4' : read a variable with read
function (callback) {
callback => {
var max_age = 0;
var nodes_to_read = [
{nodeId: node_id, attributeId: opcua.AttributeIds.Value}
];
the_session.read(nodes_to_read, max_age, function (err, nodes_to_read, dataValues) {
the_session.read(nodes_to_read, max_age, (err, nodes_to_read, dataValues) => {
if (!err) {
console.log(" free mem % = ", dataValues[0]);
}
Expand All @@ -73,7 +73,7 @@ async.series([
},

// step 5: install a subscription and install a monitored item for 10 seconds
function (callback) {
callback => {
the_subscription = new opcua.ClientSubscription(the_session, {
requestedPublishingInterval: 1000,
requestedLifetimeCount: 10,
Expand All @@ -83,15 +83,15 @@ async.series([
priority: 10
});

the_subscription.on("started", function () {
the_subscription.on("started", () => {
console.log("subscription started for 2 seconds - subscriptionId=", the_subscription.subscriptionId);
}).on("keepalive", function () {
}).on("keepalive", () => {
console.log("keepalive");
}).on("terminated", function () {
}).on("terminated", () => {
callback();
});

setTimeout(function () {
setTimeout(() => {
the_subscription.terminate();
}, 10000);

Expand All @@ -109,14 +109,14 @@ async.series([
);
console.log("-------------------------------------");

monitoredItem.on("changed", function (dataValue) {
monitoredItem.on("changed", dataValue => {
console.log(" % free mem = ", dataValue.value.value);
});
},

// close session
function (callback) {
the_session.close(function (err) {
callback => {
the_session.close(err => {
if (err) {
console.log("session closed failed ?");
}
Expand All @@ -125,12 +125,12 @@ async.series([
}

],
function (err) {
err => {
if (err) {
console.log(" failure ", err);
} else {
console.log("done!");
}
client.disconnect(function () {
client.disconnect(() => {
});
});
26 changes: 10 additions & 16 deletions 3rd-party/node-opcua/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var server = new opcua.OPCUAServer({

function post_initialize() {
console.log("initialized");

function construct_my_address_space(server) {

var addressSpace = server.engine.addressSpace;
Expand All @@ -36,9 +37,7 @@ function post_initialize() {
browseName: "v1",
dataType: "Int32",
value: {
get: function () {
return new opcua.Variant({dataType: opcua.DataType.Int32, value: v1});
}
get: () => new opcua.Variant({dataType: opcua.DataType.Int32, value: v1})
}
});

Expand All @@ -50,15 +49,13 @@ function post_initialize() {
browseName: "v2",
dataType: "Boolean",
value: {
get: function () {
return new opcua.Variant({dataType: opcua.DataType.Boolean, value: v2});
}
get: () => new opcua.Variant({dataType: opcua.DataType.Boolean, value: v2})
}
});


// emulate variable1 changing every 500 ms
setInterval(function () {
setInterval(() => {
v1 += 1;
v2 = !v2;
}, 250);
Expand All @@ -71,9 +68,7 @@ function post_initialize() {
browseName: "v3",
dataType: "String",
value: {
get: function () {
return new opcua.Variant({dataType: opcua.DataType.String, value: v3});
}
get: () => new opcua.Variant({dataType: opcua.DataType.String, value: v3})
}
});

Expand All @@ -84,26 +79,25 @@ function post_initialize() {
browseName: "v4",
dataType: "Double",
value: {
get: function () {
return new opcua.Variant({dataType: opcua.DataType.Double, value: v4});
}
get: () => new opcua.Variant({dataType: opcua.DataType.Double, value: v4})
}
});
// emulate variable1 changing every 500 ms
var slowCounter = 1;
setInterval(function () {
setInterval(() => {
slowCounter += 1;
v3 = "Hello world times " + slowCounter;
v4 = Math.sin((slowCounter % 360) * Math.PI / 180.0);
}, 1000);
}

construct_my_address_space(server);
server.start(function () {
server.start(() => {
console.log("Server is now listening on these endpoints... ( press CTRL+C to stop)");
server.endpoints[0].endpointDescriptions().forEach(function (endpoint) {
server.endpoints[0].endpointDescriptions().forEach(endpoint => {
console.log(endpoint.endpointUrl, endpoint.securityMode.toString(), endpoint.securityPolicyUri.toString());
});
});
}

server.initialize(post_initialize);

0 comments on commit 3d750cf

Please sign in to comment.