From c17b5f9ea8c892725890382ed435f8a9e2f281ac Mon Sep 17 00:00:00 2001 From: Jacob Laursen Date: Sat, 9 Nov 2024 23:17:08 +0100 Subject: [PATCH] Simplify DateTimeType handling for JuiceNet Signed-off-by: Jacob Laursen --- .../internal/JuiceNetHandlerFactory.java | 8 ++------ .../handler/JuiceNetDeviceHandler.java | 20 ++++++------------- 2 files changed, 8 insertions(+), 20 deletions(-) diff --git a/bundles/org.openhab.binding.juicenet/src/main/java/org/openhab/binding/juicenet/internal/JuiceNetHandlerFactory.java b/bundles/org.openhab.binding.juicenet/src/main/java/org/openhab/binding/juicenet/internal/JuiceNetHandlerFactory.java index f02729b0b83..d90b28dbf1a 100644 --- a/bundles/org.openhab.binding.juicenet/src/main/java/org/openhab/binding/juicenet/internal/JuiceNetHandlerFactory.java +++ b/bundles/org.openhab.binding.juicenet/src/main/java/org/openhab/binding/juicenet/internal/JuiceNetHandlerFactory.java @@ -20,7 +20,6 @@ import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.openhab.binding.juicenet.internal.handler.JuiceNetBridgeHandler; import org.openhab.binding.juicenet.internal.handler.JuiceNetDeviceHandler; -import org.openhab.core.i18n.TimeZoneProvider; import org.openhab.core.io.net.http.HttpClientFactory; import org.openhab.core.thing.Bridge; import org.openhab.core.thing.Thing; @@ -44,13 +43,10 @@ public class JuiceNetHandlerFactory extends BaseThingHandlerFactory { private static final Set SUPPORTED_THING_TYPES_UIDS = Set.of(BRIDGE_THING_TYPE, DEVICE_THING_TYPE); private final HttpClientFactory httpClientFactory; - private final TimeZoneProvider timeZoneProvider; @Activate - public JuiceNetHandlerFactory(@Reference HttpClientFactory httpClientFactory, - @Reference TimeZoneProvider timeZoneProvider) { + public JuiceNetHandlerFactory(@Reference HttpClientFactory httpClientFactory) { this.httpClientFactory = httpClientFactory; - this.timeZoneProvider = timeZoneProvider; } @Override @@ -65,7 +61,7 @@ public class JuiceNetHandlerFactory extends BaseThingHandlerFactory { if (thingTypeUID.equals(BRIDGE_THING_TYPE)) { return new JuiceNetBridgeHandler((Bridge) thing, httpClientFactory.getCommonHttpClient()); } else if (thingTypeUID.equals(DEVICE_THING_TYPE)) { - return new JuiceNetDeviceHandler(thing, timeZoneProvider); + return new JuiceNetDeviceHandler(thing); } return null; diff --git a/bundles/org.openhab.binding.juicenet/src/main/java/org/openhab/binding/juicenet/internal/handler/JuiceNetDeviceHandler.java b/bundles/org.openhab.binding.juicenet/src/main/java/org/openhab/binding/juicenet/internal/handler/JuiceNetDeviceHandler.java index bc8c67a6e2e..f803747e443 100644 --- a/bundles/org.openhab.binding.juicenet/src/main/java/org/openhab/binding/juicenet/internal/handler/JuiceNetDeviceHandler.java +++ b/bundles/org.openhab.binding.juicenet/src/main/java/org/openhab/binding/juicenet/internal/handler/JuiceNetDeviceHandler.java @@ -15,6 +15,7 @@ package org.openhab.binding.juicenet.internal.handler; import static org.openhab.binding.juicenet.internal.JuiceNetBindingConstants.*; import java.time.Instant; +import java.time.ZoneId; import java.time.ZonedDateTime; import java.time.temporal.ChronoField; import java.util.Objects; @@ -29,7 +30,6 @@ import org.openhab.binding.juicenet.internal.api.dto.JuiceNetApiDeviceStatus; import org.openhab.binding.juicenet.internal.api.dto.JuiceNetApiInfo; import org.openhab.binding.juicenet.internal.api.dto.JuiceNetApiTouSchedule; import org.openhab.core.config.core.Configuration; -import org.openhab.core.i18n.TimeZoneProvider; import org.openhab.core.library.types.DateTimeType; import org.openhab.core.library.types.DecimalType; import org.openhab.core.library.types.OnOffType; @@ -61,8 +61,6 @@ public class JuiceNetDeviceHandler extends BaseThingHandler { private final Logger logger = LoggerFactory.getLogger(JuiceNetDeviceHandler.class); - private final TimeZoneProvider timeZoneProvider; - // properties private String name = ""; @@ -75,10 +73,8 @@ public class JuiceNetDeviceHandler extends BaseThingHandler { JuiceNetApiTouSchedule deviceTouSchedule = new JuiceNetApiTouSchedule(); JuiceNetApiCar deviceCar = new JuiceNetApiCar(); - public JuiceNetDeviceHandler(Thing thing, TimeZoneProvider timeZoneProvider) { + public JuiceNetDeviceHandler(Thing thing) { super(thing); - - this.timeZoneProvider = timeZoneProvider; } public void setNameAndToken(String name, String token) { @@ -219,7 +215,7 @@ public class JuiceNetDeviceHandler extends BaseThingHandler { return; } - ZonedDateTime datetime = ((DateTimeType) command).getZonedDateTime(); + ZonedDateTime datetime = ((DateTimeType) command).getZonedDateTime(ZoneId.systemDefault()); Long targetTime = datetime.toEpochSecond() + datetime.get(ChronoField.OFFSET_SECONDS); logger.debug("DateTime: {} - {}", datetime.toString(), targetTime); @@ -314,10 +310,6 @@ public class JuiceNetDeviceHandler extends BaseThingHandler { } } - private ZonedDateTime toZonedDateTime(long localEpochSeconds) { - return Instant.ofEpochSecond(localEpochSeconds).atZone(timeZoneProvider.getTimeZone()); - } - private void refreshStatusChannels() { updateState(CHANNEL_STATE, new StringType(deviceStatus.state)); @@ -332,9 +324,9 @@ public class JuiceNetDeviceHandler extends BaseThingHandler { updateState(CHANNEL_MESSAGE, new StringType(deviceStatus.message)); updateState(CHANNEL_OVERRIDE, OnOffType.from(deviceStatus.showOverride)); updateState(CHANNEL_CHARGING_TIME_LEFT, new QuantityType<>(deviceStatus.chargingTimeLeft, Units.SECOND)); - updateState(CHANNEL_PLUG_UNPLUG_TIME, new DateTimeType(toZonedDateTime(deviceStatus.plugUnplugTime))); - updateState(CHANNEL_TARGET_TIME, new DateTimeType(toZonedDateTime(deviceStatus.targetTime))); - updateState(CHANNEL_UNIT_TIME, new DateTimeType(toZonedDateTime(deviceStatus.utcTime))); + updateState(CHANNEL_PLUG_UNPLUG_TIME, new DateTimeType(Instant.ofEpochSecond(deviceStatus.plugUnplugTime))); + updateState(CHANNEL_TARGET_TIME, new DateTimeType(Instant.ofEpochSecond(deviceStatus.targetTime))); + updateState(CHANNEL_UNIT_TIME, new DateTimeType(Instant.ofEpochSecond(deviceStatus.utcTime))); updateState(CHANNEL_TEMPERATURE, new QuantityType<>(deviceStatus.temperature, SIUnits.CELSIUS)); updateState(CHANNEL_CURRENT_LIMIT, new QuantityType<>(deviceStatus.charging.ampsLimit, Units.AMPERE)); updateState(CHANNEL_CURRENT, new QuantityType<>(deviceStatus.charging.ampsCurrent, Units.AMPERE));