Browse Source

add node, etc

Martin Kunz 2 years ago
parent
commit
eb78c85734
3 changed files with 77 additions and 17 deletions
  1. 1 0
      .gitignore
  2. 6 6
      package.json
  3. 70 11
      src/main.ts

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+node_modules

+ 6 - 6
package.json

@@ -5,18 +5,18 @@
   "main": "main.js",
   "type": "module",
   "dependencies": {
-    "node-opcua": "^2.44.0",
-    "node-opcua-samples": "^2.44.0",
-    "node-opcua-server": "^2.44.0",
-    "typescript": "^4.3.4"
+    "node-opcua": "^2.46.0",
+    "node-opcua-samples": "^2.46.0",
+    "node-opcua-server": "^2.46.0",
+    "typescript": "^4.3.5"
   },
   "devDependencies": {
-    "ts-node-dev": "^1.1.6"
+    "ts-node-dev": "^1.1.8"
   },
   "scripts": {
     "dev": "ts-node-dev --respawn --pretty --transpile-only src/main.ts",
     "test": "echo \"Error: no test specified\" && exit 1"
   },
-  "author": "",
+  "author": "CDP/Martin Kunz",
   "license": "ISC"
 }

+ 70 - 11
src/main.ts

@@ -3,7 +3,11 @@ import {
     Variant,
     DataType,
     StatusCodes,
-    standardUnits
+    standardUnits,
+    BrowsePath,
+    NodeId,
+    NodeIdType,
+    
   } from "node-opcua";
 import { nodesets, constructNodesetFilename } from "node-opcua-nodesets";
 import { generateAddressSpace } from "node-opcua-address-space/nodeJS.js";
@@ -12,8 +16,11 @@ import { AddressSpace, RootFolder, UAAnalogItem, UAObject, UAObjectType, UAVaria
 class Main {
     server: OPCUAServer;
     constructor() {
-        this.init();
-        this.start()
+    }
+
+    async run() {
+        await this.init();
+        await this.start()
     }
 
     async init() {
@@ -26,22 +33,73 @@ class Main {
                 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",
+               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 node =this.server.engine.addressSpace.findNode("ns=1;i=6003");
+        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 s top)");
+            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);
@@ -49,3 +107,4 @@ class Main {
     }
   }
   let main = new Main();
+  main.run();