[awattar] change getNow to return an instant

Signed-off-by: Thomas Leber <thomas@tl-photography.at>
This commit is contained in:
Thomas Leber 2024-12-07 11:38:28 +01:00
parent a5a7e7c3c9
commit 2cc529b143
2 changed files with 13 additions and 11 deletions

View File

@ -23,6 +23,7 @@ import static org.openhab.binding.awattar.internal.AwattarUtil.getCalendarForHou
import static org.openhab.binding.awattar.internal.AwattarUtil.getDuration;
import static org.openhab.binding.awattar.internal.AwattarUtil.getMillisToNextMinute;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.ArrayList;
@ -162,7 +163,7 @@ public class AwattarBestPriceHandler extends BaseThingHandler {
long diff;
switch (channelId) {
case CHANNEL_ACTIVE:
state = OnOffType.from(result.isActive(getNow(zoneId).toInstant()));
state = OnOffType.from(result.isActive(getNow()));
break;
case CHANNEL_START:
state = new DateTimeType(Instant.ofEpochMilli(result.getStart()));
@ -171,7 +172,7 @@ public class AwattarBestPriceHandler extends BaseThingHandler {
state = new DateTimeType(Instant.ofEpochMilli(result.getEnd()));
break;
case CHANNEL_COUNTDOWN:
diff = result.getStart() - getNow(zoneId).toInstant().toEpochMilli();
diff = result.getStart() - getNow().toEpochMilli();
if (diff >= 0) {
state = getDuration(diff);
} else {
@ -179,8 +180,8 @@ public class AwattarBestPriceHandler extends BaseThingHandler {
}
break;
case CHANNEL_REMAINING:
if (result.isActive(getNow(zoneId).toInstant())) {
diff = result.getEnd() - getNow(zoneId).toInstant().toEpochMilli();
if (result.isActive(getNow())) {
diff = result.getEnd() - getNow().toEpochMilli();
state = getDuration(diff);
} else {
state = QuantityType.valueOf(0, Units.MINUTE);
@ -226,7 +227,7 @@ public class AwattarBestPriceHandler extends BaseThingHandler {
protected TimeRange getRange(int start, int duration, ZoneId zoneId) {
ZonedDateTime startTime = getStartTime(start, zoneId);
ZonedDateTime endTime = startTime.plusHours(duration);
ZonedDateTime now = getNow(zoneId);
ZonedDateTime now = getNow().atZone(zoneId);
if (now.getHour() < start) {
// we are before the range, so we might be still within the last range
startTime = startTime.minusDays(1);
@ -257,7 +258,7 @@ public class AwattarBestPriceHandler extends BaseThingHandler {
* @param zoneId the time zone
* @return the current time
*/
protected ZonedDateTime getNow(ZoneId zoneId) {
return ZonedDateTime.now(zoneId);
protected Instant getNow() {
return Instant.now();
}
}

View File

@ -30,6 +30,7 @@ import static org.openhab.binding.awattar.internal.AwattarBindingConstants.CHANN
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Comparator;
@ -206,8 +207,8 @@ public class AwattarBridgeHandlerTest extends JavaTest {
return ZonedDateTime.of(2024, 6, 15, 12, 0, 0, 0, zoneId);
}
protected ZonedDateTime getNow(ZoneId zoneId) {
return ZonedDateTime.of(2024, 6, 15, 12, 0, 0, 0, zoneId);
protected Instant getNow() {
return ZonedDateTime.of(2024, 6, 15, 12, 0, 0, 0, ZoneId.of("GMT+2")).toInstant();
}
};
@ -262,8 +263,8 @@ public class AwattarBridgeHandlerTest extends JavaTest {
return ZonedDateTime.of(2024, 6, 15, 0, 0, 0, 0, zoneId);
}
protected ZonedDateTime getNow(ZoneId zoneId) {
return ZonedDateTime.of(2024, 6, 15, currentHour, currentMinute, 0, 0, zoneId);
protected Instant getNow() {
return ZonedDateTime.of(2024, 6, 15, currentHour, currentMinute, 0, 0, ZoneId.of("GMT+2")).toInstant();
}
};