-
Notifications
You must be signed in to change notification settings - Fork 0
/
configdb.js
753 lines (694 loc) · 24.8 KB
/
configdb.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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
const sqlite3 = require('sqlite3').verbose();
var ConfigDB = function() {
this.components = require('./components').setFacility(this, 'configdb');
this.debug = require('./debug.js');
this.config = require('./config.js');
var debug = this.debug;
this.db = new sqlite3.Database(this.config.configdb.dbfile, (err) => {
if (err) {
debug.log(1, 'configdb', 'Error while opening DB file: ' + err.message);
} else {
debug.log(1, 'configdb', 'ConfigDB file opened succesfully');
}
//console.log('Connected to the configDB database.');
});
}
/**
* (only cloud) function to add new account
*/
ConfigDB.prototype.insertAccount = function(accountID, name, email, password, salt) {
}
/**
* (only cloud) function to add new raspy
*/
ConfigDB.prototype.insertRaspy = function(accountID, raspyID, vpnID, vpnKey, remote, backup) {
}
/**
* (only Raspy) function returns vpnID value for the raspy, if it is not configured, returns null
*/
ConfigDB.prototype.getVpnID = function(accountID, raspyID, callback) {
var debug = this.debug;
var vpnID = accountID + '-' + raspyID
var sql = 'SELECT vpnID, vpnKey, initVpnKey, cloudService FROM raspys WHERE accountID = ? AND raspyID = ?';
this.db.all(sql, [accountID, raspyID], function(error, rows) {
if (error) {
callback(error, null);
return;
}
if (rows.length == 0) {
debug.logVPN(1, 'configdb', vpnID, 'DB row empty. accountID or raspyID not found in the DB.');
callback(true, null);
return;
}
for (var i = 0; i < rows.length; i++) {
var row = rows[i];
callback(null, row.vpnID, row.vpnKey, row.initVpnKey, row.cloud);
return;
}
});
}
/**
* (only Raspy) function sets the new vpnID and vpnKey (also changes raspyID in all devices and arduinos)
*/
ConfigDB.prototype.setVpnID = function(accountID, raspyID, enabled, vpnID, vpnKey, initVpnKey, initSetupFlag) {
var debug = this.debug;
sql = 'UPDATE raspys SET vpnID = ?, vpnKey = ?, cloudService = ? \
WHERE accountID = ? AND raspyID ?'
SQLUpdate = 'UPDATE raspys SET ';
SQLWhere = ' WHERE raspyID = ? AND accountID = ?';
values = [];
if (typeof(enabled) != 'undefined') {
SQLUpdate += ' cloudService = ?,';
if (enabled)
values.push(1);
else
values.push(0);
}
if (typeof(vpnID) != 'undefined') {
SQLUpdate += ' vpnID = ?,';
values.push(vpnID);
}
if (typeof(vpnKey) != 'undefined') {
SQLUpdate += ' vpnKey = ?,';
values.push(vpnKey);
}
if (typeof(initVpnKey) != 'undefined') {
SQLUpdate += ' initVpnKey = ?,';
values.push(initVpnKey);
}
if (typeof(initSetupFlag) != 'undefined') {
SQLUpdate += ' initSetupFlag = ?,';
if (initSetupFlag)
values.push(1);
else
values.push(0);
}
SQLUpdate = SQLUpdate.substring(0, SQLUpdate.length - 1);
values.push(raspyID);
values.push(accountID);
this.db.run(SQLUpdate + SQLWhere, values, function(error) {
if (error) {
debug.log(1, 'configdb', 'Error while updating Raspy with VPN details: ' + error.message);
} else {
debug.log(5, 'configdb', 'Updated Raspy with VPN details, accountid: ' + accountID + ', raspyid: ' + raspyID);
}
});
}
/**
* inserts an Arduino in the DB. This would typically happen on registering new Arduino on the raspy,
* or during receiving of device data on the cloud when there is no arduino defined in mem structure.
*/
ConfigDB.prototype.insertArduino = function(accountID, raspyID, IP, ardID, desc) {
var debug = this.debug;
var sql = 'INSERT INTO arduinos (ardID, raspyID, accountID, IP, desc) VALUES (?, ?, ?, ?, ?)';
this.db.run(sql, [ardID, raspyID, accountID, IP, desc], function(error) {
if (error) {
debug.log(1, 'configdb', 'Error while inserting new Arduino: ' + error.message);
} else {
debug.log(5, 'configdb', 'Inserted Arduino, accountid: ' + accountID + ', raspyid: ' +
raspyID + ', IP: ' + IP + ', ardID: ' + ardID + ', desc: ' + desc);
}
});
}
/**
* Insert new device into the cloud. This would happen when first data from device arravies at either cloud or raspy.
*/
ConfigDB.prototype.insertDevice = function(accountID, device) {
var debug = this.debug;
var sql = 'INSERT INTO devices (devID, ardID, raspyID, accountID, devType, dataType, value, date, desc, activated, IP, extType) \
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
var sqlShades = 'INSERT INTO shades (devID, ardID, raspyID, accountID, devType, position, tilt, sync, date, desc, activated, IP) \
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
var sqlTemp = 'INSERT INTO devices (devID, ardID, raspyID, accountID, devType, dataType, value, date, desc, activated, IP) \
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
debug.log(5, 'configdb', 'Inserting the following device into the DB: ' + JSON.stringify(device));
if (device.devType == 'digitOUT') {
this.db.run(sql, [device.devID, device.ardID, device.raspyID, accountID,
device.devType, device.dataType, device.value, JSON.stringify(device.date),
device.desc, device.activated ? 1 : 0, device.IP, device.extType
], function(error) {
if (error) {
debug.log(1, 'configdb', 'Error while inserting new Device: ' + error.message);
} else {
debug.log(5, 'configdb', 'Inserted digitOUT Device, accountid: ' + accountID + ', raspyid: ' +
device.raspyID + ', devID: ' + device.devID + ', ardID: ' + device.ardID);
}
});
} else if (device.devType == 'temp') {
this.db.run(sqlTemp, [device.devID, device.ardID, device.raspyID, accountID,
device.devType, device.dataType, device.value, JSON.stringify(device.date),
device.desc, device.activated ? 1 : 0, device.IP
], function(error) {
if (error) {
debug.log(1, 'configdb', 'Error while inserting new Temperature Device: ' + error.message);
} else {
debug.log(5, 'configdb', 'Inserted Temperature Device, accountid: ' + accountID + ', raspyid: ' +
device.raspyID + ', devID: ' + device.devID + ', ardID: ' + device.ardID);
}
});
} else if (device.devType == 'shade') {
let tilt;
let sync;
let position;
if (typeof(device.tilt) == 'undefined')
tilt = 0;
else
tilt = device.tilt;
if (typeof(device.sync) == 'undefined')
sync = 0;
else
sync = device.sync;
if (typeof(device.position) == 'undefined')
position = 0;
else
position = device.position;
this.db.run(sqlShades, [device.devID, device.ardID, device.raspyID, accountID,
device.devType, device.position, device.tilt, sync, JSON.stringify(device.date),
device.desc, device.activated ? 1 : 0, device.IP
], function(error) {
if (error) {
debug.log(1, 'configdb', 'Error while inserting new Shade Device: ' + error.message);
} else {
debug.log(5, 'configdb', 'Inserted Shade Device, accountid: ' + accountID + ', raspyid: ' +
device.raspyID + ', devID: ' + device.devID + ', ardID: ' + device.ardID);
}
});
} else {
debug.log(1, 'configdb', 'Error while inserting new Device, unrecognized devType: ' + device.devType);
}
}
/**
* This should be executed when new data arrives for device that is already in present in mem, on both raspy or cloud.
* Requires the following fields for digitIO and shades:
* device.ardID
* device.raspyID
* accountID
*
* Optional values for digitIO:
* device.activated
* device.IP
* device.desc
* device.date
* device.value
* device.devType
* device.dataType
* device.lightType
* device.lightInputType
* device.timer
* device.ctrlON
* device.extType
*
* Optional values for shades:
* device.activated
* device.IP
* device.desc
* device.date
* device.value
* device.devType
* device.dataType
* device.sync
* device.tilt
* device.position
* device.direction
*/
ConfigDB.prototype.updateDevice = function(accountID, device) {
var debug = this.debug;
let SQLUpdate;
sql = 'UPDATE devices SET value = ?, date = ?, desc = ?, ip = ?, activated = ? WHERE devID = ? AND ardID = ? AND raspyID = ? AND accountID = ?';
if (device.devType == 'shade') {
SQLUpdate = 'UPDATE shades SET ';
} else {
SQLUpdate = 'UPDATE devices SET ';
}
SQLWhere = ' WHERE devID = ? AND ardID = ? AND raspyID = ? AND accountID = ?';
values = [];
for (key in device) {
switch (key) {
case 'activated':
SQLUpdate += ' activated = ?,';
values.push(device.activated ? 1 : 0);
break;
case 'IP':
SQLUpdate += ' IP = ?,';
values.push(device.IP);
break;
case 'desc':
SQLUpdate += ' desc = ?,';
values.push(device.desc);
break;
case 'date':
SQLUpdate += ' date = ?,';
values.push(JSON.stringify(device.date));
break;
case 'value':
SQLUpdate += ' value = ?,';
values.push(device.value);
break;
case 'devType':
SQLUpdate += ' devType = ?,';
values.push(device.devType);
break;
case 'dataType':
SQLUpdate += ' dataType = ?,';
values.push(device.dataType);
break;
case 'extType':
SQLUpdate += ' extType = ?,';
values.push(device.extType);
break;
case 'sync':
SQLUpdate += ' sync = ?,';
if (device.sync == true)
values.push(true);
else
values.push(false);
break;
case 'tilt':
SQLUpdate += ' tilt = ?,';
values.push(device.tilt);
break;
case 'position':
SQLUpdate += ' position = ?,';
values.push(device.position);
break;
case 'positionTimer':
SQLUpdate += ' positionTimer = ?,';
values.push(device.positionTimer);
break;
case 'tiltTimer':
SQLUpdate += ' tiltTimer = ?,';
values.push(device.tiltTimer);
break;
case 'direction':
SQLUpdate += ' direction = ?,';
values.push(device.direction);
break;
case 'lightType':
SQLUpdate += ' lightType = ?,';
values.push(device.lightType);
break;
case 'lightInputType':
SQLUpdate += ' lightInputType = ?,';
values.push(device.lightInputType);
break;
case 'ctrlON':
SQLUpdate += ' ctrlON = ?,';
values.push(device.ctrlON);
break;
case 'timer':
SQLUpdate += ' timer = ?,';
values.push(device.timer);
break;
case 'ardID':
case 'devID':
case 'raspyID':
case 'discovered':
case 'alive': // there is no point in saving this, as it will be always updated real-time and on boot
break;
default:
debug.log(1, 'configdb', 'Error while parsing device fields, implementation issue: ' + key);
}
}
SQLUpdate = SQLUpdate.substring(0, SQLUpdate.length - 1);
//console.log('query: ' + SQLUpdate + SQLWhere);
values.push(device.devID);
values.push(device.ardID);
values.push(device.raspyID);
values.push(accountID);
//console.log('values: ' + values);
this.db.run(SQLUpdate + SQLWhere, values, function(error) {
if (error) {
debug.log(1, 'configdb', 'Error while updating Device: ' + error.message);
} else {
debug.log(5, 'configdb', 'Updated Device, accountid: ' + accountID + ', raspyid: ' +
device.raspyID + ', devID: ' + device.devID + ', ardID: ' + device.ardID);
}
});
}
/**
* This function updates the Arduino information in the DB.
* Requires the following fields:
* arduino.ardID
* arduino.raspyID
* accountID
*
* Optional values:
* arduino.IP
* arduino.desc
* arduino.date
* arduino.mac
* arduino.ctrlON
* arduino.mode
*/
ConfigDB.prototype.updateArduino = function(accountID, arduino) {
var debug = this.debug;
sql = 'UPDATE devices SET value = ?, date = ?, desc = ?, ip = ?, activated = ? \
WHERE devID = ? AND ardID = ? AND raspyID = ? AND accountID = ?';
SQLUpdate = 'UPDATE arduinos SET ';
SQLWhere = ' WHERE ardID = ? AND raspyID = ? AND accountID = ?';
values = [];
for (key in arduino) {
switch (key) {
case 'IP':
SQLUpdate += ' IP = ?,';
values.push(arduino.IP);
break;
case 'desc':
SQLUpdate += ' desc = ?,';
values.push(arduino.desc);
break;
case 'date':
SQLUpdate += ' date = ?,';
values.push(JSON.stringify(arduino.date));
break;
case 'mac':
SQLUpdate += ' mac = ?,';
values.push(arduino.mac);
break;
case 'ctrlON':
SQLUpdate += ' ctrlON = ?,';
values.push(arduino.ctrlON);
break;
case 'mode':
SQLUpdate += ' mode = ?,';
values.push(arduino.mode);
break;
case 'version':
SQLUpdate += ' version = ?,';
values.push(arduino.version);
break;
case 'ardID':
case 'raspyID':
case 'counter':
case 'alive': // there is no point in saving this, as it will be always updated real-time and on boot
break;
default:
debug.log(1, 'configdb', 'Error while parsing arduino fields, implementation issue: ' + key);
}
}
/* remove the trailing comma */
SQLUpdate = SQLUpdate.substring(0, SQLUpdate.length - 1);
//console.log('query: ' + SQLUpdate + SQLWhere);
values.push(arduino.ardID);
values.push(arduino.raspyID);
values.push(accountID);
this.db.run(SQLUpdate + SQLWhere, values, function(error) {
if (error) {
debug.log(1, 'configdb', 'Error while updating Arduino: ' + error.message);
} else {
debug.log(5, 'configdb', 'Updated Arduino, accountid: ' + accountID + ', raspyid: ' +
arduino.raspyID + ', ardID: ' + arduino.ardID);
}
});
}
/**
* This function updates the Raspy. (cloud only)
* Requires the following fields:
* raspy.raspyID
* either:
* raspy.alive
* raspy.lastSeen
*/
ConfigDB.prototype.updateRaspy = function(accountID, raspy) {
var debug = this.debug;
sql = 'UPDATE raspys SET alive = ?, lastSeen = ? \
WHERE raspyID = ? AND accountID = ?';
SQLUpdate = 'UPDATE raspys SET ';
SQLWhere = ' WHERE raspyID = ? AND accountID = ?';
values = [];
for (key in raspy) {
switch (key) {
case 'alive':
SQLUpdate += ' alive = ?,';
if (raspy.alive)
values.push(1);
else
values.push(0)
break;
case 'lastSeen':
SQLUpdate += ' lastSeen = ?,';
values.push(raspy.lastSeen);
break;
case 'raspyID':
break;
default:
debug.log(1, 'configdb', 'Error while parsing raspy fields, implementation issue: ' + key);
}
}
/* remove the trailing comma */
SQLUpdate = SQLUpdate.substring(0, SQLUpdate.length - 1);
values.push(raspy.raspyID);
values.push(accountID);
this.db.run(SQLUpdate + SQLWhere, values, function(error) {
if (error) {
debug.log(1, 'configdb', 'Error while updating Raspy: ' + error.message);
} else {
debug.log(5, 'configdb', 'Updated raspy, accountid: ' + accountID + ', raspyid: ' + raspy.raspyID);
}
});
}
/**
* This function delete the device information in the DB.
* Requires the following fields:
* device.ardID
* device.raspyID
* device.devID
* device.devType (either set to 'digitOUT' or 'shade')
* accountID
*/
ConfigDB.prototype.deleteDevice = function(accountID, device) {
var debug = this.debug;
var SQLDeviceDelete = 'DELETE FROM devices WHERE devID = ? AND ardID = ? AND raspyID = ? AND accountID = ?';
let SQLShadeDelete = 'DELETE FROM shades WHERE devID = ? AND ardID = ? AND raspyID = ? AND accountID = ?';
var values = [];
var db = this.db;
values.push(device.devID)
values.push(device.ardID);
values.push(device.raspyID);
values.push(accountID);
if (device.devType == 'digitOUT') {
db.run(SQLDeviceDelete, values, function(error) {
if (error) {
debug.log(1, 'configdb', 'Error while deleting light Device: ' + device.devID + ' Error: ' + error.message);
} else {
debug.log(5, 'configdb', 'Deleted device: ' + device.devID + ', ardID: ' + device.ardID);
}
});
} else if (device.devType == 'shade') {
db.run(SQLShadeDelete, values, function(error) {
if (error) {
debug.log(1, 'configdb', 'Error while deleting shade Device: ' + device.devID + ' Error: ' + error.message);
} else {
debug.log(5, 'configdb', 'Deleted device: ' + device.devID + ', ardID: ' + device.ardID);
}
});
} else {
debug.log(1, 'configdb', 'Incorrect devType used to delete a device: : ' + device.devType);
}
}
ConfigDB.prototype.deleteArduino = function(accountID, arduino) {
var debug = this.debug;
var SQLArduinoDelete = 'DELETE FROM arduinos WHERE ardID = ? AND raspyID = ? AND accountID = ?';
var SQLDevicesDelete = 'DELETE FROM devices WHERE ardID = ? AND raspyID = ? AND accountID = ?';
let SQLShadesDelete = 'DELETE FROM shades WHERE ardID = ? AND raspyID = ? AND accountID = ?';
var values = [];
var db = this.db;
values.push(arduino.ardID);
values.push(arduino.raspyID);
values.push(accountID);
//console.log('values: ' + values);
db.run(SQLDevicesDelete, values, function(error) {
if (error) {
debug.log(1, 'configdb', 'Error while deleting Arduino\'s devices: ' + error.message);
} else {
debug.log(5, 'configdb', 'Deleted Arduino\'s devices, accountid: ' + accountID + ', raspyid: ' +
arduino.raspyID + ', ardID: ' + arduino.ardID);
db.run(SQLShadesDelete, values, function(error) {
if (error) {
debug.log(1, 'configdb', 'Error while deleting Arduino\'s shades: ' + error.message);
} else {
debug.log(5, 'configdb', 'Deleted Arduino\'s shades, accountid: ' + accountID + ', raspyid: ' +
arduino.raspyID + ', ardID: ' + arduino.ardID);
db.run(SQLArduinoDelete, values, function(error) {
if (error) {
debug.log(1, 'configdb', 'Error while deleting Arduino: ' + error.message);
} else {
debug.log(5, 'configdb', 'Deleted Arduino, accountid: ' + accountID + ', raspyid: ' +
arduino.raspyID + ', ardID: ' + arduino.ardID);
}
});
}
});
}
});
}
/**
* Get all devices or single accountID, this should be executed on the raspy on startup, and on the cloud when:
* a) client connects to web GUI
* b) first data arrives over RCP and there is no mem structure for that accountID
*/
ConfigDB.prototype.getAllAccountDevices = function(accountID, callback) {
var debug = this.debug;
let SQLShades = 'SELECT * FROM shades WHERE accountID = ?';
var SQLDevices = 'SELECT * FROM devices WHERE accountID = ?';
var SQLArduinos = 'SELECT * FROM arduinos WHERE accountID = ?';
var SQLRaspys = 'SELECT * FROM raspys WHERE accountID = ?';
var db = this.db;
var raspys = {};
db.all(SQLRaspys, [accountID], function(error, rows) {
if (error) {
callback(error, null);
} else {
for (var i = 0; i < rows.length; i++) {
var row = rows[i];
raspys[row.raspyID] = {};
raspys[row.raspyID].vpnKey = row.vpnKey;
raspys[row.raspyID].vpnID = row.vpnID;
if (row.cloudService == '1')
raspys[row.raspyID].cloud = true;
else
raspys[row.raspyID].cloud = false;
raspys[row.raspyID].arduinos = {};
}
db.all(SQLArduinos, [accountID], function(error, rows) {
if (error) {
callback(error, null);
} else {
for (var i = 0; i < rows.length; i++) {
var row = rows[i];
var raspyID = row.raspyID;
var ardID = row.ardID;
var desc = row.desc;
if (typeof(raspys[raspyID]) == 'undefined') {
raspys[raspyID] = {};
raspys[raspyID].arduinos = {};
}
raspys[raspyID].arduinos[ardID] = {}
raspys[raspyID].arduinos[ardID].devices = {}
raspys[raspyID].arduinos[ardID].IP = row.IP;
raspys[raspyID].arduinos[ardID].raspyID = raspyID;
raspys[raspyID].arduinos[ardID].desc = desc;
raspys[raspyID].arduinos[ardID].mac = row.mac;
raspys[raspyID].arduinos[ardID].ctrlON = row.ctrlON;
raspys[raspyID].arduinos[ardID].mode = row.mode;
}
db.all(SQLDevices, [accountID], function(error, rows) {
if (error) {
callback(error, null);
} else {
for (var i = 0; i < rows.length; i++) {
var row = rows[i];
var raspyID = row.raspyID;
var ardID = row.ardID;
var devID = row.devID;
if (typeof(raspys[raspyID]) == 'undefined') {
raspys[raspyID] = {};
raspys[raspyID].arduinos = {};
}
if (typeof(raspys[raspyID].arduinos[ardID]) == 'undefined') {
raspys[raspyID].arduinos[ardID] = {};
raspys[raspyID].arduinos[ardID].devices = {};
raspys[raspyID].arduinos[ardID].alive = false;
}
raspys[raspyID].arduinos[ardID].devices[devID] = {};
raspys[raspyID].arduinos[ardID].devices[devID].activated = row.activated ? true : false;
raspys[raspyID].arduinos[ardID].devices[devID].ardID = row.ardID;
raspys[raspyID].arduinos[ardID].devices[devID].dataType = row.dataType;
raspys[raspyID].arduinos[ardID].devices[devID].date = JSON.parse(row.date);
raspys[raspyID].arduinos[ardID].devices[devID].desc = row.desc;
raspys[raspyID].arduinos[ardID].devices[devID].devType = row.devType;
raspys[raspyID].arduinos[ardID].devices[devID].devID = row.devID;
raspys[raspyID].arduinos[ardID].devices[devID].IP = row.IP;
raspys[raspyID].arduinos[ardID].devices[devID].raspyID = row.raspyID;
raspys[raspyID].arduinos[ardID].devices[devID].value = row.value;
raspys[raspyID].arduinos[ardID].devices[devID].alive = false;
raspys[raspyID].arduinos[ardID].devices[devID].lightType = row.lightType;
raspys[raspyID].arduinos[ardID].devices[devID].lightInputType = row.lightInputType
raspys[raspyID].arduinos[ardID].devices[devID].timer = row.timer;
raspys[raspyID].arduinos[ardID].devices[devID].ctrlON = row.ctrlON;
raspys[raspyID].arduinos[ardID].devices[devID].extType = row.extType;
debug.log(5, 'configdb', 'Reading device from DB: ' + JSON.stringify(raspys[raspyID].arduinos[ardID].devices[devID]));
}
//console.log(JSON.stringify(devices));
//callback(error, raspys);
}
db.all(SQLShades, [accountID], function(error, rows) {
if (error) {
callback(error, null);
} else {
for (var i = 0; i < rows.length; i++) {
var row = rows[i];
var raspyID = row.raspyID;
var ardID = row.ardID;
var devID = row.devID;
if (typeof(raspys[raspyID]) == 'undefined') {
raspys[raspyID] = {};
raspys[raspyID].arduinos = {};
}
if (typeof(raspys[raspyID].arduinos[ardID]) == 'undefined') {
raspys[raspyID].arduinos[ardID] = {};
raspys[raspyID].arduinos[ardID].devices = {};
raspys[raspyID].arduinos[ardID].alive = false;
}
raspys[raspyID].arduinos[ardID].devices[devID] = {};
raspys[raspyID].arduinos[ardID].devices[devID].activated = row.activated ? true : false;
raspys[raspyID].arduinos[ardID].devices[devID].ardID = row.ardID;
raspys[raspyID].arduinos[ardID].devices[devID].dataType = row.dataType;
raspys[raspyID].arduinos[ardID].devices[devID].date = JSON.parse(row.date);
raspys[raspyID].arduinos[ardID].devices[devID].desc = row.desc;
raspys[raspyID].arduinos[ardID].devices[devID].devType = row.devType;
raspys[raspyID].arduinos[ardID].devices[devID].devID = row.devID;
raspys[raspyID].arduinos[ardID].devices[devID].IP = row.IP;
raspys[raspyID].arduinos[ardID].devices[devID].raspyID = row.raspyID;
raspys[raspyID].arduinos[ardID].devices[devID].tilt = row.tilt;
raspys[raspyID].arduinos[ardID].devices[devID].position = row.position;
raspys[raspyID].arduinos[ardID].devices[devID].sync = row.sync ? true : false;
raspys[raspyID].arduinos[ardID].devices[devID].alive = false;
raspys[raspyID].arduinos[ardID].devices[devID].positionTimer = row.positionTimer;
raspys[raspyID].arduinos[ardID].devices[devID].tiltTimer = row.tiltTimer;
debug.log(5, 'configdb', 'Reading shade from DB: ' + JSON.stringify(raspys[raspyID].arduinos[ardID].devices[devID]));
}
//console.log(JSON.stringify(devices));
callback(error, raspys);
}
});
});
/**/
}
});
}
});
}
ConfigDB.prototype.getEverything = function(callback) {
var SQLAccounts = 'SELECT * FROM accounts';
var db = this.db;
var accounts = {};
db.all(SQLAccounts, [], function(error, rows) {
if (error) {
callback(error, null);
} else {
for (var i = 0; i < rows.length; i++) {
var row = rows[i];
let accountID = row.accountID;
var name = row.name;
var email = row.email;
var password = row.password;
accounts[accountID] = {};
accounts[accountID].raspys = {};
accounts[accountID].name = name;
accounts[accountID].email = email;
accounts[accountID].password = password;
require('./configdb.js').getAllAccountDevices(accountID, function(error, raspys) {
if (error) {
callback(error);
} else {
accounts[accountID].raspys = raspys;
}
});
}
callback(null, accounts);
}
});
}
var configdb = new ConfigDB();
module.exports = configdb;