@@ -84,6 +84,12 @@ public class LinkyHandler extends BaseThingHandler {
84
84
private @ NonNullByDefault ({}) String prmId ;
85
85
private @ NonNullByDefault ({}) String userId ;
86
86
87
+ private enum Target {
88
+ FIRST ,
89
+ LAST ,
90
+ ALL
91
+ }
92
+
87
93
public LinkyHandler (Thing thing , LocaleProvider localeProvider , Gson gson , HttpClient httpClient ) {
88
94
super (thing );
89
95
this .gson = gson ;
@@ -94,9 +100,9 @@ public LinkyHandler(Thing thing, LocaleProvider localeProvider, Gson gson, HttpC
94
100
LocalDate today = LocalDate .now ();
95
101
Consumption consumption = getConsumptionData (today .minusDays (15 ), today );
96
102
if (consumption != null ) {
97
- logData (consumption .aggregats .days , "Day" , false , DateTimeFormatter .ISO_LOCAL_DATE , false );
98
- logData (consumption .aggregats .weeks , "Week" , true , DateTimeFormatter .ISO_LOCAL_DATE_TIME , false );
99
- consumption = getConsumptionAfterChecks (consumption );
103
+ logData (consumption .aggregats .days , "Day" , false , DateTimeFormatter .ISO_LOCAL_DATE , Target . ALL );
104
+ logData (consumption .aggregats .weeks , "Week" , true , DateTimeFormatter .ISO_LOCAL_DATE_TIME , Target . ALL );
105
+ consumption = getConsumptionAfterChecks (consumption , Target . LAST );
100
106
}
101
107
return consumption ;
102
108
});
@@ -106,12 +112,9 @@ public LinkyHandler(Thing thing, LocaleProvider localeProvider, Gson gson, HttpC
106
112
LocalDate from = to .minusDays (2 );
107
113
Consumption consumption = getPowerData (from , to );
108
114
if (consumption != null ) {
109
- try {
110
- checkData (consumption );
111
- } catch (LinkyException e ) {
112
- logger .debug ("Power data: {}" , e .getMessage ());
113
- return null ;
114
- }
115
+ logData (consumption .aggregats .days , "Day (peak)" , true , DateTimeFormatter .ISO_LOCAL_DATE_TIME ,
116
+ Target .ALL );
117
+ consumption = getConsumptionAfterChecks (consumption , Target .FIRST );
115
118
}
116
119
return consumption ;
117
120
});
@@ -120,8 +123,8 @@ public LinkyHandler(Thing thing, LocaleProvider localeProvider, Gson gson, HttpC
120
123
LocalDate today = LocalDate .now ();
121
124
Consumption consumption = getConsumptionData (today .withDayOfMonth (1 ).minusMonths (1 ), today );
122
125
if (consumption != null ) {
123
- logData (consumption .aggregats .months , "Month" , true , DateTimeFormatter .ISO_LOCAL_DATE_TIME , false );
124
- consumption = getConsumptionAfterChecks (consumption );
126
+ logData (consumption .aggregats .months , "Month" , true , DateTimeFormatter .ISO_LOCAL_DATE_TIME , Target . ALL );
127
+ consumption = getConsumptionAfterChecks (consumption , Target . LAST );
125
128
}
126
129
return consumption ;
127
130
});
@@ -130,8 +133,8 @@ public LinkyHandler(Thing thing, LocaleProvider localeProvider, Gson gson, HttpC
130
133
LocalDate today = LocalDate .now ();
131
134
Consumption consumption = getConsumptionData (LocalDate .of (today .getYear () - 1 , 1 , 1 ), today );
132
135
if (consumption != null ) {
133
- logData (consumption .aggregats .years , "Year" , true , DateTimeFormatter .ISO_LOCAL_DATE_TIME , false );
134
- consumption = getConsumptionAfterChecks (consumption );
136
+ logData (consumption .aggregats .years , "Year" , true , DateTimeFormatter .ISO_LOCAL_DATE_TIME , Target . ALL );
137
+ consumption = getConsumptionAfterChecks (consumption , Target . LAST );
135
138
}
136
139
return consumption ;
137
140
});
@@ -191,8 +194,7 @@ public void initialize() {
191
194
private synchronized void updateData () {
192
195
boolean connectedBefore = isConnected ();
193
196
updatePowerData ();
194
- updateDailyData ();
195
- updateWeeklyData ();
197
+ updateDailyWeeklyData ();
196
198
updateMonthlyData ();
197
199
updateYearlyData ();
198
200
if (!connectedBefore && isConnected ()) {
@@ -202,35 +204,25 @@ private synchronized void updateData() {
202
204
203
205
private synchronized void updatePowerData () {
204
206
if (isLinked (PEAK_POWER ) || isLinked (PEAK_TIMESTAMP )) {
205
- cachedPowerData .getValue ().ifPresent (values -> {
207
+ cachedPowerData .getValue ().ifPresentOrElse (values -> {
206
208
Aggregate days = values .aggregats .days ;
207
209
updateVAChannel (PEAK_POWER , days .datas .get (0 ));
208
210
updateState (PEAK_TIMESTAMP , new DateTimeType (days .periodes .get (0 ).dateDebut ));
211
+ }, () -> {
212
+ updateKwhChannel (PEAK_POWER , Double .NaN );
213
+ updateState (PEAK_TIMESTAMP , UnDefType .UNDEF );
209
214
});
210
215
}
211
216
}
212
217
213
218
/**
214
219
* Request new dayly/weekly data and updates channels
215
220
*/
216
- private synchronized void updateDailyData () {
217
- if (isLinked (YESTERDAY )) {
221
+ private synchronized void updateDailyWeeklyData () {
222
+ if (isLinked (YESTERDAY ) || isLinked ( LAST_WEEK ) || isLinked ( THIS_WEEK ) ) {
218
223
cachedDailyData .getValue ().ifPresentOrElse (values -> {
219
224
Aggregate days = values .aggregats .days ;
220
225
updateKwhChannel (YESTERDAY , days .datas .get (days .datas .size () - 1 ));
221
- }, () -> {
222
- updateKwhChannel (YESTERDAY , Double .NaN );
223
- });
224
- }
225
- }
226
-
227
- /**
228
- * Request new weekly data and updates channels
229
- */
230
- private synchronized void updateWeeklyData () {
231
- if (isLinked (LAST_WEEK ) || isLinked (THIS_WEEK )) {
232
- cachedDailyData .getValue ().ifPresentOrElse (values -> {
233
- Aggregate days = values .aggregats .days ;
234
226
int idxLast = days .periodes .get (days .periodes .size () - 1 ).dateDebut .get (weekFields .dayOfWeek ()) == 7 ? 2
235
227
: 1 ;
236
228
Aggregate weeks = values .aggregats .weeks ;
@@ -243,6 +235,7 @@ private synchronized void updateWeeklyData() {
243
235
updateKwhChannel (THIS_WEEK , 0.0 );
244
236
}
245
237
}, () -> {
238
+ updateKwhChannel (YESTERDAY , Double .NaN );
246
239
if (ZonedDateTime .now ().get (weekFields .dayOfWeek ()) == 1 ) {
247
240
updateKwhChannel (THIS_WEEK , 0.0 );
248
241
updateKwhChannel (LAST_WEEK , Double .NaN );
@@ -432,11 +425,9 @@ public synchronized void handleCommand(ChannelUID channelUID, Command command) {
432
425
boolean connectedBefore = isConnected ();
433
426
switch (channelUID .getId ()) {
434
427
case YESTERDAY :
435
- updateDailyData ();
436
- break ;
437
428
case LAST_WEEK :
438
429
case THIS_WEEK :
439
- updateWeeklyData ();
430
+ updateDailyWeeklyData ();
440
431
break ;
441
432
case LAST_MONTH :
442
433
case THIS_MONTH :
@@ -461,14 +452,18 @@ public synchronized void handleCommand(ChannelUID channelUID, Command command) {
461
452
}
462
453
}
463
454
464
- private @ Nullable Consumption getConsumptionAfterChecks (Consumption consumption ) {
455
+ private @ Nullable Consumption getConsumptionAfterChecks (Consumption consumption , Target target ) {
465
456
try {
466
457
checkData (consumption );
467
458
} catch (LinkyException e ) {
468
459
logger .debug ("Consumption data: {}" , e .getMessage ());
469
460
return null ;
470
461
}
471
- if (!isDataLastDayAvailable (consumption )) {
462
+ if (target == Target .FIRST && !isDataFirstDayAvailable (consumption )) {
463
+ logger .debug ("Data including yesterday are not yet available" );
464
+ return null ;
465
+ }
466
+ if (target == Target .LAST && !isDataLastDayAvailable (consumption )) {
472
467
logger .debug ("Data including yesterday are not yet available" );
473
468
return null ;
474
469
}
@@ -502,19 +497,29 @@ public void checkData(Consumption consumption) throws LinkyException {
502
497
}
503
498
}
504
499
500
+ private boolean isDataFirstDayAvailable (Consumption consumption ) {
501
+ Aggregate days = consumption .aggregats .days ;
502
+ logData (days , "First day" , false , DateTimeFormatter .ISO_LOCAL_DATE , Target .FIRST );
503
+ return days .datas != null && days .datas .size () > 0 && !days .datas .get (0 ).isNaN ();
504
+ }
505
+
505
506
private boolean isDataLastDayAvailable (Consumption consumption ) {
506
507
Aggregate days = consumption .aggregats .days ;
507
- logData (days , "Last day" , false , DateTimeFormatter .ISO_LOCAL_DATE , true );
508
+ logData (days , "Last day" , false , DateTimeFormatter .ISO_LOCAL_DATE , Target . LAST );
508
509
return days .datas != null && days .datas .size () > 0 && !days .datas .get (days .datas .size () - 1 ).isNaN ();
509
510
}
510
511
511
512
private void logData (Aggregate aggregate , String title , boolean withDateFin , DateTimeFormatter dateTimeFormatter ,
512
- boolean onlyLast ) {
513
+ Target target ) {
513
514
if (logger .isDebugEnabled ()) {
514
515
int size = (aggregate .datas == null || aggregate .periodes == null ) ? 0
515
516
: (aggregate .datas .size () <= aggregate .periodes .size () ? aggregate .datas .size ()
516
517
: aggregate .periodes .size ());
517
- if (onlyLast ) {
518
+ if (target == Target .FIRST ) {
519
+ if (size > 0 ) {
520
+ logData (aggregate , 0 , title , withDateFin , dateTimeFormatter );
521
+ }
522
+ } else if (target == Target .LAST ) {
518
523
if (size > 0 ) {
519
524
logData (aggregate , size - 1 , title , withDateFin , dateTimeFormatter );
520
525
}
0 commit comments