Repository navigation
Expand file tree
/
Copy pathapi.js
More file actions
157 lines (147 loc) · 5.16 KB
/
Copy pathapi.js
File metadata and controls
157 lines (147 loc) · 5.16 KB
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
"use strict";
const { BL } = require("betterlogiclibrary");
module.exports = {
async internal({ homey, body: { action, logBookID, type } }) {
if(!homey.app.Log) throw new Error(homey.__('bllNotStartedYet'));
switch (action) {
case 'getLogBookIDs':
return homey.app.Log.LogBooks;
case 'submitLogBookForDiagnostic':
return homey.app.Log.submitLogBookForDiagnostic(logBookID);
case 'retrieveLogBookDiagnostic':
let s = homey.app.Log.retrieveLogBookDiagnostic(logBookID,);
if (type == 'base64') return Buffer.from(s).toString('base64');
else return s;
case 'writelogs':
let date = new Date();
for (let i = 0; i < 100; i++) {
date = new Date(date.setDate(date.getDate() - 1));
for (let j = 0; j < 10; j++) {
await homey.app.addLogToDB('Test message voor here is this', undefined, { severity: j === 0 ? 4 : undefined, timestamp: date });
}
}
break;
case 'getCount':
return homey.app.Log.logCount;
break;
}
},
async getLogDownloadUrl({ homey, params: { returnType } }) {
let callback = (link) => {
homey.api.realtime('downloadFileReady', link)
};
if (!homey.app.Log) throw new Error(homey.__('bllNotStartedYet'));
try {
switch (returnType) {
case 'csv': {
let filename = homey.app.settings && homey.app.settings.csvFilename ?
await BL.decode(homey.app.settings.csvFilename) :
"Logs " + BL.datetime.toString('datetimeshort') + '.csv';
const link = await BL.getDownloadUrl({ contentType: 'text/csv', filename });
homey.setTimeout(async () => {
const logs = await homey.app.Log.getLogs({ returnType: 'csv' });
await BL.setDownloadUrl({ link, text: logs.csv, callback });
}, 0);
return link;
}
break;
case 'xlsx': {
try {
let filename = homey.app.settings && homey.app.settings.xlsxFilename ?
await BL.decode(homey.app.settings.xlsxFilename) :
"Logs " + BL.datetime.toString('datetimeshort') + '.xlsx';
const link = await BL.getDownloadUrl({ contentType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', filename });
homey.setTimeout(async () => {
const logs = await homey.app.Log.getLogs({ returnType: 'xlsx' });
await BL.setDownloadUrl({ link, buffer: logs.xlsx, callback });
}, 0);
return link;
} catch (error) {
homey.error(error);
}
}
break;
case 'json': {
let filename = //false && //LET OP! false weg
homey.app.settings && homey.app.settings.jsonFilename ?
await BL.decode(homey.app.settings.jsonFilename) :
"Logs " + BL.datetime.toString('datetimeshort') + '.json';
const link = await BL.getDownloadUrl({ contentType: 'text/json', filename });
homey.setTimeout(async () => {
const logs = await homey.app.Log.getLogs({ returnType: 'json' });
//const text = JSON.stringify(logs.logs);
await BL.setDownloadUrl({ link, text: logs.json, callback });
}, 0);
return link;
}
}
if (returnType === 'csv') {
}
} catch (error) {
//homey.error(error);
let msg = error.message || error;
if (msg.indexOf('App Not Ready') > -1) throw new Error(homey.__('bllNotRunning'));
}
},
async getLogs({ homey }) {
if (!homey.app.Log) {
if(homey.app.usingNewLog)
throw new Error(homey.__('bllNotStartedYet'));
else
return homey.app.getBackLog();
}
//if(!homey.app.Log) throw new Error(homey.__('bllNotStartedYet'));
let result = await homey.app.Log.getLogs({ maxSizeKb: 900 });//BL._.take(, 1000);
return result;
//return homey.app.getLog();
},
async queryLogs({ homey, body }) {
if (!homey.app.Log) throw new Error(homey.__('bllNotStartedYet'));
body.maxSizeKb = 900;
let result = await homey.app.Log.getLogs(body);//BL._.take(, 1000);
return result;
},
async clearLog({ homey }) {
homey.app.apiClearLog();
return "OK";
},
async addLog({ homey, body }) {
homey.app.apiPutAddlog({ body });
return "OK";
}
};
// module.exports = [
// {
// description: "Add some logging.",
// method: "PUT",
// path: "/addlog/",
// requires_authorization: true,
// public: true,
// fn: function (data, callback) {
// Homey.app.apiPutAddlog(data);
// return callback(null, "OK");
// },
// },
// {
// description: "Clear logging.",
// method: "GET",
// path: "/clearlog/",
// requires_authorization: true,
// public: true,
// fn: function (data, callback) {
// Homey.app.apiClearLog();
// return callback(null, "OK");
// },
// },
// {
// description: "Get Logging.",
// method: "GET",
// path: "/",
// requires_authorization: true,
// public: true,
// fn: function (data, callback) {
// let log = Homey.app.getLog();
// return callback(null, log);
// },
// },
// ];