1
- const NodeHelper = require ( "node_helper" ) ;
1
+ const { writeFile, readFile, access, constants } = require ( "node:fs" ) ;
2
+ const path = require ( "node:path" ) ;
2
3
const cron = require ( "node-cron" ) ;
3
4
const { CronExpressionParser } = require ( "cron-parser" ) ;
5
+ const NodeHelper = require ( "node_helper" ) ;
4
6
5
7
var log = ( ) => { /* do nothing */ } ;
6
8
7
9
module . exports = NodeHelper . create ( {
8
-
9
10
start ( ) {
10
11
this . Linky = null ;
11
12
this . config = null ;
@@ -14,6 +15,7 @@ module.exports = NodeHelper.create({
14
15
this . consumptionData = { } ;
15
16
this . cronExpression = "0 14 * * *" ;
16
17
this . error = null ;
18
+ this . dataFile = path . resolve ( __dirname , "linkyData.json" ) ;
17
19
} ,
18
20
19
21
socketNotificationReceived ( notification , payload ) {
@@ -59,18 +61,36 @@ module.exports = NodeHelper.create({
59
61
}
60
62
}
61
63
this . retryTimer ( ) ;
64
+ } else {
65
+ console . error ( "[LINKY] ---------" ) ;
66
+ console . error ( "[LINKY] Please report this error to developer" ) ;
67
+ console . error ( "[LINKY] Core Error:" , error ) ;
68
+ console . error ( "[LINKY] ---------" ) ;
62
69
}
63
70
} ) ;
71
+
72
+ await this . readChartData ( ) ;
73
+ if ( Object . keys ( this . chartData ) . length ) {
74
+ this . sendSocketNotification ( "DATA" , this . chartData ) ;
75
+ }
76
+ else {
77
+ this . getConsumptionData ( ) ;
78
+ }
79
+ this . scheduleDataFetch ( ) ;
80
+ } ,
81
+
82
+ async initLinky ( callback ) {
64
83
const { Session } = await this . loadLinky ( ) ;
65
84
try {
66
85
this . Linky = new Session ( this . config . token , this . config . prm ) ;
86
+ log ( "API linky Prête" ) ;
87
+ if ( callback ) callback ( ) ;
67
88
} catch ( error ) {
68
89
console . error ( `[LINKY] ${ error } ` ) ;
69
90
this . error = error . message ;
70
91
this . sendSocketNotification ( "ERROR" , this . error ) ;
71
- return ;
92
+
72
93
}
73
- this . scheduleDataFetch ( ) ;
74
94
} ,
75
95
76
96
initWithCache ( ) {
@@ -88,13 +108,15 @@ module.exports = NodeHelper.create({
88
108
this . getConsumptionData ( ) ;
89
109
this . displayNextCron ( ) ;
90
110
} ) ;
91
-
92
- this . getConsumptionData ( ) ;
93
111
this . displayNextCron ( ) ;
94
112
} ,
95
113
96
114
// Récupération des données
97
115
async getConsumptionData ( ) {
116
+ if ( ! this . Linky ) {
117
+ this . initLinky ( ( ) => this . getConsumptionData ( ) ) ;
118
+ return ;
119
+ }
98
120
this . consumptionData = { } ;
99
121
var error = 0 ;
100
122
await Promise . all ( this . Dates . map (
@@ -183,9 +205,11 @@ module.exports = NodeHelper.create({
183
205
labels : days ,
184
206
datasets : datasets ,
185
207
energie : this . config . annee_n_minus_1 === 1 ? this . setEnergie ( ) : null ,
186
- update : `Données du ${ new Date ( Date . now ( ) ) . toLocaleString ( "fr" ) } `
208
+ update : `Données du ${ new Date ( Date . now ( ) ) . toLocaleString ( "fr" ) } ` ,
209
+ seed : Date . now ( )
187
210
} ;
188
211
this . sendSocketNotification ( "DATA" , this . chartData ) ;
212
+ this . saveChartData ( ) ;
189
213
} ,
190
214
191
215
// Selection schémas de couleurs
@@ -323,5 +347,49 @@ module.exports = NodeHelper.create({
323
347
displayNextCron ( ) {
324
348
const next = CronExpressionParser . parse ( this . cronExpression , { tz : "Europe/Paris" } ) ;
325
349
log ( "Prochaine tâche planifiée pour le" , new Date ( next . next ( ) . toString ( ) ) . toLocaleString ( "fr" ) ) ;
350
+ } ,
351
+
352
+ saveChartData ( ) {
353
+ const jsonData = JSON . stringify ( this . chartData , null , 2 ) ;
354
+ writeFile ( this . dataFile , jsonData , "utf8" , ( err ) => {
355
+ if ( err ) {
356
+ console . error ( "Erreur lors de l'exportation des données" , err ) ;
357
+ } else {
358
+ log ( "Les données ont été exporté vers" , this . dataFile ) ;
359
+ }
360
+ } ) ;
361
+ } ,
362
+
363
+ readChartData ( ) {
364
+ return new Promise ( ( resolve ) => {
365
+ access ( this . dataFile , constants . F_OK , ( error ) => {
366
+ if ( error ) {
367
+ log ( "Pas de fichier cache trouvé" ) ;
368
+ this . chartData = { } ;
369
+ resolve ( ) ;
370
+ return ;
371
+ }
372
+
373
+ readFile ( this . dataFile , ( err , data ) => {
374
+ if ( err ) {
375
+ console . error ( "[LINKY] Erreur de la lecture du fichier cache!" , err ) ;
376
+ this . chartData = { } ;
377
+ resolve ( ) ;
378
+ return ;
379
+ }
380
+ const linkyData = JSON . parse ( data ) ;
381
+ const now = Date . now ( ) ;
382
+ const next = linkyData . seed + ( 1000 * 60 * 60 * 24 ) ;
383
+ if ( now > next ) {
384
+ log ( "Les dernieres données reçues sont > 24h, utilisation de l'API..." ) ;
385
+ this . chartData = { } ;
386
+ } else {
387
+ log ( "Utilisation du cache..." ) ;
388
+ this . chartData = linkyData ;
389
+ }
390
+ resolve ( ) ;
391
+ } ) ;
392
+ } ) ;
393
+ } ) ;
326
394
}
327
395
} ) ;
0 commit comments