-
Notifications
You must be signed in to change notification settings - Fork 36
/
server.mjs
130 lines (109 loc) · 3.35 KB
/
server.mjs
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
import os from 'os';
import {
OPCUAServer,
Variant,
DataType,
nodesets,
VariantArrayType,
MessageSecurityMode,
SecurityPolicy
} from 'node-opcua';
/**
* @param {IAddressSpace} addressSpace
*/
function constructAddressSpace (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;
namespace.addVariable({
componentOf: myDevice,
nodeId: 's=Temperature',
browseName: 'Temperature',
dataType: 'Double',
minimumSamplingInterval: 500,
accessLevel: 'CurrentRead',
value: {
get: () => {
const t = new Date() / 10000.0;
const value = variable1 + 10.0 * Math.sin(t);
return new Variant({ dataType: DataType.Double, value });
}
}
});
const uaVariable2 = namespace.addVariable({
componentOf: myDevice,
browseName: 'MyVariable2',
dataType: 'String'
});
uaVariable2.setValueFromSource({
dataType: DataType.String,
value: 'Learn Node-OPCUA ! Read https://leanpub.com/node-opcuabyexample-edition2024'
});
const uaVariable3 = namespace.addVariable({
componentOf: myDevice,
browseName: 'MyVariable3',
dataType: 'Double',
arrayDimensions: [3],
accessLevel: 'CurrentRead | CurrentWrite',
userAccessLevel: 'CurrentRead | CurrentWrite',
valueRank: 1
});
uaVariable3.setValueFromSource({
dataType: DataType.Double,
arrayType: VariantArrayType.Array,
value: [1.0, 2.0, 3.0]
});
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 });
}
}
});
}
(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
],
buildInfo: {
productName: 'Sample NodeOPCUA Server1',
buildNumber: '7658',
buildDate: new Date(2024, 1, 26)
},
securityPolicies: [SecurityPolicy.None, SecurityPolicy.Basic256Sha256 ],
});
await server.initialize();
constructAddressSpace(server.engine.addressSpace);
// 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((endpoint) => {
console.log(endpoint.endpointUrl, MessageSecurityMode[endpoint.securityMode], endpoint.securityPolicyUri.toString().padEnd(60));
console.log(" ", endpoint.userIdentityTokens.map((x) => x.policyId.toString()).join(' '));
});
await new Promise((resolve) => process.once('SIGINT', resolve));
await server.shutdown();
console.log('server shutdown completed !');
} catch (err) {
console.log(err.message);
process.exit(-1);
}
})();