forked from node-opcua/node-opcua-sampleserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
132 lines (109 loc) · 3.55 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
"use strict";
const {
OPCUAServer,
Variant,
DataType,
nodesets,
StatusCodes,
VariantArrayType,
ServerEngine
} = require("node-opcua");
const os = require("os");
/**
* @param server {OPCUAServer} server
*/
function constructAddressSpace(server) {
const addressSpace = server.engine.addressSpace;
const namespace = addressSpace.getOwnNamespace();
// we create a new folder under RootFolder
const myDevice = namespace.addFolder("ObjectsFolder", {
browseName: "MyDevice"
});
// now let's add first variable in folder
// the addVariableInFolder
const variable1 = 10.0;
server.nodeVariable1 = namespace.addVariable({
componentOf: myDevice,
nodeId: "s=Temperature",
browseName: "Temperature",
dataType: "Double",
value: {
get: () => {
const t = new Date() / 10000.0;
const value = variable1 + 10.0 * Math.sin(t);
return new Variant({ dataType: DataType.Double, value: value });
}
}
});
const nodeVariable2 = namespace.addVariable({
componentOf: myDevice,
browseName: "MyVariable2",
dataType: "String",
});
nodeVariable2.setValueFromSource({
dataType: DataType.String,
value: "Learn Node-OPCUA ! Read https://leanpub.com/node-opcuabyexample"
});
const nodeVariable3 = namespace.addVariable({
componentOf: myDevice,
browseName: "MyVariable3",
dataType: "Double",
arrayDimensions: [3],
accessLevel: "CurrentRead | CurrentWrite",
userAccessLevel: "CurrentRead | CurrentWrite",
valueRank: 1
});
nodeVariable3.setValueFromSource({
dataType: DataType.Double,
arrayType: VariantArrayType.Array,
value: [1.0, 2.0, 3.0]
});
const nodeVariable4 = namespace.addVariable({
componentOf: myDevice,
nodeId: "b=1020ffab",
browseName: "Percentage Memory Used",
dataType: "Double",
minimumSamplingInterval: 1000,
value: {
get: () => {
// const value = process.memoryUsage().heapUsed / 1000000;
const percentageMemUsed = 1.0 - (os.freemem() / os.totalmem());
const value = percentageMemUsed * 100;
return new Variant({ dataType: DataType.Double, value: value });
}
}
});
// please
}
(async () => {
try {
// Let create an instance of OPCUAServer
const server = new OPCUAServer({
port: 26543, // the port of the listening socket of the server
nodeset_filename: [
nodesets.standard,
nodesets.di
],
buidIfo: {
productName: "Sample NodeOPCUA Server1",
buildNumber: "7658",
buildDate: new Date(2020, 6, 14)
}
});
await server.initialize();
constructAddressSpace(server);
// we can now start the server
await server.start();
console.log("Server is now listening ... ( press CTRL+C to stop) ");
server.endpoints[0].endpointDescriptions().forEach(function(endpoint) {
console.log(endpoint.endpointUrl, endpoint.securityMode.toString(), endpoint.securityPolicyUri.toString());
});
process.on("SIGINT", async () => {
await server.shutdown();
console.log("terminated");
});
} catch (err) {
console.log(err);
process.exit(-1);
}
})();