-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.js
1215 lines (1129 loc) · 40.2 KB
/
app.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
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* eslint-disable no-await-in-loop */
/*
Copyright 2017 - 2023 Robin de Gruijter
This file is part of com.gruijter.insights2csv.
com.gruijter.insights2csv is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
com.gruijter.insights2csv is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with com.gruijter.insights2csv. If not, see <http://www.gnu.org/licenses/>.
*/
'use strict';
const Homey = require('homey');
const { HomeyAPIApp } = require('homey-api');
// const { HomeyAPI } = require('homey-api');
const fs = require('fs');
const util = require('util');
const archiver = require('archiver');
const SMB2 = require('@marsaud/smb2');
const { createClient } = require('webdav');
const ftp = require('basic-ftp');
const Logger = require('./captureLogs');
const setTimeoutPromise = util.promisify(setTimeout);
// ============================================================
// Some helper functions here
const JSDateToExcelDate = (inDate) => {
// convert to yyyy-MM-dd HH:mm:ss
const dateTime = inDate.toISOString().replace(/T/, ' ').replace(/\..+/, '');
return dateTime;
};
class App extends Homey.App {
log2csv(logEntries, log) {
try {
const meta = {
entries: logEntries.values.length,
};
Object.keys(logEntries).forEach((key) => {
if (key === 'values') return;
meta[key] = logEntries[key];
});
const delimiter = ';';
const LocalDateTime = this.IncludeLocalDateTime.includeLocalDateTime ? `${delimiter}Local datetime` : '';
let { id } = logEntries;
if (id.indexOf(':') > -1) { id = id.split(':'); id = id[id.length - 1]; }
if (log.ownerUri === 'homey:manager:logic') id = log.title;
const header = `Zulu dateTime${delimiter}${id}${LocalDateTime}\r\n`;
let csv = header;
logEntries.values.forEach((e) => {
const entry = e;
const time = JSDateToExcelDate(new Date(entry.t));
const value = JSON.stringify(entry.v).replace('.', ',');
if (this.IncludeLocalDateTime.includeLocalDateTime) entry.tLocal = this.dateFormatter.format(new Date(entry.t));
csv += `${time}${delimiter}${value}${this.IncludeLocalDateTime.includeLocalDateTime ? delimiter + entry.tLocal : ''}\r\n`;
});
return { csv, meta }; // csv is string. meta is object.
} catch (error) {
return error;
}
}
async onInit() {
try {
if (!this.logger) this.logger = new Logger({ name: 'log', length: 200, homey: this.homey });
if (process.env.DEBUG === '1' || false) {
try {
// eslint-disable-next-line global-require
require('inspector').waitForDebugger();
} catch (error) {
// eslint-disable-next-line global-require
require('inspector').open(9325, '0.0.0.0', true);
}
}
// generic properties
this.homeyAPI = undefined;
this.devices = {};
this.logs = [];
this.allNames = [];
this.smb2Client = {};
this.webdavClient = {};
this.FTPClient = {};
this.webdavSettings = {};
this.smbSettings = {};
this.FTPSettings = {};
this.CPUSettings = {};
this.WaitBetweenEntities = {};
this.OnlyZipWithLogs = {};
this.resolutionSelection = ['lastHour', 'last6Hours', 'last24Hours', 'last7Days', 'last14Days', 'last31Days',
'last2Years', 'today', 'thisWeek', 'thisMonth', 'thisYear', 'yesterday', 'lastWeek', 'lastMonth', 'lastYear'];
// queue properties
this.abort = false;
this.queue = [];
this.head = 0;
this.tail = 0;
this.queueRunning = false;
// register some listeners
process.on('unhandledRejection', (error) => {
this.error('unhandledRejection! ', error);
});
process.on('uncaughtException', (error) => {
this.error('uncaughtException! ', error);
});
this.homey
.on('unload', () => {
this.log('app unload called');
// save logs to persistant storage
this.logger.saveLogs();
})
.on('memwarn', () => {
this.log('memwarn!');
// global.gc();
})
.on('cpuwarn', () => {
this.log('cpuwarn!');
});
this.homey.settings.on('set', (key) => {
this.log(`${key} changed from frontend`);
// this.FTPsettingsHaveChanged = true;
});
// ==============FLOW CARD STUFF======================================
const archiveAllAction = this.homey.flow.getActionCard('archive_all');
archiveAllAction
.registerRunListener(async (args) => {
this.log(`Exporting all insights ${args.resolution}`);
this.exportAll(args.resolution);
return Promise.resolve(true);
});
const archiveAppAction = this.homey.flow.getActionCard('archive_app');
archiveAppAction
.registerRunListener(async (args) => {
this.exportApp(args.selectedApp.id, args.resolution);
return Promise.resolve(true);
})
.registerArgumentAutocompleteListener(
'selectedApp',
async (query) => {
const results = this.allNames.filter((result) => { // filter for query on appId and appName
const appIdFound = result.id.toLowerCase().indexOf(query.toLowerCase()) > -1;
const appNameFound = result.name.toLowerCase().indexOf(query.toLowerCase()) > -1;
return appIdFound || appNameFound;
});
return Promise.resolve(results);
},
);
const purgeAction = this.homey.flow.getActionCard('purge');
purgeAction
.registerRunListener((args) => {
this.log(`Deleting old data on ${args.storage}`);
if (args.storage === 'FTP') {
this.purgeFTP(args.daysOld, args.types === 'allTypes');
}
if (args.storage === 'SMB') {
this.purgeSMB(args.daysOld, args.types === 'allTypes');
}
return Promise.resolve(true);
});
const archiveAllTypeFolderAction = this.homey.flow.getActionCard('archive_all_type_folder');
archiveAllTypeFolderAction
.registerRunListener(async (args) => {
this.log(`Exporting all insights ${args.resolution} of type ${args.type} into subfolder ${args.subfolder} `);
this.exportAll(args.resolution, args.type === 'all' ? undefined : args.type, args.subfolder
&& args.subfolder !== 'undefined' ? args.subfolder : undefined);
return Promise.resolve(true);
});
const archiveAppTypeFolderAction = this.homey.flow.getActionCard('archive_app_type_folder');
archiveAppTypeFolderAction
.registerRunListener(async (args) => {
this.exportApp(args.selectedApp.id, args.resolution, null, true, args.type === 'all' ? undefined : args.type, args.subfolder
&& args.subfolder !== 'undefined' ? args.subfolder : undefined);
return Promise.resolve(true);
})
.registerArgumentAutocompleteListener(
'selectedApp',
async (query) => {
const results = this.allNames.filter((result) => { // filter for query on appId and appName
const appIdFound = result.id.toLowerCase().indexOf(query.toLowerCase()) > -1;
const appNameFound = result.name.toLowerCase().indexOf(query.toLowerCase()) > -1;
return appIdFound || appNameFound;
});
return Promise.resolve(results);
},
);
this.deleteAllFiles();
await this.initExport(new Date());
// initiate test stuff from here
this.test();
this.log('ExportInsights App is running!');
} catch (error) {
this.error(error);
}
}
// ============================================================
// do the stuff from here
async test() {
try {
// await this.exportApp('weather', 'lastHour');
// await this.exportApp('com.gruijter.enelogic', 'last24Hours');
// await this.exportAll('last2Years');
// this.purgeSMB();
} catch (error) {
this.error(error);
}
}
// ============================================================
// stuff for queue handling here
async enQueue(item) {
this.queue[this.tail] = item;
this.tail += 1;
if (!this.queueRunning) {
this.queueRunning = true;
// await this.initExport(item.date);
this.runQueue();
}
}
deQueue() {
const size = this.tail - this.head;
if (size <= 0) return undefined;
const item = this.queue[this.head];
delete this.queue[this.head];
this.head += 1;
// Reset the counter
if (this.head === this.tail) {
this.head = 0;
this.tail = 0;
}
return item;
}
flushQueue() {
this.queue = [];
this.head = 0;
this.tail = 0;
this.queueRunning = false;
this.log('Export queue is flushed');
}
async runQueue() {
this.queueRunning = true;
const item = this.deQueue();
if (item) {
await this._exportApp(item.appId, item.resolution, item.date, item.type, item.subfolder)
.catch(this.error);
// wait a bit to reduce cpu and mem load?
if (global.gc) global.gc();
await setTimeoutPromise(this.CPUSettings && this.CPUSettings.lowCPU ? 10 * 1000 : 1, 'waiting is done');
this.runQueue();
} else {
this.queueRunning = false;
this.log('Finshed all exports');
}
}
// ============================================================
// stuff for frontend API here
deleteLogs() {
return this.logger.deleteLogs();
}
getLogs() {
return this.logger.logArray;
}
testSmb(smbSettings) {
this.log('testing SMB settings from frontend');
return new Promise((resolve, reject) => {
try {
const smb2Client = new SMB2({
share: smbSettings.smbShare.replace(/\//gi, '\\'),
domain: smbSettings.smbDomain,
username: smbSettings.smbUsername,
password: smbSettings.smbPassword,
autoCloseTimeout: 5000,
});
let path = `${smbSettings.smbPath.replace(/\//gi, '\\')}\\`;
if (smbSettings.smbPath === '') {
path = '';
}
smb2Client.writeFile(`${path}insights2csv.txt`, 'Homey can write to this folder!', { flag: 'w' }, (error) => {
if (error) {
this.log(error.message);
reject(error);
} else {
this.log('Connection successfull!');
resolve(true);
}
});
} catch (error) {
this.error(error);
reject(error);
}
});
}
async testWebdav(webdavSettings) {
this.log('testing WebDAV settings from frontend');
try {
const webdavClient = createClient(
webdavSettings.webdavUrl,
{
username: webdavSettings.webdavUsername,
password: webdavSettings.webdavPassword,
},
);
await webdavClient.putFileContents('insights2csv.txt', 'Homey can write to this folder!');
const quota = await webdavClient.getQuota();
this.log('Connection successfull!');
return Promise.resolve(quota);
} catch (error) {
this.error(error);
return Promise.reject(error);
}
}
async testFTP(FTPSettings) {
this.log('testing FTP settings from frontend');
try {
const client = new ftp.Client();
client.ftp.verbose = true;
await client.access({
host: FTPSettings.FTPHost,
port: FTPSettings.FTPPort,
user: FTPSettings.FTPUsername,
password: FTPSettings.FTPPassword,
secure: FTPSettings.useSFTP,
secureOptions: { rejectUnauthorized: false },
});
await client.ensureDir(FTPSettings.FTPFolder);
// console.log(await client.list());
this.log('Connection successfull!');
client.close();
return Promise.resolve();
} catch (error) {
this.error(error);
return Promise.reject(error);
}
}
getResolutions() {
return this.resolutionSelection;
}
getAppList() {
return this.allNames;
}
async exportAll(resolution, type, subfolder) {
const date = new Date();
await this.initExport(date);
this.allNames.forEach((name) => {
this.exportApp(name.id, resolution, date, false, type, subfolder);
});
return true;
}
async exportApp(appId, resolution, _date, reload, type, subfolder) {
// date = date || new Date();
const date = new Date();
if (reload !== false) await this.initExport(date);
this.enQueue({
appId, resolution, date, type, subfolder,
});
return true;
}
stopExport() {
if (this.queueRunning) this.log('aborting export');
this.abort = true;
this.flushQueue();
return true;
}
// ============================================================
// Local file handling in app userdata folder
deleteAllFiles() {
fs.readdir('/userdata/', (err, res) => {
if (err) {
return this.log(err);
}
res.forEach((elem) => {
if (elem !== 'log.json') {
fs.unlink(`/userdata/${elem}`, (error) => {
if (error) { this.log(error); } else { this.log(`deleted ${elem}`); }
});
}
});
return this.log('all local files deleted');
});
}
deleteFile(filename) {
fs.unlink(`/userdata/${filename}`, (error) => {
if (error) { this.log(error); }
// else { this.log(`deleted ${filename}`); }
});
}
// ============================================================
// Homey API stuff here
async loginHomeyApi() {
if (this.homeyAPI) return Promise.resolve(this.homeyAPI);
// Authenticate against the current Homey.
this.homeyAPI = new HomeyAPIApp({ homey: this.homey, $timeout: 90000 });
// this.homeyAPI = await HomeyAPI.createAppAPI({
// homey: this.homey,
// $timeout: 90000
// });
return Promise.resolve(this.homeyAPI);
}
async getAllLogs() {
this.logs = Object.values(await this.homeyAPI.insights.getLogs({ $timeout: 60000 }));
return Promise.resolve(this.logs);
}
async getAllDevices() {
this.devices = await this.homeyAPI.devices.getDevices({ $timeout: 60000 });
return Promise.resolve(this.devices);
}
// Get a list of all app names
async getAppNameList() {
try {
const allApps = await this.homeyAPI.apps.getApps({ $timeout: 60000 });
const mappedArray = Object.entries(allApps).map((app) => {
const map = {
id: app[1].id,
name: app[1].name,
icon: `${app[1].id}${app[1].icon}`,
type: 'app',
};
return map;
});
return Promise.all(mappedArray);
} catch (error) {
return Promise.reject(error);
}
}
// getUriObj(log) {
// if (log.uriObj) return log.uriObj;
// else
// {
// this.log(log);
// return {id:log.ownerId};
// }
// }
// Get a list of all logged manager names
async getManagerNameList() {
try {
// eslint-disable-next-line prefer-destructuring
const logs = this.logs; // Object.values(await this.homeyAPI.insights.getLogs({ $timeout: 60000 }));
const list = logs.filter((log) => (log.uriObj ? log.uriObj.type === 'manager' : log.ownerUri.startsWith('homey:manager:')))
.map((log) => {
// let uriObj = this.getUriObj(log);
const ids = log.ownerUri ? log.ownerUri.split(':') : null;
const id = ids ? ids[ids.length - 1] : null;
const name = (log.uriObj ? log.uriObj.name : log.ownerName) || id; // : app ? app.name : null;
if (!name) return null;
const _app = {
id: log.uriObj ? log.uriObj.id : (id), // + log.ownerId
name,
icon: '',
type: log.uriObj ? log.uriObj.type : ids[1], // app ? app.type : null,
};
return _app;
});
const uniqueList = list.filter((elem, index) => elem && index === list.findIndex((obj) => JSON.stringify(obj) === JSON.stringify(elem)));
return Promise.all(uniqueList);
} catch (error) {
return Promise.reject(error);
}
}
async getAllNames() {
try {
const managerNameList = await this.getManagerNameList();
const appNameList = await this.getAppNameList();
this.allNames = appNameList.concat(managerNameList);
return Promise.all(this.allNames);
} catch (error) {
return Promise.reject(error);
}
}
async getAppRelatedLogs(appId, type) {
try {
this.log(`getting logs related to ${appId}`);
let appRelatedLogs = [];
const appUri = `homey:app:${appId}`;
const managerUri = `homey:manager:${appId}`;
// look for app logs
const appLogs = this.logs.filter((log) => ((log.ownerUri && (log.ownerUri === appUri || log.ownerUri === managerUri))
|| (log.uri === appUri || log.uri === managerUri)) && (!type || log.type === type));
appRelatedLogs = appRelatedLogs.concat(appLogs);
// find app related devices and add their logs
Object.keys(this.devices).forEach((key) => {
if (this.devices[key].ownerUri === appUri || this.devices[key].driverUri === appUri
|| (this.devices[key].driverId && this.devices[key].driverId.startsWith(appUri))) {
const deviceUri = `homey:device:${this.devices[key].id}`;
const deviceLogs = this.logs.filter((log) => (log.ownerUri === deviceUri
|| log.uri === deviceUri) && (!type || log.type === type));
appRelatedLogs = appRelatedLogs.concat(deviceLogs);
}
});
return Promise.resolve(appRelatedLogs);
} catch (error) {
return Promise.reject(error);
}
}
/**
*
* @param {*} log
* @param {*} resolution
* @param {Date} date
* @returns
*/
async getLogEntries(log, resolution, date) {
try {
const opts = {
uri: (log.uri || log.ownerUri),
id: log.id,
$timeout: 90000,
};
if (log.type !== 'boolean') {
opts.resolution = resolution;
}
const logEntries = await this.homeyAPI.insights.getLogEntries(opts);
if (log.type === 'boolean') {
// const date = new Date();
const dateTimezoned = new Date(this.enDateFormatter.format(date));
const hourOffset = date.getHours() - dateTimezoned.getHours();
let _dateFrom;
let _dateTo;
switch (resolution) {
case 'lastHour':
_dateFrom = new Date(new Date(date).setHours(date.getHours() - 1));
logEntries.values = logEntries.values.filter((x) => new Date(x.t) >= _dateFrom);
break;
case 'last6Hours':
_dateFrom = new Date(new Date(date).setHours(date.getHours() - 6));
logEntries.values = logEntries.values.filter((x) => new Date(x.t) >= _dateFrom);
break;
case 'last24Hours':
_dateFrom = new Date(new Date(date).setHours(date.getHours() - 24));
logEntries.values = logEntries.values.filter((x) => new Date(x.t) >= _dateFrom);
break;
case 'last7Days':
_dateFrom = new Date(new Date(date).setDate(date.getDate() - 7));
logEntries.values = logEntries.values.filter((x) => new Date(x.t) >= _dateFrom);
break;
case 'last14Days':
_dateFrom = new Date(new Date(date).setDate(date.getDate() - 14));
logEntries.values = logEntries.values.filter((x) => new Date(x.t) >= _dateFrom);
break;
case 'last31Days':
_dateFrom = new Date(new Date(date).setDate(date.getDate() - 31));
logEntries.values = logEntries.values.filter((x) => new Date(x.t) >= _dateFrom);
break;
case 'today':
_dateFrom = new Date(new Date(dateTimezoned.getFullYear(), dateTimezoned.getMonth(), dateTimezoned.getDate(), 0, 0, 0, 0));
_dateTo = new Date(date);
_dateFrom = new Date(new Date(_dateFrom).setHours(_dateFrom.getHours() + hourOffset));
logEntries.values = logEntries.values.filter((x) => new Date(x.t) >= _dateFrom && new Date(x.t) <= _dateTo);
break;
case 'yesterday':
_dateFrom = new Date(new Date(dateTimezoned.getFullYear(), dateTimezoned.getMonth(), dateTimezoned.getDate(), 0, 0, 0, 0)
.setDate(dateTimezoned.getDate() - 1));
_dateTo = new Date(new Date(_dateFrom).setDate(_dateFrom.getDate() + 1));
_dateFrom = new Date(new Date(_dateFrom).setHours(_dateFrom.getHours() + hourOffset));
_dateTo = new Date(new Date(_dateTo).setHours(_dateTo.getHours() + hourOffset));
logEntries.values = logEntries.values.filter((x) => new Date(x.t) >= _dateFrom && new Date(x.t) < _dateTo);
break;
case 'thisWeek':
_dateFrom = new Date(new Date(dateTimezoned.getFullYear(), dateTimezoned.getMonth(), dateTimezoned.getDate(), 0, 0, 0, 0)
.setDate(dateTimezoned.getDate() - (dateTimezoned.getDay() - 1)));
_dateTo = new Date(date);
_dateFrom = new Date(new Date(_dateFrom).setHours(_dateFrom.getHours() + hourOffset));
logEntries.values = logEntries.values.filter((x) => new Date(x.t) >= _dateFrom && new Date(x.t) <= _dateTo);
break;
case 'lastWeek':
_dateFrom = new Date(new Date(dateTimezoned.getFullYear(), dateTimezoned.getMonth(), dateTimezoned.getDate(), 0, 0, 0, 0)
.setDate(dateTimezoned.getDate() - (dateTimezoned.getDay() - 1) - 7));
_dateTo = new Date(new Date(_dateFrom).setDate(_dateFrom.getDate() + 7));
_dateFrom = new Date(new Date(_dateFrom).setHours(_dateFrom.getHours() + hourOffset));
_dateTo = new Date(new Date(_dateTo).setHours(_dateTo.getHours() + hourOffset));
logEntries.values = logEntries.values.filter((x) => new Date(x.t) >= _dateFrom && new Date(x.t) < _dateTo);
break;
case 'thisMonth':
_dateFrom = new Date(new Date(dateTimezoned.getFullYear(), dateTimezoned.getMonth(), 1, 0, 0, 0, 0));
_dateTo = new Date(date);
_dateFrom = new Date(new Date(_dateFrom).setHours(_dateFrom.getHours() + hourOffset));
logEntries.values = logEntries.values.filter((x) => new Date(x.t) >= _dateFrom && new Date(x.t) <= _dateTo);
break;
case 'lastMonth':
_dateFrom = new Date(new Date(dateTimezoned.getFullYear(), dateTimezoned.getMonth(), 1, 0, 0, 0, 0)
.setMonth(dateTimezoned.getMonth() - 1));
_dateTo = new Date(new Date(_dateFrom).setMonth(_dateFrom.getMonth() + 1));
_dateFrom = new Date(new Date(_dateFrom).setHours(_dateFrom.getHours() + hourOffset));
_dateTo = new Date(new Date(_dateTo).setHours(_dateTo.getHours() + hourOffset));
logEntries.values = logEntries.values.filter((x) => new Date(x.t) >= _dateFrom && new Date(x.t) < _dateTo);
break;
case 'thisYear':
_dateFrom = new Date(new Date(dateTimezoned.getFullYear(), 0, 1, 0, 0, 0, 0));
_dateTo = new Date(date);
_dateFrom = new Date(new Date(_dateFrom).setHours(_dateFrom.getHours() + hourOffset));
logEntries.values = logEntries.values.filter((x) => new Date(x.t) >= _dateFrom && new Date(x.t) <= _dateTo);
break;
case 'lastYear':
_dateFrom = new Date(new Date(dateTimezoned.getFullYear(), 0, 1, 0, 0, 0, 0).setFullYear(dateTimezoned.getFullYear() - 1));
_dateTo = new Date(new Date(_dateFrom).setFullYear(_dateFrom.getFullYear() + 1));
_dateFrom = new Date(new Date(_dateFrom).setHours(_dateFrom.getHours() + hourOffset));
_dateTo = new Date(new Date(_dateTo).setHours(_dateTo.getHours() + hourOffset));
logEntries.values = logEntries.values.filter((x) => new Date(x.t) >= _dateFrom && new Date(x.t) < _dateTo);
break;
case 'last2Years':
_dateFrom = new Date(new Date(date).setFullYear(date.getFullYear() - 2));
_dateTo = new Date(date);
logEntries.values = logEntries.values.filter((x) => new Date(x.t) >= _dateFrom && new Date(x.t) <= _dateTo);
break;
default:
throw Error('invalid resolution', resolution);
}
}
if (logEntries.values.length > 2925) {
this.error(`Insights data is corrupt and will be truncated to the first 2925 records for
${(log.uriObj ? log.uriObj.name : log.ownerName)} ${logEntries.id}.`);
// ${logEntries.uri}`);
logEntries.values = logEntries.values.slice(0, 2925);
if (global.gc) global.gc();
await setTimeoutPromise(this.CPUSettings && this.CPUSettings.lowCPU ? 10 * 1000 : 1, 'waiting is done');
}
return Promise.resolve(logEntries);
} catch (error) {
if (global.gc) global.gc();
await setTimeoutPromise(this.CPUSettings && this.CPUSettings.lowCPU ? 10 * 1000 : 1, 'waiting is done');
// await setTimeoutPromise(10 * 1000, 'waiting is done');
return Promise.reject(error);
}
}
async initExport(date) {
try {
this.abort = false;
this.webdavSettings = this.homey.settings.get('webdavSettings');
this.smbSettings = this.homey.settings.get('smbSettings');
this.FTPSettings = this.homey.settings.get('FTPSettings');
this.CPUSettings = this.homey.settings.get('CPUSettings');
this.timeZone = this.homey.clock.getTimezone();
this.locale = await this.homey.i18n.getLanguage();
this.dateFormatter = new Intl.DateTimeFormat(this.locale, {
timeZone: this.timeZone,
// dateStyle: 'medium',
// timeStyle: 'medium',
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
});
this.enDateFormatter = new Intl.DateTimeFormat('en', {
timeZone: this.timeZone,
// dateStyle: 'medium',
// timeStyle: 'medium',
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
});
// this.WaitBetweenEntities = this.homey.settings.get('WaitBetweenEntities');
// if (!this.WaitBetweenEntities) {
// this.WaitBetweenEntities = { waitBetweenEntities: 0 };
// this.homey.settings.set('WaitBetweenEntities', this.WaitBetweenEntities);
// }
// if (typeof (this.WaitBetweenEntities.waitBetweenEntities) == 'string') this.WaitBetweenEntities.waitBetweenEntities = Number.parseInt(this.WaitBetweenEntities.waitBetweenEntities);
this.OnlyZipWithLogs = this.homey.settings.get('OnlyZipWithLogs');
if (!this.OnlyZipWithLogs) {
this.OnlyZipWithLogs = { onlyZipWithLogs: false };
this.homey.settings.set('OnlyZipWithLogs', this.OnlyZipWithLogs);
}
this.IncludeLocalDateTime = this.homey.settings.get('IncludeLocalDateTime');
if (!this.IncludeLocalDateTime) {
this.IncludeLocalDateTime = { includeLocalDateTime: false };
this.homey.settings.set('IncludeLocalDateTime', this.IncludeLocalDateTime);
}
if (this.CPUSettings && this.CPUSettings.lowCPU) this.log('Low CPU load selected for export');
this.timestamp = date.toISOString()
.replace(/:/g, '') // delete :
.replace(/-/g, '') // delete -
.replace(/\..+/, 'Z'); // delete the dot and everything after
await this.loginHomeyApi();
await this.getAllLogs();
await this.getAllDevices();
await this.getAllNames();
return Promise.resolve(true);
} catch (error) {
return Promise.reject(error);
}
}
// ============================================================
// WebDAV file handling here
getWebdavClient() {
try {
this.webdavClient = createClient(
this.webdavSettings.webdavUrl,
{
username: this.webdavSettings.webdavUsername,
password: this.webdavSettings.webdavPassword,
},
);
return Promise.resolve(this.webdavClient);
} catch (error) {
return Promise.reject(error);
}
}
// save a file to WebDAV as promise; resolves webdav filename
async saveWebDav(fileName, subfolder) { // filename is appId
try {
await this.getWebdavClient();
const folder = subfolder ? `/${subfolder}` : '';
let webdavFileName = `${folder}/${fileName}`;
// save to seperate folder when selected by user
if (this.webdavSettings.webdavUseSeperateFolders) {
await this.webdavClient.createDirectory(`${folder}/${this.timestamp}/`)
.then(() => {
this.log(`${this.timestamp} folder created!`);
})
.catch(() => null);
webdavFileName = `${folder}/${this.timestamp}/${fileName}`;
}
const options = {
format: 'binary',
overwrite: true,
};
const webDavWriteStream = this.webdavClient.createWriteStream(webdavFileName, options);
const fileStream = fs.createReadStream(`/userdata/${fileName}`);
return new Promise((resolve, reject) => {
fileStream.on('open', () => {
// this.log('piping to webdav');
fileStream.pipe(webDavWriteStream);
});
fileStream.on('close', () => {
// The file has been read completely
this.log(`${fileName} has been saved to webDav`);
webDavWriteStream.end();
resolve(fileName);
});
fileStream.on('error', (err) => {
this.log('filestream error: ', err);
});
webDavWriteStream.on('error', (err) => {
this.error(err.message);
this.log('webdavwritestream error');
return reject(err);
});
});
} catch (error) {
// this.error('error:', error);
return Promise.reject(error);
}
}
// ============================================================
// SMB file handling here
// create SMB client
getSmb2Client() {
try {
this.smb2Client = new SMB2({
share: this.smbSettings.smbShare.replace(/\//gi, '\\'),
domain: this.smbSettings.smbDomain,
username: this.smbSettings.smbUsername,
password: this.smbSettings.smbPassword,
autoCloseTimeout: 0,
});
return Promise.resolve(this.smb2Client);
} catch (error) {
return Promise.reject(error);
}
}
// save a file to a network share via SMB as promise; resolves smb2 filename
async saveSmb(fileName, subfolder) {
try {
await this.getSmb2Client();
const folder = subfolder ? `${subfolder}\\` : '';
let path = `${this.smbSettings.smbPath.replace(/\//gi, '\\')}\\${folder}`;
if (this.smbSettings.smbPath === '') {
path = `${folder}`;
}
// save to seperate folder when selected by user
if (this.smbSettings.smbUseSeperateFolders) {
path = `${path}${this.timestamp}\\`;
if (this.smbSettings.smbPath === '') {
path = `${this.timestamp}\\`;
}
const folderExists = await new Promise((resolve, reject) => {
this.smb2Client.exists(path, async (error, exists) => {
if (error) reject(error);
// console.log(exists ? "it's there" : "it's not there!");
resolve(exists);
});
});
if (!folderExists) {
await new Promise((res, rej) => {
this.smb2Client.mkdir(path, (err) => {
if (err) rej(err);
this.log(`${path} folder created!`);
res(true);
});
});
}
}
return new Promise((resolve, reject) => {
this.smb2Client.createWriteStream(`${path}${fileName}`, { flag: 'w' }, (error, smbWriteStream) => {
if (error) {
this.error(error);
reject(error);
return;
}
const fileStream = fs.createReadStream(`/userdata/${fileName}`);
fileStream.on('open', () => {
// this.log('piping to SMB2');
fileStream.pipe(smbWriteStream);
});
fileStream.on('close', () => {
// The file has been read completely
this.log(`${fileName} has been saved to SMB2`);
fileStream.unpipe();
resolve(fileName);
});
fileStream.on('error', (err) => {
this.log('filestream error: ', err);
// fileStream.unpipe();
// smbWriteStream.end();
reject(err);
});
});
});
} catch (error) {
// this.error('error:', error);
// this.smb2Client.close();
// this.getSmb2Client();
return Promise.reject(error);
}
}
// purge SMB folder
async purgeSMB(daysOld, allTypes) {
try {
let selectList = [];
await this.getSmb2Client();
let path = `${this.smbSettings.smbPath.replace(/\//gi, '\\')}\\`;
if (this.smbSettings.smbPath === '') {
path = '';
}
return new Promise((resolve, reject) => {
this.smb2Client.readdir(path, (err, files) => {
if (err) return reject(err);
selectList = files;
// select only zip files
if (!allTypes) {
selectList = selectList.filter((item) => {
const isZip = item.toLowerCase().slice(-4) === '.zip';
return isZip;
});
}
// select older then daysOld
selectList = selectList.filter((item) => {
let dateString = item.match(/_(.*?)_/)[1];
if (dateString.length !== 16) return false;
const YYYY = dateString.substring(0, 4);
const MM = dateString.substring(4, 6);
const DD = dateString.substring(6, 8);
const hh = dateString.substring(9, 11);
const mm = dateString.substring(11, 13);
const ss = dateString.substring(13, 15);
dateString = `${YYYY}-${MM}-${DD}T${hh}:${mm}:${ss}Z`;
const days = (Date.now() - new Date(dateString)) / 1000 / 60 / 60 / 24;
return days > daysOld;
});
// delete the file or folder
for (let idx = 0; idx < selectList.length; idx += 1) {
const item = selectList[idx];
this.log(`removing SMB file ${item}`);
this.smb2Client.unlink(`${path}${item}`, (error) => {
if (error) this.error(error);
});
}
return resolve(selectList);
});
});
} catch (error) {
return Promise.reject(error);
}
}
// ============================================================
// FTP file handling here
// create FTP client
async getFTPClient() {
try {
if ((this.FTPClient.closed === false) && !this.FTPsettingsHaveChanged) {
return Promise.resolve(this.FTPClient);
}
if (this.FTPClient instanceof ftp.Client) {
this.log('closing FTP client');
this.FTPClient.close();
}
this.FTPClient = new ftp.Client();
// client.ftp.verbose = true;
this.FTPsettingsHaveChanged = false;
await this.FTPClient.access({
host: this.FTPSettings.FTPHost,
port: this.FTPSettings.FTPPort,
user: this.FTPSettings.FTPUsername,
password: this.FTPSettings.FTPPassword,
secure: this.FTPSettings.useSFTP,
secureOptions: {
host: this.FTPSettings.FTPHost,
rejectUnauthorized: false,
},
});
return Promise.resolve(this.FTPClient);
} catch (error) {
return Promise.reject(error);
}
}
// save a file to a network share via FTP as promise; resolves FTP filename
async saveFTP(fileName, subfolder) {
try {
const sub = subfolder ? `/${subfolder}` : '';
const folder = `//${this.FTPSettings.FTPFolder}${sub}`;
await this.getFTPClient();
await this.FTPClient.ensureDir(`//${folder}`);
// save to seperate folder when selected by user
if (this.FTPSettings.FTPUseSeperateFolders) {
await this.FTPClient.ensureDir(`//${folder}/${this.timestamp}`);
}
const fileStream = fs.createReadStream(`/userdata/${fileName}`);
await this.FTPClient.upload(fileStream, fileName);
this.log(`${fileName} has been saved to FTP(S)`);
return Promise.resolve(fileName);
} catch (error) {
this.error('error:', error);
return Promise.reject(error);
}
}
// purge FTP folder
async purgeFTP(daysOld, allTypes) {
try {
await this.getFTPClient();
await this.FTPClient.ensureDir(`//${this.FTPSettings.FTPFolder}`);