main.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import {
  2. OPCUAServer,
  3. Variant,
  4. DataType,
  5. StatusCodes,
  6. standardUnits,
  7. BrowsePath,
  8. NodeId,
  9. NodeIdType,
  10. } from "node-opcua";
  11. import { nodesets, constructNodesetFilename } from "node-opcua-nodesets";
  12. import { generateAddressSpace } from "node-opcua-address-space/nodeJS.js";
  13. import { AddressSpace, RootFolder, UAAnalogItem, UAObject, UAObjectType, UAVariable } from "node-opcua-address-space";
  14. class Main {
  15. server: OPCUAServer;
  16. constructor() {
  17. }
  18. async run() {
  19. await this.init();
  20. await this.start()
  21. }
  22. async init() {
  23. this.server = new OPCUAServer({
  24. port: 4334, // the port of the listening socket of the server
  25. resourcePath: "/UA/nfrontend", // this path will be added to the endpoint resource name
  26. buildInfo: {
  27. productName: "NFrontend",
  28. buildNumber: "1337",
  29. buildDate: new Date(2021, 3, 16)
  30. },
  31. nodeset_filename: [
  32. nodesets.standard,
  33. "nodeset/Opc.Ua.MachineTool.Nodeset2.xml",
  34. "nodeset/Opc.Ua.Machinery.NodeSet2.xml",
  35. "nodeset/Opc.Ua.Di.NodeSet2.xml",
  36. "nodeset/Opc.Ua.Ia.NodeSet2.xml",
  37. "nodeset/emco_umati.xml",
  38. ]
  39. });
  40. await this.server.initialize();
  41. let addressSpace=this.server.engine.addressSpace
  42. let node = <UAVariable>addressSpace.findNode(new NodeId(NodeIdType.NUMERIC, 6003, 2))
  43. var options = {
  44. get: async function () {
  45. return new Variant({
  46. dataType: DataType.Double,
  47. value: 1
  48. });
  49. }
  50. };
  51. let namespace = addressSpace.getOwnNamespace();
  52. // addressSpace.getDefaultNamespace.n
  53. const device = namespace.addObject({
  54. organizedBy: addressSpace.rootFolder.objects,
  55. browseName: "MyDevice"
  56. });
  57. let nodeset=addressSpace.getNamespace(2).toNodeset2XML();
  58. // add a variable named MyVariable1 to the newly created folder "MyDevice"
  59. let variable1 = 1;
  60. // emulate variable1 changing every 500 ms
  61. setInterval(() => { variable1+=1; }, 500);
  62. let mynode = namespace.addVariable({
  63. componentOf: device,
  64. browseName: "MyVariable1",
  65. dataType: "Double",
  66. value: {
  67. get: () => new Variant({dataType: DataType.Double, value: variable1 })
  68. }
  69. });
  70. mynode.bindVariable(options, true)
  71. // const setpointTemperature = namespace.addVariable({
  72. // browseName: "SetPointTemperature",
  73. // componentOf: device,
  74. // dataType: "Double",
  75. // accessLevel: "CurrentRead | CurrentWrite",
  76. // value: {
  77. // get: function () {
  78. // console.log("Client is requesting SetPointTemperature")
  79. // return new Variant({
  80. // dataType: DataType.Double,
  81. // value: 1.1 });
  82. // },
  83. // set: function (variant) {
  84. // console.log("Client is setting SetPointTemperature")
  85. // // setPointTemperatureValue = parseFloat(variant.value);
  86. // return StatusCodes.Good;
  87. // }
  88. // }
  89. // });
  90. // node.displayName;
  91. }
  92. async start() {
  93. console.log("initialized");
  94. this.server.start(() => {
  95. console.log("Server is now listening ... ( press CTRL+C to stop)");
  96. console.log("port ", this.server.endpoints[0].port);
  97. const endpointUrl = this.server.endpoints[0].endpointDescriptions()[0].endpointUrl;
  98. console.log(" the primary server endpoint url is ", endpointUrl);
  99. });
  100. }
  101. }
  102. let main = new Main();
  103. main.run();