Skip to content

Commit 5bde4ad

Browse files
committed
move datafile to data folder
1 parent ee4f0e0 commit 5bde4ad

File tree

3 files changed

+89
-60
lines changed

3 files changed

+89
-60
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
/node_modules/
22
/MMM-Linky.js
33
/node_helper.js
4-
/linkyData.json
4+
/data/consumption.json

data/.keep

Whitespace-only changes.

src/node_helper.js

Lines changed: 88 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module.exports = NodeHelper.create({
1919
this.consumptionData = {};
2020
this.cronExpression = "0 0 14 * * *";
2121
this.error = null;
22-
this.dataFile = path.resolve(__dirname, "linkyData.json");
22+
this.dataPath = path.resolve(__dirname, "data");
2323
this.timers = {};
2424
},
2525

@@ -29,7 +29,7 @@ module.exports = NodeHelper.create({
2929
if (!this.ready) {
3030
this.config = payload;
3131
this.ready = true;
32-
this.chartData = {};
32+
this.consumptionData = {};
3333
this.initialize();
3434
} else {
3535
this.initWithCache();
@@ -45,9 +45,9 @@ module.exports = NodeHelper.create({
4545
if (this.config.debug) log = (...args) => { console.log("[LINKY]", ...args); };
4646
if (this.config.dev) log("Config:", this.config);
4747

48-
await this.readChartData();
49-
if (Object.keys(this.chartData).length) {
50-
this.sendSocketNotification("DATA", this.chartData);
48+
await this.readChartData("consumption");
49+
if (Object.keys(this.consumptionData).length) {
50+
this.sendSocketNotification("DATA", this.consumptionData);
5151
}
5252
else {
5353
this.getConsumptionData();
@@ -59,7 +59,7 @@ module.exports = NodeHelper.create({
5959
initWithCache () {
6060
console.log(`[LINKY] [Cache] MMM-Linky Version: ${require("./package.json").version} Revison: ${require("./package.json").rev}`);
6161
if (this.error) this.sendSocketNotification("ERROR", this.error);
62-
if (Object.keys(this.chartData).length) this.sendSocketNotification("DATA", this.chartData);
62+
if (Object.keys(this.consumptionData).length) this.sendSocketNotification("DATA", this.consumptionData);
6363
if (Object.keys(this.timers).length) this.sendTimers();
6464
},
6565

@@ -173,15 +173,15 @@ module.exports = NodeHelper.create({
173173
}
174174

175175
log("Données des graphiques :", { labels: days, data: datasets });
176-
this.chartData = {
176+
this.consumptionData = {
177177
labels: days,
178178
datasets: datasets,
179179
energie: this.config.annee_n_minus_1 === 1 ? this.setEnergie() : null,
180180
update: `Données du ${dayjs().format("DD/MM/YYYY -- HH:mm:ss")}`,
181181
seed: dayjs().valueOf()
182182
};
183-
this.sendSocketNotification("DATA", this.chartData);
184-
this.saveChartData();
183+
this.sendSocketNotification("DATA", this.consumptionData);
184+
this.saveChartData("consumption");
185185
},
186186

187187
// Selection schémas de couleurs
@@ -271,55 +271,6 @@ module.exports = NodeHelper.create({
271271
return total;
272272
},
273273

274-
// Exporte les donnée Charts vers linkyData.json
275-
saveChartData () {
276-
const jsonData = JSON.stringify(this.chartData, null, 2);
277-
writeFile(this.dataFile, jsonData, "utf8", (err) => {
278-
if (err) {
279-
console.error("Erreur lors de l'exportation des données", err);
280-
} else {
281-
log("Les données ont été exporté vers", this.dataFile);
282-
}
283-
});
284-
},
285-
286-
// Lecture du fichier linkyData.json
287-
readChartData () {
288-
return new Promise((resolve) => {
289-
// verifie la presence
290-
access(this.dataFile, constants.F_OK, (error) => {
291-
if (error) {
292-
log("Pas de fichier cache trouvé");
293-
this.chartData = {};
294-
resolve();
295-
return;
296-
}
297-
298-
// lit le fichier
299-
readFile(this.dataFile, (err, data) => {
300-
if (err) {
301-
console.error("[LINKY] Erreur de la lecture du fichier cache!", err);
302-
this.chartData = {};
303-
resolve();
304-
return;
305-
}
306-
const linkyData = JSON.parse(data);
307-
const now = dayjs().valueOf();
308-
const seed = dayjs(linkyData.seed).format("DD/MM/YYYY -- HH:mm:ss");
309-
const next = dayjs(linkyData.seed).add(12, "hour").valueOf();
310-
if (now > next) {
311-
log("Les dernieres données reçues sont > 12h, utilisation de l'API...");
312-
this.chartData = {};
313-
} else {
314-
log("Utilisation du cache:", seed);
315-
this.chartData = linkyData;
316-
}
317-
resolve();
318-
});
319-
});
320-
});
321-
},
322-
323274
// -----------
324275
// API -------
325276
// -----------
@@ -352,7 +303,6 @@ module.exports = NodeHelper.create({
352303
resolve(await this.sendRequest(type, date));
353304
});
354305
} else {
355-
// !!! todo catch()
356306
this.Linky[type](date.startDate, date.endDate)
357307
.then((result) => {
358308
resolve(result);
@@ -487,5 +437,84 @@ module.exports = NodeHelper.create({
487437
timers.forEach((timer) => {
488438
this.sendSocketNotification("TIMERS", timer);
489439
});
440+
},
441+
442+
// -----------
443+
// CACHE FILES
444+
// -----------
445+
446+
// Exporte les donnée Charts
447+
saveChartData (type) {
448+
var file, data;
449+
switch (type) {
450+
case "consumption":
451+
file = `${this.dataPath}/consumption.json`;
452+
data = this.consumptionData;
453+
break;
454+
}
455+
456+
const jsonData = JSON.stringify(data, null, 2);
457+
writeFile(file, jsonData, "utf8", (err) => {
458+
if (err) {
459+
console.error(`[${type}] Erreur lors de l'exportation des données`, err);
460+
} else {
461+
log(`[${type}] Les données ont été exporté vers`, file);
462+
}
463+
});
464+
},
465+
466+
// Lecture des fichiers de données Charts
467+
readChartData (type) {
468+
var file;
469+
switch (type) {
470+
case "consumption":
471+
file = `${this.dataPath}/consumption.json`;
472+
break;
473+
}
474+
return new Promise((resolve) => {
475+
// verifie la presence
476+
access(file, constants.F_OK, (error) => {
477+
if (error) {
478+
log(`[${type}] Pas de fichier cache trouvé`);
479+
switch (type) {
480+
case "consumption":
481+
this.consumptionData = {};
482+
break;
483+
}
484+
resolve();
485+
return;
486+
}
487+
488+
// lit le fichier
489+
readFile(file, (err, data) => {
490+
if (err) {
491+
console.error(`[LINKY] [${type}] Erreur de la lecture du fichier cache!`, err);
492+
this.consumptionData = {};
493+
resolve();
494+
return;
495+
}
496+
const linkyData = JSON.parse(data);
497+
const now = dayjs().valueOf();
498+
const seed = dayjs(linkyData.seed).format("DD/MM/YYYY -- HH:mm:ss");
499+
const next = dayjs(linkyData.seed).add(12, "hour").valueOf();
500+
if (now > next) {
501+
log(`[${type}] Les dernieres données reçues sont > 12h, utilisation de l'API...`);
502+
switch (type) {
503+
case "consumption":
504+
this.consumptionData = {};
505+
break;
506+
}
507+
} else {
508+
log(`[${type}] Utilisation du cache:`, seed);
509+
switch (type) {
510+
case "consumption":
511+
this.consumptionData = linkyData;
512+
break;
513+
}
514+
}
515+
resolve();
516+
});
517+
});
518+
});
490519
}
491520
});

0 commit comments

Comments
 (0)