|
@@ -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();
|