-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapp.js
More file actions
85 lines (70 loc) · 2.13 KB
/
app.js
File metadata and controls
85 lines (70 loc) · 2.13 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
'use strict';
const Homey = require('homey');
const moment = require('moment');
class Polestar extends Homey.App {
async onInit() {
this.userLanguage = this.homey.i18n.getLanguage();
moment.locale(this.userLanguage == 'no' ? 'nb' : 'en');
this.homey.settings.set('debugLog', null);
this.log(this.homey.__({
en: 'Polestar App has been initialized',
no: 'Polestar App har blitt initialisert'
}));
this.homey.settings.on('set', (key) => {
if (key === 'debugLog') return;
this.log(this.homey.__({
en: 'Setting updated:',
no: 'Innstilling oppdatert:'
}), 'Polestar App', 'DEBUG', `${key}: ${key == 'polestar_token' ? '********' : this.homey.settings.get(key)}`);
});
}
async log(message, instance = 'Polestar App', severity = 'DEBUG', data = null) {
const now = new Date();
let datestring = now.toLocaleDateString(this.userLanguage, {
dateStyle: 'short',
timeZone: 'Europe/Oslo'
});
let timestring = now.toLocaleTimeString(this.userLanguage, {
timeStyle: 'medium',
timeZone: 'Europe/Oslo'
});
let debugDateString = `${datestring} ${timestring}`;
datestring = `${datestring} - ${timestring}`;
const debugLog = this.homey.settings.get('debugLog') || [];
const entry = { registered: debugDateString, severity, message };
switch (severity) {
case 'DEBUG':
severity = '\x1b[36mDEBUG\x1b[0m';
break;
case 'WARNING':
severity = '\x1b[33mWARNING\x1b[0m';
break;
case 'ERROR':
severity = '\x1b[31mERROR\x1b[0m';
break;
default:
severity = '\x1b[36mDEBUG\x1b[0m';
break;
}
const logMessage = `${datestring} [${instance}] [${severity}] ${message}`;
if (data) {
console.log(logMessage, data || '');
if (typeof data === 'string') {
entry.data = { data };
} else if (data.message) {
entry.data = { error: data.message, stacktrace: data.stack };
} else {
entry.data = data;
}
} else {
console.log(logMessage || '');
}
debugLog.push(entry);
if (debugLog.length > 100) {
debugLog.splice(0, 1);
}
this.homey.settings.set('debugLog', debugLog);
this.homey.api.realtime('debugLog', entry);
}
}
module.exports = Polestar;