-
Notifications
You must be signed in to change notification settings - Fork 8
/
lightning-rod.js
425 lines (262 loc) · 11.5 KB
/
lightning-rod.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
//############################################################################################
//##
//# Copyright (C) 2014-2018 Dario Bruneo, Francesco Longo, Andrea Rocco Lotronto,
//# Giovanni Merlino, Nicola Peditto
//##
//# Licensed under the Apache License, Version 2.0 (the "License");
//# you may not use this file except in compliance with the License.
//# You may obtain a copy of the License at
//##
//# http://www.apache.org/licenses/LICENSE-2.0
//##
//# Unless required by applicable law or agreed to in writing, software
//# distributed under the License is distributed on an "AS IS" BASIS,
//# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//# See the License for the specific language governing permissions and
//# limitations under the License.
//##
//############################################################################################
// LIBRARIES
var fs = require("fs");
var spawn = require('child_process').spawn;
var running = require('is-running'); // We use this library to check if a process is running (PID check)
var autobahn = require('autobahn');
var connectionTester = require('connection-tester');
//settings parser
nconf = require('nconf');
SETTINGS = process.env.IOTRONIC_HOME+'/settings.json';
nconf.file ('config', {file: SETTINGS});
AUTH_CONF = '/etc/iotronic/authentication.json';
nconf.file ('auth', {file: AUTH_CONF});
//logging configuration: "board-management"
log4js = require('log4js');
logger = log4js.getLogger('main');
// Device settings
boardCode = null; //valued in checkSettings
boardLabel = null; //valued in checkSettings
board_position = null; //valued by Iotronic (via updateConf, setConf or setBoardPosition RPCs)
reg_status = null; //valued in checkSettings
device = null; //valued in checkSettings
lyt_device = null; //valued here in main
net_backend = '';
wampIP = null;
auth_lr_mode = null;
var wampConnection = null;
//WAMP SOCKETS RECOVERY HACK
wamp_socket_recovery = nconf.get('auth:wamp:wamp_socket_recovery');
if(wamp_socket_recovery == true || wamp_socket_recovery == "true"){
//timing
rpc_alive_time = nconf.get('auth:wamp:rpc_alive_time');
if (isNaN(rpc_alive_time))
rpc_alive_time = 60; //set default value
check_skt_time = nconf.get('auth:wamp:check_skt_time');
if (isNaN(check_skt_time))
check_skt_time = 30; //set default value
online = true; // We use this flag during the process of connection recovery
RECOVERY_SESSION = null;
//timers
keepWampAlive = null; // It is a timer related to the function that every "X" seconds/minutes checks the connection status
checkSocketTimer = null; // It is a timer
//timeout
expectIotronicResponse = null;
//trigger variable
socketNotAlive = false;
}
// LR s4t libraries
var LIGHTNINGROD_HOME = process.env.LIGHTNINGROD_HOME;
var manageBoard = require(LIGHTNINGROD_HOME + '/modules/board-manager/board-management');
LR_VERSION = manageBoard.getLRversion();
LR_PID = process.pid;
//Init_Ligthning_Rod(function (check) {
manageBoard.Init_Ligthning_Rod(function (check) {
if(check.result === "ERROR"){
// Got a critical error in configuration file (settings.json)
logger.error('[SYSTEM] - Logger configuration is wrong or not specified!');
var log_template={log: {logfile: "<LOG-FILE>", loglevel: "<LOG-LEVEL>"}};
logger.error("[SYSTEM] - Logger configuration expected: \n" + JSON.stringify(log_template, null, "\t"));
process.exit();
}
else{
// Configuration file is correctly configured... Starting LR...
logger.info('[SYSTEM] - DEVICE: ' + device);
//----------------------------------------
// 1. Set WAMP connection configuration
//----------------------------------------
var wampUrl = url_wamp+":"+port_wamp+"/ws";
try{
wampIP = wampUrl.split("//")[1].split(":")[0];
}
catch(err){
logger.warn('[WAMP] - Error in WAMP url format: '+ err);
process.exit(1);
}
//var wampRealm = nconf.get('config:wamp:realm');
var wampRealm = realm;
logger.info("[SYSTEM] - Iotronic server IP: "+wampIP);
wampConnection = new autobahn.Connection({
url: wampUrl,
realm: wampRealm,
tlsConfiguration: {},
max_retries: -1
});
logger.info("[SYSTEM] - WAMP server IP: " + wampIP);
logger.info("[SYSTEM] - Board ID: " + boardCode);
logger.info("[SYSTEM] - Board label: " + boardLabel);
logger.debug("[SYSTEM] - WAMP socket recovery: " + wamp_socket_recovery);
//----------------------------------------------------------------------------------------
// 3. On WAMP connection open the device will load LR libraries and start each LR module
//----------------------------------------------------------------------------------------
//This function is called as soon as the connection is created successfully
wampConnection.onopen = function (session, details) {
logger.info('[WAMP] - Connection to WAMP server '+ wampUrl + ' created successfully:');
logger.info('[WAMP] |--> Realm: '+ wampRealm);
logger.info('[WAMP] |--> Session ID: '+ session._id);
//logger.debug('[WAMP] |--> Connection details:\n'+ JSON.stringify(details));
//--WAMP-RECOVERY-SYSTEM-INIT-------------------------------------------------------------------------------
if(wamp_socket_recovery == true || wamp_socket_recovery == "true") {
logger.info('[WAMP] |--> socket check timing: '+ wamp_socket_recovery + " - Timing: " + rpc_alive_time + " seconds");
RECOVERY_SESSION = session;
socketNotAlive = false;
if (expectIotronicResponse != null) {
clearTimeout(expectIotronicResponse);
logger.debug('[WAMP-RECOVERY] - Check socket timers cleared!');
}
}
//----------------------------------------------------------------------------------------------------------
// Test if IoTronic is connected to the realm
session.call("s4t.iotronic.isAlive", [boardCode]).then(
function(response){
logger.info("[SYSTEM] - " + response.message );
// RPC registration of Board Management Commands
manageBoard.IotronicLogin(session);
if(wamp_socket_recovery == true || wamp_socket_recovery == "true"){
if (keepWampAlive != null){
logger.info('[WAMP-RECOVERY] - WAMP CONNECTION RECOVERED!');
//We trigger this event after a connection recovery: we are deleting the previous timer
clearInterval( keepWampAlive );
if (checkSocketTimer != null){
clearInterval( checkSocketTimer );
}
logger.debug('[WAMP-RECOVERY] - Old timers cleared!');
}
// The function managed by setInterval checks the connection status every "X" TIME
keepWampAlive = setInterval(function(){
// connectionTester: library used to check the reachability of Crossbar-Wamp-Broker (timeout check set to 10 seconds)
connectionTester.test(wampIP, port_wamp, 10000, function (err, output) {
var reachable = output.success;
var error_test = output.error;
if(!reachable){
//CONNECTION STATUS: FALSE
logger.warn("[CONNECTION-RECOVERY] - INTERNET CONNECTION STATUS: " + reachable + " - Error: " + error_test);
online = false;
} else {
if(!online){
// In the previous checks the "online" flag was set to FALSE.
logger.info("[CONNECTION-RECOVERY] - INTERNET CONNECTION RECOVERED");
online = true;
}
//logger.debug("[CONNECTION-TEST] - INTERNET CONNECTION STATUS: " + reachable + " - Error: " + error_test);
//CONNECTION COMEBACK AVAILABLE - STATUS: TRUE
expectIotronicResponse = setTimeout(function(){
logger.warn("[WAMP-RECOVERY] - No Iotronic response, socket restoring...");
socketNotAlive = true; //trigger the 'checkSocketTimer' procedure that will clean the zombie-wamp-socket
}, 5000);
try{
// Test if IoTronic is connected to the realm
RECOVERY_SESSION.call("s4t.iotronic.isAlive", [boardCode]).then(
function(response){
// WAMP connection is OK
clearTimeout(expectIotronicResponse);
//logger.info("[WAMP-ALIVE] - WAMP CONNECTION STATUS: " + response.message);
socketNotAlive = false;
},
function (err) {
// WAMP connection is OK but I got an error on RPC communication
clearTimeout(expectIotronicResponse);
logger.warn("[WAMP-RECOVERY] - WAMP CONNECTION STATUS: " + JSON.stringify(err));
}
);
}
catch(err){
logger.warn('[CONNECTION-RECOVERY] - Error keeping alive wamp connection: '+ err);
}
}
});
}, rpc_alive_time * 1000);
checkSocketTimer = setInterval(function(){
if(socketNotAlive == true){
logger.warn("[WAMP-RECOVERY] - GDB - Start socket cleaning...");
var cp = require('child_process');
var gdb = cp.fork(LIGHTNINGROD_HOME + '/modules/board-manager/wamp_recovery.js', [LR_PID, port_wamp]);
gdb.on('message', function(msg) {
if(msg != undefined){
if (msg.result === "SUCCESS"){
logger.info("[WAMP-RECOVERY] - GDB - " + msg.message);
}
else{
logger.warn("[WAMP-RECOVERY] - GDB - " + msg.message);
}
}
});
}
}, check_skt_time * 1000);
logger.debug('[WAMP] - TIMERS to keep alive WAMP connection set up!');
}
},
function (err) {
// IoTronic is not connected to the realm yet so LR need to try to reconnect later
logger.error("[SYSTEM] - IoTronic is not online: " + JSON.stringify(err) );
//if (err.error !== 'wamp.error.no_such_procedure') {}
logger.warn("[SYSTEM] - Lightning-rod reconnecting...");
wampConnection.close();
setTimeout(function(){
wampConnection.open();
}, 10 * 1000); //time to wait for the next reconnection attempt
}
);
};
//-------------------------------
// 4. On WAMP connection close
//-------------------------------
//This function is called if there are problems with the WAMP connection
wampConnection.onclose = function (reason, details) {
try{
logger.error('[WAMP] - Error in connecting to WAMP server!');
logger.error('- Reason: ' + reason);
logger.error('- Reconnection Details: ');
logger.error(" - retry_delay:", details.retry_delay);
logger.error(" - retry_count:", details.retry_count);
logger.error(" - will_retry:", details.will_retry);
if(wampConnection.isOpen){
logger.info("[WAMP] - connection is open!");
}
else{
logger.warn("[WAMP] - connection is closed!");
}
}
catch(err){
logger.warn('[WAMP] - Error in WAMP connection: '+ err);
}
};
//--------------------------------------------------------------
// 2. The selected device will connect to Iotronic WAMP server
//--------------------------------------------------------------
try{
IoT_Device = require('./device/lyt_'+device);
lyt_device = new IoT_Device(device);
logger.info("[SYSTEM] - Lightning-rod "+ lyt_device.name +" starting...");
lyt_device.Main(wampConnection, logger);
}
catch (e) {
logger.error('[SYSTEM] - Loading IoT device failure: ', e);
logger.error('[SYSTEM] - Device "' + device + '" not supported!');
logger.error('[SYSTEM] - Supported devices are: "server", "arduino_yun", "raspberry_pi".');
logger.info("Bye!");
process.exit(1);
}
//--------------------------------------------------------------
// 5. Load Module Manager
//--------------------------------------------------------------
manageBoard.moduleLoaderOnBoot();
}
});