39
39
import org .openhab .binding .awattar .internal .AwattarConsecutiveBestPriceResult ;
40
40
import org .openhab .binding .awattar .internal .AwattarNonConsecutiveBestPriceResult ;
41
41
import org .openhab .binding .awattar .internal .AwattarPrice ;
42
- import org .openhab .core . i18n . TimeZoneProvider ;
42
+ import org .openhab .binding . awattar . internal . dto . AwattarTimeProvider ;
43
43
import org .openhab .core .library .types .DateTimeType ;
44
44
import org .openhab .core .library .types .OnOffType ;
45
45
import org .openhab .core .library .types .QuantityType ;
@@ -70,14 +70,13 @@ public class AwattarBestPriceHandler extends BaseThingHandler {
70
70
private static final int THING_REFRESH_INTERVAL = 60 ;
71
71
72
72
private final Logger logger = LoggerFactory .getLogger (AwattarBestPriceHandler .class );
73
+ private final AwattarTimeProvider timeProvider ;
73
74
74
75
private @ Nullable ScheduledFuture <?> thingRefresher ;
75
76
76
- private final TimeZoneProvider timeZoneProvider ;
77
-
78
- public AwattarBestPriceHandler (Thing thing , TimeZoneProvider timeZoneProvider ) {
77
+ public AwattarBestPriceHandler (Thing thing , AwattarTimeProvider timeProvider ) {
79
78
super (thing );
80
- this .timeZoneProvider = timeZoneProvider ;
79
+ this .timeProvider = timeProvider ;
81
80
}
82
81
83
82
@ Override
@@ -97,7 +96,7 @@ public void initialize() {
97
96
* here
98
97
*/
99
98
thingRefresher = scheduler .scheduleAtFixedRate (this ::refreshChannels ,
100
- getMillisToNextMinute (1 , timeZoneProvider ), THING_REFRESH_INTERVAL * 1000L ,
99
+ getMillisToNextMinute (1 , timeProvider . getZoneId () ), THING_REFRESH_INTERVAL * 1000L ,
101
100
TimeUnit .MILLISECONDS );
102
101
}
103
102
}
@@ -141,7 +140,7 @@ public void refreshChannel(ChannelUID channelUID) {
141
140
return ;
142
141
}
143
142
144
- ZoneId zoneId = bridgeHandler . getTimeZone ();
143
+ ZoneId zoneId = timeProvider . getZoneId ();
145
144
146
145
AwattarBestPriceConfiguration config = getConfigAs (AwattarBestPriceConfiguration .class );
147
146
TimeRange timerange = getRange (config .rangeStart , config .rangeDuration , zoneId );
@@ -163,7 +162,7 @@ public void refreshChannel(ChannelUID channelUID) {
163
162
long diff ;
164
163
switch (channelId ) {
165
164
case CHANNEL_ACTIVE :
166
- state = OnOffType .from (result .isActive ());
165
+ state = OnOffType .from (result .isActive (timeProvider . getInstantNow () ));
167
166
break ;
168
167
case CHANNEL_START :
169
168
state = new DateTimeType (Instant .ofEpochMilli (result .getStart ()));
@@ -172,16 +171,16 @@ public void refreshChannel(ChannelUID channelUID) {
172
171
state = new DateTimeType (Instant .ofEpochMilli (result .getEnd ()));
173
172
break ;
174
173
case CHANNEL_COUNTDOWN :
175
- diff = result .getStart () - Instant . now ().toEpochMilli ();
174
+ diff = result .getStart () - timeProvider . getInstantNow ().toEpochMilli ();
176
175
if (diff >= 0 ) {
177
176
state = getDuration (diff );
178
177
} else {
179
178
state = QuantityType .valueOf (0 , Units .MINUTE );
180
179
}
181
180
break ;
182
181
case CHANNEL_REMAINING :
183
- if (result .isActive ()) {
184
- diff = result .getEnd () - Instant . now ().toEpochMilli ();
182
+ if (result .isActive (timeProvider . getInstantNow () )) {
183
+ diff = result .getEnd () - timeProvider . getInstantNow ().toEpochMilli ();
185
184
state = getDuration (diff );
186
185
} else {
187
186
state = QuantityType .valueOf (0 , Units .MINUTE );
@@ -216,20 +215,39 @@ private List<AwattarPrice> getPriceRange(AwattarBridgeHandler bridgeHandler, Tim
216
215
return result ;
217
216
}
218
217
218
+ /**
219
+ * Returns the time range for the given start hour and duration.
220
+ *
221
+ * @param start the start hour (0-23)
222
+ * @param duration the duration in hours
223
+ * @param zoneId the time zone to use
224
+ * @return the range
225
+ */
219
226
protected TimeRange getRange (int start , int duration , ZoneId zoneId ) {
220
- ZonedDateTime startCal = getCalendarForHour (start , zoneId );
221
- ZonedDateTime endCal = startCal .plusHours (duration );
222
- ZonedDateTime now = ZonedDateTime . now ( zoneId );
227
+ ZonedDateTime startTime = getStartTime (start , zoneId );
228
+ ZonedDateTime endTime = startTime .plusHours (duration );
229
+ ZonedDateTime now = timeProvider . getZonedDateTimeNow ( );
223
230
if (now .getHour () < start ) {
224
231
// we are before the range, so we might be still within the last range
225
- startCal = startCal .minusDays (1 );
226
- endCal = endCal .minusDays (1 );
232
+ startTime = startTime .minusDays (1 );
233
+ endTime = endTime .minusDays (1 );
227
234
}
228
- if (endCal . toInstant (). toEpochMilli () < Instant . now (). toEpochMilli ( )) {
235
+ if (endTime . isBefore ( now )) {
229
236
// span is in the past, add one day
230
- startCal = startCal .plusDays (1 );
231
- endCal = endCal .plusDays (1 );
237
+ startTime = startTime .plusDays (1 );
238
+ endTime = endTime .plusDays (1 );
232
239
}
233
- return new TimeRange (startCal .toInstant ().toEpochMilli (), endCal .toInstant ().toEpochMilli ());
240
+ return new TimeRange (startTime .toInstant ().toEpochMilli (), endTime .toInstant ().toEpochMilli ());
241
+ }
242
+
243
+ /**
244
+ * Returns the start time for the given hour.
245
+ *
246
+ * @param start the hour. Must be between 0 and 23.
247
+ * @param zoneId the time zone
248
+ * @return the start time
249
+ */
250
+ protected ZonedDateTime getStartTime (int start , ZoneId zoneId ) {
251
+ return getCalendarForHour (start , zoneId );
234
252
}
235
253
}
0 commit comments