30
30
import java .util .ArrayList ;
31
31
import java .util .Comparator ;
32
32
import java .util .List ;
33
- import java .util .SortedMap ;
33
+ import java .util .SortedSet ;
34
34
import java .util .concurrent .ScheduledFuture ;
35
35
import java .util .concurrent .TimeUnit ;
36
- import java .util .stream .Collectors ;
37
36
38
37
import org .eclipse .jdt .annotation .NonNullByDefault ;
39
38
import org .eclipse .jdt .annotation .Nullable ;
67
66
*/
68
67
@ NonNullByDefault
69
68
public class AwattarBestpriceHandler extends BaseThingHandler {
69
+ private static final int THING_REFRESH_INTERVAL = 60 ;
70
70
71
71
private final Logger logger = LoggerFactory .getLogger (AwattarBestpriceHandler .class );
72
72
73
- private final int thingRefreshInterval = 60 ;
74
- @ Nullable
75
- private ScheduledFuture <?> thingRefresher ;
73
+ private @ Nullable ScheduledFuture <?> thingRefresher ;
76
74
77
75
private final TimeZoneProvider timeZoneProvider ;
78
76
@@ -85,14 +83,8 @@ public AwattarBestpriceHandler(Thing thing, TimeZoneProvider timeZoneProvider) {
85
83
public void initialize () {
86
84
AwattarBestpriceConfiguration config = getConfigAs (AwattarBestpriceConfiguration .class );
87
85
88
- boolean configValid = true ;
89
-
90
86
if (config .length >= config .rangeDuration ) {
91
87
updateStatus (ThingStatus .OFFLINE , ThingStatusDetail .CONFIGURATION_ERROR , "@text/error.length.value" );
92
- configValid = false ;
93
- }
94
-
95
- if (!configValid ) {
96
88
return ;
97
89
}
98
90
@@ -104,7 +96,8 @@ public void initialize() {
104
96
* here
105
97
*/
106
98
thingRefresher = scheduler .scheduleAtFixedRate (this ::refreshChannels ,
107
- getMillisToNextMinute (1 , timeZoneProvider ), thingRefreshInterval * 1000 , TimeUnit .MILLISECONDS );
99
+ getMillisToNextMinute (1 , timeZoneProvider ), THING_REFRESH_INTERVAL * 1000 ,
100
+ TimeUnit .MILLISECONDS );
108
101
}
109
102
}
110
103
updateStatus (ThingStatus .UNKNOWN );
@@ -138,24 +131,24 @@ public void refreshChannel(ChannelUID channelUID) {
138
131
return ;
139
132
}
140
133
AwattarBridgeHandler bridgeHandler = (AwattarBridgeHandler ) bridge .getHandler ();
141
- if (bridgeHandler == null || bridgeHandler .getPriceMap () == null ) {
134
+ if (bridgeHandler == null || bridgeHandler .getPrices () == null ) {
142
135
logger .debug ("No prices available, so can't refresh channel." );
143
136
// no prices available, can't continue
144
137
updateState (channelUID , state );
145
138
return ;
146
139
}
147
140
AwattarBestpriceConfiguration config = getConfigAs (AwattarBestpriceConfiguration .class );
148
- Timerange timerange = getRange (config .rangeStart , config .rangeDuration , bridgeHandler .getTimeZone ());
149
- if (!(bridgeHandler .containsPriceFor (timerange .start ) && bridgeHandler .containsPriceFor (timerange .end ))) {
141
+ TimeRange timerange = getRange (config .rangeStart , config .rangeDuration , bridgeHandler .getTimeZone ());
142
+ if (!(bridgeHandler .containsPriceFor (timerange .start ()) && bridgeHandler .containsPriceFor (timerange .end () ))) {
150
143
updateState (channelUID , state );
151
144
return ;
152
145
}
153
146
154
147
AwattarBestPriceResult result ;
148
+ List <AwattarPrice > range = getPriceRange (bridgeHandler , timerange );
149
+
155
150
if (config .consecutive ) {
156
- ArrayList <AwattarPrice > range = new ArrayList <>(config .rangeDuration );
157
- range .addAll (getPriceRange (bridgeHandler , timerange ,
158
- (o1 , o2 ) -> Long .compare (o1 .getStartTimestamp (), o2 .getStartTimestamp ())));
151
+ range .sort (Comparator .comparing (AwattarPrice ::timerange ));
159
152
AwattarConsecutiveBestPriceResult res = new AwattarConsecutiveBestPriceResult (
160
153
range .subList (0 , config .length ), bridgeHandler .getTimeZone ());
161
154
@@ -168,9 +161,8 @@ public void refreshChannel(ChannelUID channelUID) {
168
161
}
169
162
result = res ;
170
163
} else {
171
- List <AwattarPrice > range = getPriceRange (bridgeHandler , timerange ,
172
- (o1 , o2 ) -> Double .compare (o1 .getPrice (), o2 .getPrice ()));
173
- AwattarNonConsecutiveBestPriceResult res = new AwattarNonConsecutiveBestPriceResult (config .length ,
164
+ range .sort (Comparator .naturalOrder ());
165
+ AwattarNonConsecutiveBestPriceResult res = new AwattarNonConsecutiveBestPriceResult (
174
166
bridgeHandler .getTimeZone ());
175
167
int ct = 0 ;
176
168
for (AwattarPrice price : range ) {
@@ -223,21 +215,18 @@ public void handleCommand(ChannelUID channelUID, Command command) {
223
215
}
224
216
}
225
217
226
- private List <AwattarPrice > getPriceRange (AwattarBridgeHandler bridgeHandler , Timerange range ,
227
- Comparator <AwattarPrice > comparator ) {
228
- ArrayList <AwattarPrice > result = new ArrayList <>();
229
- SortedMap <Long , AwattarPrice > priceMap = bridgeHandler .getPriceMap ();
230
- if (priceMap == null ) {
218
+ private List <AwattarPrice > getPriceRange (AwattarBridgeHandler bridgeHandler , TimeRange range ) {
219
+ List <AwattarPrice > result = new ArrayList <>();
220
+ SortedSet <AwattarPrice > prices = bridgeHandler .getPrices ();
221
+ if (prices == null ) {
231
222
logger .debug ("No prices available, can't compute ranges" );
232
223
return result ;
233
224
}
234
- result .addAll (priceMap .values ().stream ().filter (x -> x .isBetween (range .start , range .end ))
235
- .collect (Collectors .toSet ()));
236
- result .sort (comparator );
225
+ result .addAll (prices .stream ().filter (x -> range .contains (x .timerange ())).toList ());
237
226
return result ;
238
227
}
239
228
240
- private Timerange getRange (int start , int duration , ZoneId zoneId ) {
229
+ protected TimeRange getRange (int start , int duration , ZoneId zoneId ) {
241
230
ZonedDateTime startCal = getCalendarForHour (start , zoneId );
242
231
ZonedDateTime endCal = startCal .plusHours (duration );
243
232
ZonedDateTime now = ZonedDateTime .now (zoneId );
@@ -251,16 +240,6 @@ private Timerange getRange(int start, int duration, ZoneId zoneId) {
251
240
startCal = startCal .plusDays (1 );
252
241
endCal = endCal .plusDays (1 );
253
242
}
254
- return new Timerange (startCal .toInstant ().toEpochMilli (), endCal .toInstant ().toEpochMilli ());
255
- }
256
-
257
- private class Timerange {
258
- long start ;
259
- long end ;
260
-
261
- Timerange (long start , long end ) {
262
- this .start = start ;
263
- this .end = end ;
264
- }
243
+ return new TimeRange (startCal .toInstant ().toEpochMilli (), endCal .toInstant ().toEpochMilli ());
265
244
}
266
245
}
0 commit comments