-
Notifications
You must be signed in to change notification settings - Fork 2
/
sample_server.js
93 lines (80 loc) · 2.65 KB
/
sample_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
let os = require("os");
let opcua = require("node-opcua");
let util = require("./utility");
let server = new opcua.OPCUAServer({
port: 4334,
resourcePath: "/UA/RaspServer",
buildInfo: {
productName: "RaspUAServer1",
buildNumber: "1",
buildDate: new Date()
}
});
server.initialize(() => {
console.log("OPC UA Server initialized.");
function build_my_address_space(server) {
const addressSpace = server.engine.addressSpace;
const namespace = addressSpace.getOwnNamespace();
//declare a new object
let device = namespace.addObject({
organizedBy: addressSpace.rootFolder.objects,
browseName: "MyDevice"
});
//add some variables
namespace.addVariable({
propertyOf: device,
browseName: "CPU_Architecture",
dataType: "String",
value: {
get: function() {
return new opcua.Variant({
dataType: opcua.DataType.String,
value: process.arch
});
}
}
});
namespace.addVariable({
componentOf: device,
browseName: "FreeMemory",
dataType: "Double",
value: {
get: function() { return new opcua.Variant({
dataType: opcua.DataType.Double,
value: util.getAvailableMemory()
})}
}
});
//add the following variable for a Raspberry only
if (["arm", "arm64"].indexOf(os.arch()) !== -1) {
namespace.addVariable({
componentOf: device,
browseName: "CPU_Temperature",
dataType: "Double",
value: {
refreshFunc: util.getTemperatureAsync
}
});
namespace.addVariable({
componentOf: device,
browseName: "RedLed",
dataType: "Boolean",
value: {
get: util.getLedValue,
set: util.setLedValue
}
});
}
}
build_my_address_space(server);
console.log("Address space initialized.");
server.start(() => {
console.log(`Server is now listening port ${server.endpoints[0].port}... (press CTRL+C to stop)`);
let endpointUrl = server.endpoints[0].endpointDescriptions()[0].endpointUrl;
console.log("The primary server endpoint url is ", endpointUrl);
});
});
process.on('SIGINT', function () {
util.freeResources();
process.exit(0);
});