@@ -19,7 +19,7 @@ module.exports = NodeHelper.create({
19
19
this . consumptionData = { } ;
20
20
this . cronExpression = "0 0 14 * * *" ;
21
21
this . error = null ;
22
- this . dataFile = path . resolve ( __dirname , "linkyData.json " ) ;
22
+ this . dataPath = path . resolve ( __dirname , "data " ) ;
23
23
this . timers = { } ;
24
24
} ,
25
25
@@ -29,7 +29,7 @@ module.exports = NodeHelper.create({
29
29
if ( ! this . ready ) {
30
30
this . config = payload ;
31
31
this . ready = true ;
32
- this . chartData = { } ;
32
+ this . consumptionData = { } ;
33
33
this . initialize ( ) ;
34
34
} else {
35
35
this . initWithCache ( ) ;
@@ -45,9 +45,9 @@ module.exports = NodeHelper.create({
45
45
if ( this . config . debug ) log = ( ...args ) => { console . log ( "[LINKY]" , ...args ) ; } ;
46
46
if ( this . config . dev ) log ( "Config:" , this . config ) ;
47
47
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 ) ;
51
51
}
52
52
else {
53
53
this . getConsumptionData ( ) ;
@@ -59,7 +59,7 @@ module.exports = NodeHelper.create({
59
59
initWithCache ( ) {
60
60
console . log ( `[LINKY] [Cache] MMM-Linky Version: ${ require ( "./package.json" ) . version } Revison: ${ require ( "./package.json" ) . rev } ` ) ;
61
61
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 ) ;
63
63
if ( Object . keys ( this . timers ) . length ) this . sendTimers ( ) ;
64
64
} ,
65
65
@@ -173,15 +173,15 @@ module.exports = NodeHelper.create({
173
173
}
174
174
175
175
log ( "Données des graphiques :" , { labels : days , data : datasets } ) ;
176
- this . chartData = {
176
+ this . consumptionData = {
177
177
labels : days ,
178
178
datasets : datasets ,
179
179
energie : this . config . annee_n_minus_1 === 1 ? this . setEnergie ( ) : null ,
180
180
update : `Données du ${ dayjs ( ) . format ( "DD/MM/YYYY -- HH:mm:ss" ) } ` ,
181
181
seed : dayjs ( ) . valueOf ( )
182
182
} ;
183
- this . sendSocketNotification ( "DATA" , this . chartData ) ;
184
- this . saveChartData ( ) ;
183
+ this . sendSocketNotification ( "DATA" , this . consumptionData ) ;
184
+ this . saveChartData ( "consumption" ) ;
185
185
} ,
186
186
187
187
// Selection schémas de couleurs
@@ -271,55 +271,6 @@ module.exports = NodeHelper.create({
271
271
return total ;
272
272
} ,
273
273
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
-
323
274
// -----------
324
275
// API -------
325
276
// -----------
@@ -352,7 +303,6 @@ module.exports = NodeHelper.create({
352
303
resolve ( await this . sendRequest ( type , date ) ) ;
353
304
} ) ;
354
305
} else {
355
- // !!! todo catch()
356
306
this . Linky [ type ] ( date . startDate , date . endDate )
357
307
. then ( ( result ) => {
358
308
resolve ( result ) ;
@@ -487,5 +437,84 @@ module.exports = NodeHelper.create({
487
437
timers . forEach ( ( timer ) => {
488
438
this . sendSocketNotification ( "TIMERS" , timer ) ;
489
439
} ) ;
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
+ } ) ;
490
519
}
491
520
} ) ;
0 commit comments