32
32
import org .eclipse .jdt .annotation .NonNullByDefault ;
33
33
import org .eclipse .jdt .annotation .Nullable ;
34
34
import org .eclipse .jetty .client .HttpClient ;
35
- import org .openhab .binding .senechome .internal .json .SenecHomeResponse ;
35
+ import org .openhab .binding .senechome .internal .dto .SenecHomeResponse ;
36
36
import org .openhab .core .cache .ExpiringCache ;
37
37
import org .openhab .core .library .types .DecimalType ;
38
38
import org .openhab .core .library .types .OnOffType ;
@@ -67,8 +67,6 @@ public class SenecHomeHandler extends BaseThingHandler {
67
67
private static final BigDecimal DIVISOR_MILLI_TO_KILO = BigDecimal .valueOf (1000000 );
68
68
// divisor to transform from milli to "iso" UNIT (e.g. mV => V)
69
69
private static final BigDecimal DIVISOR_MILLI_TO_ISO = BigDecimal .valueOf (1000 );
70
- // divisor to transform from "iso" to kilo UNIT (e.g. W => kW)
71
- private static final BigDecimal DIVISOR_ISO_TO_KILO = BigDecimal .valueOf (1000 );
72
70
// ix (x=1,3,8) types => hex encoded integer value
73
71
private static final String VALUE_TYPE_INT1 = "i1" ;
74
72
public static final String VALUE_TYPE_INT3 = "i3" ;
@@ -82,7 +80,7 @@ public class SenecHomeHandler extends BaseThingHandler {
82
80
83
81
private @ Nullable ScheduledFuture <?> refreshJob ;
84
82
private @ Nullable PowerLimitationStatusDTO limitationStatus = null ;
85
- private final @ Nullable SenecHomeApi senecHomeApi ;
83
+ private final SenecHomeApi senecHomeApi ;
86
84
private SenecHomeConfigurationDTO config = new SenecHomeConfigurationDTO ();
87
85
private final ExpiringCache <Boolean > refreshCache = new ExpiringCache <>(Duration .ofSeconds (5 ), this ::refreshState );
88
86
@@ -133,6 +131,18 @@ private void refresh() {
133
131
refreshCache .getValue ();
134
132
}
135
133
134
+ private <Q extends Quantity <Q >> void updateQtyStateIfAvailable (String channel , String @ Nullable [] valueArray ,
135
+ int arrayIndex , int scale , Unit <Q > unit ) {
136
+ updateQtyStateIfAvailable (channel , valueArray , arrayIndex , scale , unit , null );
137
+ }
138
+
139
+ private <Q extends Quantity <Q >> void updateQtyStateIfAvailable (String channel , String @ Nullable [] valueArray ,
140
+ int arrayIndex , int scale , Unit <Q > unit , @ Nullable BigDecimal divisor ) {
141
+ if (valueArray != null && valueArray .length > arrayIndex ) {
142
+ updateQtyState (channel , valueArray [arrayIndex ], scale , unit , divisor );
143
+ }
144
+ }
145
+
136
146
public @ Nullable Boolean refreshState () {
137
147
SenecHomeResponse response = null ;
138
148
try {
@@ -197,65 +207,68 @@ private void refresh() {
197
207
updateQtyState (CHANNEL_SENEC_GRID_VOLTAGE_PH3 , response .grid .currentGridVoltagePerPhase [2 ], 2 , Units .VOLT );
198
208
updateQtyState (CHANNEL_SENEC_GRID_FREQUENCY , response .grid .currentGridFrequency , 2 , Units .HERTZ );
199
209
200
- if (response .battery .chargedEnergy != null ) {
201
- updateQtyState (CHANNEL_SENEC_CHARGED_ENERGY_PACK1 , response .battery .chargedEnergy [0 ], 2 ,
202
- Units .KILOWATT_HOUR , DIVISOR_MILLI_TO_KILO );
203
- updateQtyState (CHANNEL_SENEC_CHARGED_ENERGY_PACK2 , response .battery .chargedEnergy [1 ], 2 ,
204
- Units .KILOWATT_HOUR , DIVISOR_MILLI_TO_KILO );
205
- updateQtyState (CHANNEL_SENEC_CHARGED_ENERGY_PACK3 , response .battery .chargedEnergy [2 ], 2 ,
206
- Units .KILOWATT_HOUR , DIVISOR_MILLI_TO_KILO );
207
- updateQtyState (CHANNEL_SENEC_CHARGED_ENERGY_PACK4 , response .battery .chargedEnergy [3 ], 2 ,
208
- Units .KILOWATT_HOUR , DIVISOR_MILLI_TO_KILO );
209
- }
210
- if (response .battery .dischargedEnergy != null ) {
211
- updateQtyState (CHANNEL_SENEC_DISCHARGED_ENERGY_PACK1 , response .battery .dischargedEnergy [0 ], 2 ,
212
- Units .KILOWATT_HOUR , DIVISOR_MILLI_TO_KILO );
213
- updateQtyState (CHANNEL_SENEC_DISCHARGED_ENERGY_PACK2 , response .battery .dischargedEnergy [1 ], 2 ,
214
- Units .KILOWATT_HOUR , DIVISOR_MILLI_TO_KILO );
215
- updateQtyState (CHANNEL_SENEC_DISCHARGED_ENERGY_PACK3 , response .battery .dischargedEnergy [2 ], 2 ,
216
- Units .KILOWATT_HOUR , DIVISOR_MILLI_TO_KILO );
217
- updateQtyState (CHANNEL_SENEC_DISCHARGED_ENERGY_PACK4 , response .battery .dischargedEnergy [3 ], 2 ,
218
- Units .KILOWATT_HOUR , DIVISOR_MILLI_TO_KILO );
219
- }
210
+ updateQtyStateIfAvailable (CHANNEL_SENEC_CHARGED_ENERGY_PACK1 , response .battery .chargedEnergy , 0 , 2 ,
211
+ Units .KILOWATT_HOUR , DIVISOR_MILLI_TO_KILO );
212
+ updateQtyStateIfAvailable (CHANNEL_SENEC_CHARGED_ENERGY_PACK2 , response .battery .chargedEnergy , 1 , 2 ,
213
+ Units .KILOWATT_HOUR , DIVISOR_MILLI_TO_KILO );
214
+ updateQtyStateIfAvailable (CHANNEL_SENEC_CHARGED_ENERGY_PACK3 , response .battery .chargedEnergy , 2 , 2 ,
215
+ Units .KILOWATT_HOUR , DIVISOR_MILLI_TO_KILO );
216
+ updateQtyStateIfAvailable (CHANNEL_SENEC_CHARGED_ENERGY_PACK4 , response .battery .chargedEnergy , 3 , 2 ,
217
+ Units .KILOWATT_HOUR , DIVISOR_MILLI_TO_KILO );
218
+
219
+ updateQtyStateIfAvailable (CHANNEL_SENEC_DISCHARGED_ENERGY_PACK1 , response .battery .dischargedEnergy , 0 , 2 ,
220
+ Units .KILOWATT_HOUR , DIVISOR_MILLI_TO_KILO );
221
+ updateQtyStateIfAvailable (CHANNEL_SENEC_DISCHARGED_ENERGY_PACK2 , response .battery .dischargedEnergy , 1 , 2 ,
222
+ Units .KILOWATT_HOUR , DIVISOR_MILLI_TO_KILO );
223
+ updateQtyStateIfAvailable (CHANNEL_SENEC_DISCHARGED_ENERGY_PACK3 , response .battery .dischargedEnergy , 2 , 2 ,
224
+ Units .KILOWATT_HOUR , DIVISOR_MILLI_TO_KILO );
225
+ updateQtyStateIfAvailable (CHANNEL_SENEC_DISCHARGED_ENERGY_PACK4 , response .battery .dischargedEnergy , 3 , 2 ,
226
+ Units .KILOWATT_HOUR , DIVISOR_MILLI_TO_KILO );
227
+
220
228
if (response .battery .cycles != null ) {
221
- updateDecimalState (CHANNEL_SENEC_CYCLES_PACK1 , response .battery .cycles [0 ]);
222
- updateDecimalState (CHANNEL_SENEC_CYCLES_PACK2 , response .battery .cycles [1 ]);
223
- updateDecimalState (CHANNEL_SENEC_CYCLES_PACK3 , response .battery .cycles [2 ]);
224
- updateDecimalState (CHANNEL_SENEC_CYCLES_PACK4 , response .battery .cycles [3 ]);
225
- }
226
- if (response .battery .current != null ) {
227
- updateQtyState (CHANNEL_SENEC_CURRENT_PACK1 , response .battery .current [0 ], 2 , Units .AMPERE );
228
- updateQtyState (CHANNEL_SENEC_CURRENT_PACK2 , response .battery .current [1 ], 2 , Units .AMPERE );
229
- updateQtyState (CHANNEL_SENEC_CURRENT_PACK3 , response .battery .current [2 ], 2 , Units .AMPERE );
230
- updateQtyState (CHANNEL_SENEC_CURRENT_PACK4 , response .battery .current [3 ], 2 , Units .AMPERE );
231
- }
232
- if (response .battery .voltage != null ) {
233
- updateQtyState (CHANNEL_SENEC_VOLTAGE_PACK1 , response .battery .voltage [0 ], 2 , Units .VOLT );
234
- updateQtyState (CHANNEL_SENEC_VOLTAGE_PACK2 , response .battery .voltage [1 ], 2 , Units .VOLT );
235
- updateQtyState (CHANNEL_SENEC_VOLTAGE_PACK3 , response .battery .voltage [2 ], 2 , Units .VOLT );
236
- updateQtyState (CHANNEL_SENEC_VOLTAGE_PACK4 , response .battery .voltage [3 ], 2 , Units .VOLT );
237
- }
238
- if (response .battery .maxCellVoltage != null ) {
239
- updateQtyState (CHANNEL_SENEC_MAX_CELL_VOLTAGE_PACK1 , response .battery .maxCellVoltage [0 ], 3 , Units .VOLT ,
240
- DIVISOR_MILLI_TO_ISO );
241
- updateQtyState (CHANNEL_SENEC_MAX_CELL_VOLTAGE_PACK2 , response .battery .maxCellVoltage [1 ], 3 , Units .VOLT ,
242
- DIVISOR_MILLI_TO_ISO );
243
- updateQtyState (CHANNEL_SENEC_MAX_CELL_VOLTAGE_PACK3 , response .battery .maxCellVoltage [2 ], 3 , Units .VOLT ,
244
- DIVISOR_MILLI_TO_ISO );
245
- updateQtyState (CHANNEL_SENEC_MAX_CELL_VOLTAGE_PACK4 , response .battery .maxCellVoltage [3 ], 3 , Units .VOLT ,
246
- DIVISOR_MILLI_TO_ISO );
247
- }
248
- if (response .battery .minCellVoltage != null ) {
249
- updateQtyState (CHANNEL_SENEC_MIN_CELL_VOLTAGE_PACK1 , response .battery .minCellVoltage [0 ], 3 , Units .VOLT ,
250
- DIVISOR_MILLI_TO_ISO );
251
- updateQtyState (CHANNEL_SENEC_MIN_CELL_VOLTAGE_PACK2 , response .battery .minCellVoltage [1 ], 3 , Units .VOLT ,
252
- DIVISOR_MILLI_TO_ISO );
253
- updateQtyState (CHANNEL_SENEC_MIN_CELL_VOLTAGE_PACK3 , response .battery .minCellVoltage [2 ], 3 , Units .VOLT ,
254
- DIVISOR_MILLI_TO_ISO );
255
- updateQtyState (CHANNEL_SENEC_MIN_CELL_VOLTAGE_PACK4 , response .battery .minCellVoltage [3 ], 3 , Units .VOLT ,
256
- DIVISOR_MILLI_TO_ISO );
229
+ int length = response .battery .cycles .length ;
230
+ if (length > 0 ) {
231
+ updateDecimalState (CHANNEL_SENEC_CYCLES_PACK1 , response .battery .cycles [0 ]);
232
+ }
233
+ if (length > 1 ) {
234
+ updateDecimalState (CHANNEL_SENEC_CYCLES_PACK2 , response .battery .cycles [1 ]);
235
+ }
236
+ if (length > 2 ) {
237
+ updateDecimalState (CHANNEL_SENEC_CYCLES_PACK3 , response .battery .cycles [2 ]);
238
+ }
239
+ if (length > 3 ) {
240
+ updateDecimalState (CHANNEL_SENEC_CYCLES_PACK4 , response .battery .cycles [3 ]);
241
+ }
257
242
}
258
243
244
+ updateQtyStateIfAvailable (CHANNEL_SENEC_CURRENT_PACK1 , response .battery .current , 0 , 2 , Units .AMPERE );
245
+ updateQtyStateIfAvailable (CHANNEL_SENEC_CURRENT_PACK2 , response .battery .current , 1 , 2 , Units .AMPERE );
246
+ updateQtyStateIfAvailable (CHANNEL_SENEC_CURRENT_PACK3 , response .battery .current , 2 , 2 , Units .AMPERE );
247
+ updateQtyStateIfAvailable (CHANNEL_SENEC_CURRENT_PACK4 , response .battery .current , 3 , 2 , Units .AMPERE );
248
+
249
+ updateQtyStateIfAvailable (CHANNEL_SENEC_VOLTAGE_PACK1 , response .battery .voltage , 0 , 2 , Units .VOLT );
250
+ updateQtyStateIfAvailable (CHANNEL_SENEC_VOLTAGE_PACK2 , response .battery .voltage , 1 , 2 , Units .VOLT );
251
+ updateQtyStateIfAvailable (CHANNEL_SENEC_VOLTAGE_PACK3 , response .battery .voltage , 2 , 2 , Units .VOLT );
252
+ updateQtyStateIfAvailable (CHANNEL_SENEC_VOLTAGE_PACK4 , response .battery .voltage , 3 , 2 , Units .VOLT );
253
+
254
+ updateQtyStateIfAvailable (CHANNEL_SENEC_MAX_CELL_VOLTAGE_PACK1 , response .battery .maxCellVoltage , 0 , 3 ,
255
+ Units .VOLT , DIVISOR_MILLI_TO_ISO );
256
+ updateQtyStateIfAvailable (CHANNEL_SENEC_MAX_CELL_VOLTAGE_PACK2 , response .battery .maxCellVoltage , 1 , 3 ,
257
+ Units .VOLT , DIVISOR_MILLI_TO_ISO );
258
+ updateQtyStateIfAvailable (CHANNEL_SENEC_MAX_CELL_VOLTAGE_PACK3 , response .battery .maxCellVoltage , 2 , 3 ,
259
+ Units .VOLT , DIVISOR_MILLI_TO_ISO );
260
+ updateQtyStateIfAvailable (CHANNEL_SENEC_MAX_CELL_VOLTAGE_PACK4 , response .battery .maxCellVoltage , 3 , 3 ,
261
+ Units .VOLT , DIVISOR_MILLI_TO_ISO );
262
+
263
+ updateQtyStateIfAvailable (CHANNEL_SENEC_MIN_CELL_VOLTAGE_PACK1 , response .battery .minCellVoltage , 0 , 3 ,
264
+ Units .VOLT , DIVISOR_MILLI_TO_ISO );
265
+ updateQtyStateIfAvailable (CHANNEL_SENEC_MIN_CELL_VOLTAGE_PACK2 , response .battery .minCellVoltage , 1 , 3 ,
266
+ Units .VOLT , DIVISOR_MILLI_TO_ISO );
267
+ updateQtyStateIfAvailable (CHANNEL_SENEC_MIN_CELL_VOLTAGE_PACK3 , response .battery .minCellVoltage , 2 , 3 ,
268
+ Units .VOLT , DIVISOR_MILLI_TO_ISO );
269
+ updateQtyStateIfAvailable (CHANNEL_SENEC_MIN_CELL_VOLTAGE_PACK4 , response .battery .minCellVoltage , 3 , 3 ,
270
+ Units .VOLT , DIVISOR_MILLI_TO_ISO );
271
+
259
272
if (response .temperature != null ) {
260
273
updateQtyState (CHANNEL_SENEC_BATTERY_TEMPERATURE , response .temperature .batteryTemperature , 0 ,
261
274
SIUnits .CELSIUS );
@@ -376,9 +389,10 @@ private static BigDecimal parseFloatValue(String value) {
376
389
}
377
390
378
391
protected void updatePowerLimitationStatus (Channel channel , boolean status , int duration ) {
379
- if (this .limitationStatus != null ) {
380
- if (this .limitationStatus .state == status ) {
381
- long stateSince = new Date ().getTime () - this .limitationStatus .time ;
392
+ PowerLimitationStatusDTO limitationStatus = this .limitationStatus ;
393
+ if (limitationStatus != null ) {
394
+ if (limitationStatus .state == status ) {
395
+ long stateSince = new Date ().getTime () - limitationStatus .time ;
382
396
383
397
if (((int ) (stateSince / 1000 )) < duration ) {
384
398
// skip updating state (possible flapping state)
@@ -387,15 +401,17 @@ protected void updatePowerLimitationStatus(Channel channel, boolean status, int
387
401
logger .debug ("{} longer than required duration {}" , status , duration );
388
402
}
389
403
} else {
390
- this .limitationStatus .state = status ;
391
- this .limitationStatus .time = new Date ().getTime ();
404
+ limitationStatus .state = status ;
405
+ limitationStatus .time = new Date ().getTime ();
406
+ this .limitationStatus = limitationStatus ;
392
407
393
408
// skip updating state (state changed, possible flapping state)
394
409
return ;
395
410
}
396
411
} else {
397
- this .limitationStatus = new PowerLimitationStatusDTO ();
398
- this .limitationStatus .state = status ;
412
+ limitationStatus = new PowerLimitationStatusDTO ();
413
+ limitationStatus .state = status ;
414
+ this .limitationStatus = limitationStatus ;
399
415
}
400
416
401
417
logger .debug ("Updating power limitation state {}" , status );
0 commit comments