-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapp.js
128 lines (107 loc) · 3.63 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
'use strict';
const Homey = require('homey');
const ESPEasyUnits = require('./lib/ESPEasyUnits.js');
const Telemetry = require('./lib/Telemetry.js');
class ESPEasy extends Homey.App {
onInit() {
if (process.env.DEBUG === '1') {
require('inspector').open(9222, '0.0.0.0', false);
process.stdout.write = () => {}
}
this.triggers = {};
this.actions = {};
this.units = new ESPEasyUnits();
this.telemetry = new Telemetry({
host: 'espeasy.homey.joolee.nl',
siteId: 2
});
this.units.on('unit-initialized', this.unitInitialized.bind(this));
// When, just before time, number of units is still 0, send that telemetry
setTimeout(this.unitInitialized.bind(this), this.telemetry.initialTimeout - 1000);
}
onUninit() {
this.telemetry.send('App', 'Uninit', '/app/uninit', {});
}
unitInitialized(unit) {
if (!this.telemetry.appInitialized && this.units.getAll().length >= this.units.getOnline().length) {
this.updateTelemetry('Initialized', '/app/initialized', false);
this.telemetry.appInitialized = true;
}
}
get supportedTasks() {
if (this._supportedTasks)
return this._supportedTasks;
this._supportedTasks = Object.values(Homey.ManagerDrivers.getDrivers()).flatMap(driver => {
if (driver.taskTypes)
return driver.taskTypes.map(type => `${type.plugin} - ${type.name}`);
else
return [];
}).sort((a, b) => a.localeCompare(b, undefined, {
numeric: true
}));
this._supportedTasks.unshift('26 - Generic - System Info');
return this._supportedTasks;
}
getI18nString(i18n) {
const lang = Homey.ManagerI18n.getLanguage();
if (i18n[lang])
return i18n[lang];
else if (i18n['en'])
return i18n['en'];
else
return `Untranslated string: ${i18n}`;
}
safeIncrement(i) {
// I don't want a crash because of an app that runs too long :)
if (i++ >= Number.MAX_SAFE_INTEGER - 10) {
return 0;
}
return i;
}
// This was a re-implementation of Homeylib.getCapability to include custom capabilities
// I removed Homeylib alltogether to reduce dependencies
getCapability(capability) {
return this.getCapabilities()[capability];
}
// This was a re-implementation of Homeylib.getCapabilities to include custom capabilities
// I removed Homeylib alltogether to reduce dependencies
// The list in assets/json/allCapabilities.json is generated by tools/create-capabilitylist.sh
// The data source is still HomeyLib
// Note: Only properties 'title', 'type', 'getable', 'setable', 'min', 'max' and 'uiComponent' are included
getCapabilities() {
const defaultCapabilities = require("/assets/json/allCapabilities.json");
const customCapabilities = this.manifest["capabilities"];
return {
...defaultCapabilities,
...customCapabilities
};
}
updateTelemetry(reason, url, recurse) {
try {
const onlineUnits = this.units.getOnline();
let metrics = {
"Total tasks": onlineUnits.reduce((numTasks, unit) => numTasks + unit.tasks.length, 0),
"Total tasks in use": onlineUnits.reduce((numTasks, unit) => {
numTasks += unit.sensors.length;
numTasks += unit.getTasksByName(26, 'Generic - System Info', false).length;
return numTasks;
}, 0),
"Total units": this.units.getAll().length,
"Total GPIO used": onlineUnits.reduce((numTasks, unit) => numTasks + unit.gpios.length, 0),
}
this.telemetry.send('App', reason, url, metrics);
} catch (error) {
this.error('Error updating app telemetry:', error);
}
if (recurse) {
this.units.getOnline().forEach(unit => {
try {
unit.updateTelemetry(reason, recurse)
} catch (error) {
this.error('Error updating unit telemetry:', unit.name, error);
}
});
}
}
}
module.exports = ESPEasy;