123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- import {
- OPCUAServer,
- Variant,
- DataType,
- StatusCodes,
- standardUnits,
- BrowsePath,
- NodeId,
- NodeIdType,
-
- } from "node-opcua";
- import { nodesets, constructNodesetFilename } from "node-opcua-nodesets";
- import { generateAddressSpace } from "node-opcua-address-space/nodeJS.js";
- import { AddressSpace, RootFolder, UAAnalogItem, UAObject, UAObjectType, UAVariable } from "node-opcua-address-space";
- class Main {
- server: OPCUAServer;
- constructor() {
- }
- async run() {
- await this.init();
- await this.start()
- }
- async init() {
- this.server = new OPCUAServer({
- port: 4334, // the port of the listening socket of the server
- resourcePath: "/UA/nfrontend", // this path will be added to the endpoint resource name
- buildInfo: {
- productName: "NFrontend",
- buildNumber: "1337",
- buildDate: new Date(2021, 3, 16)
- },
- nodeset_filename: [
- nodesets.standard,
- "nodeset/Opc.Ua.MachineTool.Nodeset2.xml",
- "nodeset/Opc.Ua.Machinery.NodeSet2.xml",
- "nodeset/Opc.Ua.Di.NodeSet2.xml",
- "nodeset/Opc.Ua.Ia.NodeSet2.xml",
- "nodeset/emco_umati.xml",
- ]
- });
- await this.server.initialize();
- let addressSpace=this.server.engine.addressSpace
- let node = <UAVariable>addressSpace.findNode(new NodeId(NodeIdType.NUMERIC, 6003, 2))
- var options = {
- get: async function () {
- return new Variant({
- dataType: DataType.Double,
- value: 1
- });
- }
- };
- let namespace = addressSpace.getOwnNamespace();
- // addressSpace.getDefaultNamespace.n
- const device = namespace.addObject({
- organizedBy: addressSpace.rootFolder.objects,
- browseName: "MyDevice"
- });
- let nodeset=addressSpace.getNamespace(2).toNodeset2XML();
- // add a variable named MyVariable1 to the newly created folder "MyDevice"
- let variable1 = 1;
- // emulate variable1 changing every 500 ms
- setInterval(() => { variable1+=1; }, 500);
- let mynode = namespace.addVariable({
- componentOf: device,
- browseName: "MyVariable1",
- dataType: "Double",
- value: {
- get: () => new Variant({dataType: DataType.Double, value: variable1 })
- }
- });
- mynode.bindVariable(options, true)
- // const setpointTemperature = namespace.addVariable({
- // browseName: "SetPointTemperature",
- // componentOf: device,
- // dataType: "Double",
- // accessLevel: "CurrentRead | CurrentWrite",
- // value: {
- // get: function () {
- // console.log("Client is requesting SetPointTemperature")
- // return new Variant({
- // dataType: DataType.Double,
- // value: 1.1 });
- // },
- // set: function (variant) {
- // console.log("Client is setting SetPointTemperature")
- // // setPointTemperatureValue = parseFloat(variant.value);
- // return StatusCodes.Good;
- // }
- // }
- // });
- // node.displayName;
- }
- async start() {
- console.log("initialized");
- this.server.start(() => {
- console.log("Server is now listening ... ( press CTRL+C to stop)");
- console.log("port ", this.server.endpoints[0].port);
- const endpointUrl = this.server.endpoints[0].endpointDescriptions()[0].endpointUrl;
- console.log(" the primary server endpoint url is ", endpointUrl);
- });
- }
- }
- let main = new Main();
- main.run();
|