generated from Dennis-Rosenbaum/MMM-Template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnode_helper.js
470 lines (424 loc) · 14.9 KB
/
node_helper.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
const { writeFile, readFile, access, constants } = require("node:fs");
const path = require("node:path");
const cron = require("node-cron");
const { CronExpressionParser } = require("cron-parser");
const dayjs = require("dayjs");
const isBetween = require("dayjs/plugin/isBetween");
dayjs.extend(isBetween);
const NodeHelper = require("node_helper");
var log = () => { /* do nothing */ };
module.exports = NodeHelper.create({
start () {
this.Linky = null;
this.config = null;
this.dates = [];
this.timer = null;
this.consumptionData = {};
this.cronExpression = "0 0 14 * * *";
this.error = null;
this.dataFile = path.resolve(__dirname, "linkyData.json");
this.timers = {};
},
socketNotificationReceived (notification, payload) {
switch (notification) {
case "INIT":
if (!this.ready) {
this.config = payload;
this.ready = true;
this.chartData = {};
this.initialize();
} else {
this.initWithCache();
}
break;
}
},
// intialisation de MMM-Linky
async initialize () {
this.catchError();
console.log(`[LINKY] MMM-Linky Version: ${require("./package.json").version} Revison: ${require("./package.json").rev}`);
if (this.config.debug) log = (...args) => { console.log("[LINKY]", ...args); };
if (this.config.dev) log("Config:", this.config);
await this.readChartData();
if (Object.keys(this.chartData).length) {
this.sendSocketNotification("DATA", this.chartData);
}
else {
this.getConsumptionData();
}
this.scheduleDataFetch();
},
// Initialisation de l'api linky
async initLinky (callback) {
const { Session } = await this.loadLinky();
try {
this.Linky = new Session(this.config.token, this.config.prm);
log("API linky Prête");
if (callback) callback();
} catch (error) {
console.error(`[LINKY] ${error}`);
this.error = error.message;
this.sendSocketNotification("ERROR", this.error);
}
},
// Utilisation du cache interne lors d'une utilisation du "mode Server"
initWithCache () {
console.log(`[LINKY] [Cache] MMM-Linky Version: ${require("./package.json").version} Revison: ${require("./package.json").rev}`);
if (this.error) this.sendSocketNotification("ERROR", this.error);
if (Object.keys(this.chartData).length) this.sendSocketNotification("DATA", this.chartData);
if (Object.keys(this.timers).length) this.sendTimers();
},
// Récupération planifié des données
scheduleDataFetch () {
const randomMinute = Math.floor(Math.random() * 59);
const randomSecond = Math.floor(Math.random() * 59);
this.cronExpression = `${randomSecond} ${randomMinute} 14 * * *`;
cron.schedule(this.cronExpression, () => {
log("Exécution de la tâche planifiée de récupération des données.");
this.getConsumptionData();
this.displayNextCron();
});
this.displayNextCron();
},
// Récupération des données
async getConsumptionData () {
this.Dates = this.calculateDates();
if (this.Dates === null) return;
log("Dates:", this.Dates);
if (!this.Linky) {
this.initLinky(() => this.getConsumptionData());
return;
}
this.consumptionData = {};
var error = 0;
await this.sendConsumptionRequest(this.Dates).then((result) => {
if (result.start && result.end && result.interval_reading) {
log("Données reçues de l'API :", result);
result.interval_reading.forEach((reading) => {
const year = dayjs(reading.date).get("year");
const day = dayjs(reading.date).get("date");
const month = dayjs(reading.date).get("month") + 1;
const value = parseFloat(reading.value);
if (!this.consumptionData[year]) this.consumptionData[year] = [];
if (this.config.annee_n_minus_1 === 1) {
var current = dayjs().set("hour", 0).set("minute", 0).set("second", 0);
const currentYear = current.year();
var testDate = current.subtract(1, "day");
switch (this.config.periode) {
case 1:
testDate = testDate.subtract(1, "day");
break;
case 2:
testDate = testDate.subtract(3, "day");
break;
case 3:
testDate = testDate.subtract(7, "day");
break;
default:
testDate = current;
break;
}
if (currentYear !== year) {
testDate = testDate.subtract(1, "year");
current = current.subtract(1, "day").subtract(1, "year");
}
if (dayjs(reading.date).isBetween(testDate, current)) {
this.consumptionData[year].push({ day, month, value });
}
} else {
this.consumptionData[year].push({ day, month, value });
}
});
} else {
error = 1;
console.error("[LINKY] Format inattendu des données :", result);
if (result.error) {
this.error = result.error.error;
this.sendSocketNotification("ERROR", this.error);
} else {
this.error = "Erreur lors de la collecte de données.";
this.sendSocketNotification("ERROR", this.error);
}
}
});
if (!error) {
log("Données de consommation collecté:", this.consumptionData);
this.error = null;
this.clearRetryTimer();
this.setChartValue();
} else {
log("Il y a des Erreurs API...");
this.retryTimer();
}
},
// création des données chartjs
setChartValue () {
const days = [];
const datasets = [];
const colors = this.getChartColors();
let index = 0;
for (const year in this.consumptionData) {
const data = this.consumptionData[year].sort((a, b) => {
if (a.month === b.month) {
return a.day - b.day;
}
return a.month - b.month;
});
const values = data.map((item) => item.value);
if (index === 0) {
days.push(
...data.map(
(item) => `${item.day} ${["Error", "janv.", "févr.", "mars", "avr.", "mai", "juin", "juil.", "août", "sept.", "oct.", "nov.", "déc."][item.month]}`
)
);
}
datasets.push({
label: year,
data: values,
backgroundColor: colors[index],
borderColor: colors[index].replace("0.8", "1"),
borderWidth: 1
});
index++;
}
log("Données des graphiques :", { labels: days, data: datasets });
this.chartData = {
labels: days,
datasets: datasets,
energie: this.config.annee_n_minus_1 === 1 ? this.setEnergie() : null,
update: `Données du ${dayjs().format("DD/MM/YYYY -- HH:mm:ss")}`,
seed: dayjs().valueOf()
};
this.sendSocketNotification("DATA", this.chartData);
this.saveChartData();
},
// Selection schémas de couleurs
getChartColors () {
const colorSchemes = {
1: ["rgba(245, 234, 39, 0.8)", "rgba(245, 39, 230, 0.8)"],
2: ["rgba(252, 255, 0, 0.8)", "rgba(13, 255, 0, 0.8)"],
3: ["rgba(255, 255, 255, 0.8)", "rgba(0, 255, 242, 0.8)"],
4: ["rgba(255, 125, 0, 0.8)", "rgba(220, 0, 255, 0.8)"]
};
return colorSchemes[this.config.couleur] || colorSchemes[1];
},
// Demande des datas selon l'API
sendConsumptionRequest (date) {
return new Promise((resolve) => {
this.Linky.getDailyConsumption(date.startDate, date.endDate).then((result) => {
resolve(result);
});
});
},
// Importation de la librairie linky (dynamic impor)
async loadLinky () {
const loaded = await import("linky");
return loaded;
},
// cacul des dates périodique
calculateDates () {
const endDate = dayjs().format("YYYY-MM-DD");
var start = dayjs();
switch (this.config.periode) {
case 1:
start = start.subtract(1, "day");
break;
case 2:
start = start.subtract(3, "day");
break;
case 3:
start = start.subtract(7, "day");
break;
default:
console.error("[LINKY] periode invalide.");
this.sendSocketNotification("ERROR", "periode invalide.");
return null;
}
if (this.config.annee_n_minus_1 === 1) start = start.subtract(1, "year");
const startDate = dayjs(start).format("YYYY-MM-DD");
return { startDate, endDate };
},
// Création du message Energie
setEnergie () {
const currentYearTotal = this.calculateTotalConsumption(dayjs().get("year"));
const previousYearTotal = this.calculateTotalConsumption(dayjs().subtract(1, "year").get("year"));
var message, color, periodText;
switch (this.config.periode) {
case 1:
periodText = "le dernier jour";
break;
case 2:
periodText = "les 3 derniers jours";
break;
case 3:
periodText = "les 7 derniers jours";
break;
default:
periodText = "période inconnue";
}
if (currentYearTotal < previousYearTotal) {
message = `Félicitations, votre consommation d'énergie a baissé sur ${periodText} par rapport à l'année dernière !`;
color = "green";
} else if (currentYearTotal > previousYearTotal) {
message = `Attention, votre consommation d'énergie a augmenté sur ${periodText} par rapport à l'année dernière !`;
color = "red";
} else {
message = `Votre consommation d'énergie est stable sur ${periodText} par rapport à l'année dernière.`;
color = "yellow";
}
return {
message: message,
color: color
};
},
// Calcul de la comsommation totale
calculateTotalConsumption (year) {
let total = 0;
if (this.consumptionData[year]) {
this.consumptionData[year].forEach((data) => {
total += data.value;
});
}
return total;
},
// Retry Timer en cas d'erreur, relance la requete 2 heures apres
retryTimer () {
if (this.timer) this.clearRetryTimer();
this.timer = setTimeout(() => {
log("Retry-Timer: Démarrage");
this.getConsumptionData();
}, 1000 * 60 * 60 * 2);
let job = dayjs(dayjs() + this.timer._idleNext.expiry);
log("Retry-Timer planifié:", job.format("[Le] DD/MM/YYYY -- HH:mm:ss"));
this.sendTimer(job.valueOf(), job.format("[Le] DD/MM/YYYY -- HH:mm:ss"), "RETRY");
},
// Retry Timer kill
clearRetryTimer () {
if (this.timer) log("Retry-Timer: Arrêt");
clearTimeout(this.timer);
this.timer = null;
this.sendTimer(null, null, "RETRY");
},
// Affiche la prochaine tache Cron
displayNextCron () {
const next = CronExpressionParser.parse(this.cronExpression, { tz: "Europe/Paris" });
let nextCron = dayjs(next.next().toString());
log("Prochaine tâche planifiée:", nextCron.format("[Le] DD/MM/YYYY -- HH:mm:ss"));
this.sendTimer(nextCron.valueOf(), nextCron.format("[Le] DD/MM/YYYY -- HH:mm:ss"), "CRON");
},
// Exporte les donnée Charts vers linkyData.json
saveChartData () {
const jsonData = JSON.stringify(this.chartData, null, 2);
writeFile(this.dataFile, jsonData, "utf8", (err) => {
if (err) {
console.error("Erreur lors de l'exportation des données", err);
} else {
log("Les données ont été exporté vers", this.dataFile);
}
});
},
// Lecture du fichier linkyData.json
readChartData () {
return new Promise((resolve) => {
// verifie la presence
access(this.dataFile, constants.F_OK, (error) => {
if (error) {
log("Pas de fichier cache trouvé");
this.chartData = {};
resolve();
return;
}
// lit le fichier
readFile(this.dataFile, (err, data) => {
if (err) {
console.error("[LINKY] Erreur de la lecture du fichier cache!", err);
this.chartData = {};
resolve();
return;
}
const linkyData = JSON.parse(data);
const now = dayjs().valueOf();
const seed = dayjs(linkyData.seed).format("DD/MM/YYYY -- HH:mm:ss");
const next = dayjs(linkyData.seed).add(12, "hour").valueOf();
if (now > next) {
log("Les dernieres données reçues sont > 12h, utilisation de l'API...");
this.chartData = {};
} else {
log("Utilisation du cache:", seed);
this.chartData = linkyData;
}
resolve();
});
});
});
},
// Envoi l'affichage de la date du prochain update
sendTimer (seed, date, type) {
let timer = {
seed: seed,
date: date,
type: type
};
this.timers[type] = timer;
this.sendSocketNotification("TIMERS", timer);
},
// envoi l'affichage de tous les timers (server mode)
sendTimers () {
const timers = Object.values(this.timers);
timers.forEach((timer) => {
this.sendSocketNotification("TIMERS", timer);
});
},
catchError () {
process.on("unhandledRejection", (error) => {
// catch conso API error and Enedis only
if (error.stack.includes("MMM-Linky/node_modules/linky/") && error.response) {
// catch Enedis error
if (error.response.status && error.response.message && error.response.error) {
console.error(`[LINKY] [${error.response.status}] ${error.response.message}`);
this.error = error.response.message;
this.sendSocketNotification("ERROR", this.error);
} else {
// catch Conso API error
if (error.message) {
console.error(`[LINKY] [${error.code}] ${error.message}`);
this.error = `[${error.code}] ${error.message}`;
this.sendSocketNotification("ERROR", this.error);
} else {
// must never Happen...
console.error("[LINKY]", error);
}
}
this.retryTimer();
} else {
// detect any errors of node_helper of MMM-Linky
if (error.stack.includes("MMM-Linky/node_helper.js")) {
console.error(`[LINKY] ${this._citation()}`);
console.error("[LINKY] Merci de signaler cette erreur aux développeurs");
console.error("[LINKY] ---------");
console.error("[LINKY] node_helper Error:", error);
console.error("[LINKY] ---------");
} else {
// from other modules (must never happen... but...)
console.error("-Other-", error);
}
}
});
},
_citation () {
let citations = [
"J'ai glissé, chef !",
"Mirabelle appelle Églantine...",
"Mais tremblez pas comme ça, ça fait de la mousse !!!",
"C'est dur d'être chef, Chef ?",
"Un lapin, chef !",
"Fou afez trop chaud ou fou afez trop froid ? ",
"Restez groupire!",
"On fait pas faire des mouvements respiratoires à un type qu'a les bras cassés !!!",
"Si j’connaissais l’con qui a fait sauter l’pont...",
"Le fil rouge sur le bouton rouge, le fil bleu sur le bouton bleu."
];
const random = Math.floor(Math.random() * citations.length);
return citations[random];
}
});