Skip to content

Instantly share code, notes, and snippets.

@erossignon
Created August 13, 2019 12:27
Show Gist options
  • Save erossignon/3b31b733dee14a97fc447c7a9ef8ba6c to your computer and use it in GitHub Desktop.
Save erossignon/3b31b733dee14a97fc447c7a9ef8ba6c to your computer and use it in GitHub Desktop.
const chalk = require("chalk");
const {
OPCUAServer ,
OPCUACertificateManager
} = require("node-opcua");
const certificateManager = new OPCUACertificateManager({
automaticallyAcceptUnknownCertificate: true,
rootFolder: "./certificates/PKI",
});
const server = new OPCUAServer({
port: 26545,
serverCertificateManager: certificateManager,
/** */
certificateFile: "./certificates/server_certificate.pem",
privateKeyFile: certificateManager.privateKeyFile,
buildInfo: {
productUri: "urn:{hostname}:CapgeminiDemoApp"
}
});
(async ()=> {
console.log(chalk.red("Hello s") + chalk.green("World"));
await server.initialize();
/**
*
* @param {AddressSpace} addressSpace
*/
function populateAddressSppace(addressSpace)
{
const namespace = addressSpace.getOwnNamespace();
const folder = namespace.addFolder(addressSpace.rootFolder.objects,
{
browseName: "MyFolder"
});
const boiler = namespace.addObject({
browseName: "Boiler",
organizedBy: folder
});
const temperature = namespace.addVariable({
browseName: "Temperature",
accessLevel: "CurrentRead",
componentOf: boiler
});
let setPointTemperatureValue = 10.0;
// install a Read/Write variable representing a temperature set point of a temperature controller.
const setpointTemperature = namespace.addVariable({
browseName: "SetPointTemperature",
componentOf: boiler,
dataType: "Double",
accessLevel: "CurrentRead | CurrentWrite",
value: {
get: function () {
console.log("Client is requesting SetPointTemperature")
return new Variant({
dataType: DataType.Double,
value: setPointTemperatureValue });
},
set: function (variant) {
console.log("Client is setting SetPointTemperature")
setPointTemperatureValue = parseFloat(variant.value);
return StatusCodes.Good;
}
}
});
}
populateAddressSpace(server.engine.addressSpace);
await server.start();
console.log("Certificate rejected folder ", server.serverCertificateManager.rejectedFolder);
console.log("Certificate trusted folder ", server.serverCertificateManager.trustedFolder);
console.log("Server private key ", server.serverCertificateManager.privateKey);
console.log("Server started");
const endpointUrl = server.endpoints[0].endpointDescriptions()[0].endpointUrl;
console.log(" server is ready on ", endpointUrl);
console.log("CTRL+C to stop");
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment