-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
98 lines (81 loc) · 3.29 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
/*global require,setInterval,console */
var opcua = require("node-opcua");
// Let's create an instance of OPCUAServer
var server = new opcua.OPCUAServer({
port: 4334, // the port of the listening socket of the server
resourcePath: "UA/MyLittleServer", // this path will be added to the endpoint resource name
buildInfo : {
productName: "MySampleServer1",
buildNumber: "7658",
buildDate: new Date(2014,5,2)
}
});
function post_initialize() {
console.log("initialized");
function construct_my_address_space(server) {
var addressSpace = server.engine.addressSpace;
// declare a new object
var device = addressSpace.addObject({
organizedBy: addressSpace.rootFolder.objects,
browseName: "MyDevice"
});
// add some variables
// add a variable named MyVariable1 to the newly created folder "MyDevice"
var variable1 = true;
addressSpace.addVariable({
componentOf: device,
nodeId: "ns=1;s=MyVariable1",
browseName: "MyVariable1",
dataType: "Boolean",
value: {
get: function () {
return new opcua.Variant({dataType: opcua.DataType.Boolean, value: variable1 });
}
}
});
// add a variable named MyVariable2 to the newly created folder "MyDevice"
var variable2 = true;
server.engine.addressSpace.addVariable({
componentOf: device,
nodeId: "ns=1;s=MyVariable2", // some opaque NodeId in namespace 4
browseName: "MyVariable2",
dataType: "Boolean",
value: {
get: function () {
return new opcua.Variant({dataType: opcua.DataType.Boolean, value: variable2 });
},
set: function (variant) {
variable2 = variant.value;
return opcua.StatusCodes.Good;
}
}
});
var os = require("os");
/**
* returns the percentage of free memory on the running machine
* @return {double}
*/
function available_memory() {
// var value = process.memoryUsage().heapUsed / 1000000;
var percentageMemUsed = os.freemem() / os.totalmem() * 100.0;
return percentageMemUsed;
}
server.engine.addressSpace.addVariable({
componentOf: device,
nodeId: "ns=1;s=free_memory", // a string nodeID
browseName: "FreeMemory",
dataType: "Double",
value: {
get: function () {return new opcua.Variant({dataType: opcua.DataType.Double, value: available_memory() });}
}
});
}
construct_my_address_space(server);
server.start(function() {
console.log("Server is now listening ... ( press CTRL+C to stop)");
console.log("port ", server.endpoints[0].port);
var endpointUrl = server.endpoints[0].endpointDescriptions()[0].endpointUrl;
console.log(" the primary server endpoint url is ", endpointUrl );
});
}
server.initialize(post_initialize);