Skip to content

Commit

Permalink
fixing sample_server, add minimumSamplingInterval #1133
Browse files Browse the repository at this point in the history
  • Loading branch information
erossignon committed Apr 14, 2022
1 parent b27d7b8 commit 5015a38
Showing 1 changed file with 20 additions and 25 deletions.
45 changes: 20 additions & 25 deletions documentation/sample_server.js
@@ -1,3 +1,4 @@
const os = require("os");
const { OPCUAServer, Variant, DataType, StatusCodes } = require("node-opcua");


Expand Down Expand Up @@ -27,42 +28,32 @@ const { OPCUAServer, Variant, DataType, StatusCodes } = require("node-opcua");

// add some variables
// add a variable named MyVariable1 to the newly created folder "MyDevice"
let variable1 = 1;

// emulate variable1 changing every 500 ms
setInterval(() => { variable1 += 1; }, 500);

namespace.addVariable({
const variable1 = namespace.addVariable({
componentOf: device,
browseName: "MyVariable1",
dataType: "Double",
value: {
get: () => new Variant({ dataType: DataType.Double, value: variable1 })
}
minimumSamplingInterval: 100,
});

let counter = 0;
// emulate variable1 changing every 500 ms
const timerId = setInterval(() => { counter += 1; variable1.setValueFromSource({ dataType: DataType.Double, value: counter }) }, 500);

addressSpace.registerShutdownTask(() => { clearInterval(timerId); });

// add a variable named MyVariable2 to the newly created folder "MyDevice"
let variable2 = 10.0;

namespace.addVariable({

componentOf: device,

nodeId: "ns=1;b=1020FFAA", // some opaque NodeId in namespace 4

browseName: "MyVariable2",

dataType: "Double",

value: {
get: () => new Variant({ dataType: DataType.Double, value: variable2 }),
set: (variant) => {
variable2 = parseFloat(variant.value);
return StatusCodes.Good;
}
}
minimumSamplingInterval: 0, // this variable will be event driven
});
const os = require("os");

/**
* returns the percentage of free memory on the running machine
* @return {double}
Expand All @@ -79,16 +70,20 @@ const { OPCUAServer, Variant, DataType, StatusCodes } = require("node-opcua");
nodeId: "s=free_memory", // a string nodeID
browseName: "FreeMemory",
dataType: "Double",
minimumSamplingInterval: 1234,
value: {
get: () => new Variant({ dataType: DataType.Double, value: available_memory() })
}
});

server.start(function() {
console.log("Server is now listening ... ( press CTRL+C to stop)");
console.log("port ", server.endpoints[0].port);
const endpointUrl = server.endpoints[0].endpointDescriptions()[0].endpointUrl;
console.log(" the primary server endpoint url is ", endpointUrl);
await server.start();
console.log("Server is now listening ... ( press CTRL+C to stop)");
console.log("port ", server.endpoints[0].port);
console.log(" the primary server endpoint url is ", server.getEndpointUrl());

process.once("SIGINT", async () => {
console.log("shuting down");
await server.shutdown();
});

})();

0 comments on commit 5015a38

Please sign in to comment.