From 82813cdcd4f26b5be265cf82c801bbe9410e4876 Mon Sep 17 00:00:00 2001 From: Jacob Laursen Date: Mon, 16 Dec 2024 23:13:34 +0100 Subject: [PATCH] Simplify DateTimeType handling for WeatherUnderground Signed-off-by: Jacob Laursen --- .../WeatherUndergroundHandlerFactory.java | 8 ++------ .../handler/WeatherUndergroundHandler.java | 15 +++------------ .../json/WeatherUndergroundJsonCurrent.java | 7 +++---- .../json/WeatherUndergroundJsonForecastDay.java | 7 +++---- .../json/WeatherUndergroundJsonUtils.java | 11 ++++------- 5 files changed, 15 insertions(+), 33 deletions(-) diff --git a/bundles/org.openhab.binding.weatherunderground/src/main/java/org/openhab/binding/weatherunderground/internal/WeatherUndergroundHandlerFactory.java b/bundles/org.openhab.binding.weatherunderground/src/main/java/org/openhab/binding/weatherunderground/internal/WeatherUndergroundHandlerFactory.java index fd0f097e8dd..c78fa58e4ed 100644 --- a/bundles/org.openhab.binding.weatherunderground/src/main/java/org/openhab/binding/weatherunderground/internal/WeatherUndergroundHandlerFactory.java +++ b/bundles/org.openhab.binding.weatherunderground/src/main/java/org/openhab/binding/weatherunderground/internal/WeatherUndergroundHandlerFactory.java @@ -29,7 +29,6 @@ import org.openhab.binding.weatherunderground.internal.handler.WeatherUndergroun import org.openhab.core.config.discovery.DiscoveryService; import org.openhab.core.i18n.LocaleProvider; import org.openhab.core.i18n.LocationProvider; -import org.openhab.core.i18n.TimeZoneProvider; import org.openhab.core.i18n.UnitProvider; import org.openhab.core.thing.Bridge; import org.openhab.core.thing.Thing; @@ -64,16 +63,13 @@ public class WeatherUndergroundHandlerFactory extends BaseThingHandlerFactory { private final LocaleProvider localeProvider; private final LocationProvider locationProvider; private final UnitProvider unitProvider; - private final TimeZoneProvider timeZoneProvider; @Activate public WeatherUndergroundHandlerFactory(final @Reference LocaleProvider localeProvider, - final @Reference LocationProvider locationProvider, final @Reference UnitProvider unitProvider, - final @Reference TimeZoneProvider timeZoneProvider) { + final @Reference LocationProvider locationProvider, final @Reference UnitProvider unitProvider) { this.localeProvider = localeProvider; this.locationProvider = locationProvider; this.unitProvider = unitProvider; - this.timeZoneProvider = timeZoneProvider; } @Override @@ -86,7 +82,7 @@ public class WeatherUndergroundHandlerFactory extends BaseThingHandlerFactory { ThingTypeUID thingTypeUID = thing.getThingTypeUID(); if (thingTypeUID.equals(THING_TYPE_WEATHER)) { - return new WeatherUndergroundHandler(thing, localeProvider, unitProvider, timeZoneProvider); + return new WeatherUndergroundHandler(thing, localeProvider, unitProvider); } if (thingTypeUID.equals(THING_TYPE_BRIDGE)) { diff --git a/bundles/org.openhab.binding.weatherunderground/src/main/java/org/openhab/binding/weatherunderground/internal/handler/WeatherUndergroundHandler.java b/bundles/org.openhab.binding.weatherunderground/src/main/java/org/openhab/binding/weatherunderground/internal/handler/WeatherUndergroundHandler.java index 645fb0d9998..8058adade82 100644 --- a/bundles/org.openhab.binding.weatherunderground/src/main/java/org/openhab/binding/weatherunderground/internal/handler/WeatherUndergroundHandler.java +++ b/bundles/org.openhab.binding.weatherunderground/src/main/java/org/openhab/binding/weatherunderground/internal/handler/WeatherUndergroundHandler.java @@ -16,7 +16,6 @@ import static org.openhab.core.library.unit.MetricPrefix.*; import java.io.IOException; import java.math.BigDecimal; -import java.time.ZoneId; import java.util.HashMap; import java.util.Locale; import java.util.Map; @@ -37,7 +36,6 @@ import org.openhab.binding.weatherunderground.internal.json.WeatherUndergroundJs import org.openhab.binding.weatherunderground.internal.json.WeatherUndergroundJsonForecast; import org.openhab.binding.weatherunderground.internal.json.WeatherUndergroundJsonForecastDay; import org.openhab.core.i18n.LocaleProvider; -import org.openhab.core.i18n.TimeZoneProvider; import org.openhab.core.i18n.UnitProvider; import org.openhab.core.io.net.http.HttpUtil; import org.openhab.core.library.types.DateTimeType; @@ -179,7 +177,6 @@ public class WeatherUndergroundHandler extends BaseThingHandler { private final LocaleProvider localeProvider; private final UnitProvider unitProvider; - private final TimeZoneProvider timeZoneProvider; private final Gson gson; private final Map forecastMap; @@ -189,12 +186,10 @@ public class WeatherUndergroundHandler extends BaseThingHandler { private @Nullable WeatherUndergroundBridgeHandler bridgeHandler; - public WeatherUndergroundHandler(Thing thing, LocaleProvider localeProvider, UnitProvider unitProvider, - TimeZoneProvider timeZoneProvider) { + public WeatherUndergroundHandler(Thing thing, LocaleProvider localeProvider, UnitProvider unitProvider) { super(thing); this.localeProvider = localeProvider; this.unitProvider = unitProvider; - this.timeZoneProvider = timeZoneProvider; gson = new Gson(); forecastMap = initForecastDayMap(); } @@ -358,9 +353,7 @@ public class WeatherUndergroundHandler extends BaseThingHandler { case "stationId": return undefOrState(current.getStationId(), new StringType(current.getStationId())); case "observationTime": - ZoneId zoneId = timeZoneProvider.getTimeZone(); - return undefOrState(current.getObservationTime(zoneId), - new DateTimeType(current.getObservationTime(zoneId))); + return undefOrState(current.getObservationTime(), new DateTimeType(current.getObservationTime())); case "conditions": return undefOrState(current.getConditions(), new StringType(current.getConditions())); case "temperature": @@ -433,9 +426,7 @@ public class WeatherUndergroundHandler extends BaseThingHandler { String channelTypeId = getChannelTypeId(channelId); switch (channelTypeId) { case "forecastTime": - ZoneId zoneId = timeZoneProvider.getTimeZone(); - return undefOrState(dayForecast.getForecastTime(zoneId), - new DateTimeType(dayForecast.getForecastTime(zoneId))); + return undefOrState(dayForecast.getForecastTime(), new DateTimeType(dayForecast.getForecastTime())); case "conditions": return undefOrState(dayForecast.getConditions(), new StringType(dayForecast.getConditions())); case "minTemperature": diff --git a/bundles/org.openhab.binding.weatherunderground/src/main/java/org/openhab/binding/weatherunderground/internal/json/WeatherUndergroundJsonCurrent.java b/bundles/org.openhab.binding.weatherunderground/src/main/java/org/openhab/binding/weatherunderground/internal/json/WeatherUndergroundJsonCurrent.java index 874c79d166b..bbd9be43bf6 100644 --- a/bundles/org.openhab.binding.weatherunderground/src/main/java/org/openhab/binding/weatherunderground/internal/json/WeatherUndergroundJsonCurrent.java +++ b/bundles/org.openhab.binding.weatherunderground/src/main/java/org/openhab/binding/weatherunderground/internal/json/WeatherUndergroundJsonCurrent.java @@ -14,8 +14,7 @@ package org.openhab.binding.weatherunderground.internal.json; import java.math.BigDecimal; import java.net.URL; -import java.time.ZoneId; -import java.time.ZonedDateTime; +import java.time.Instant; /** * The {@link WeatherUndergroundJsonCurrent} is the Java class used @@ -134,8 +133,8 @@ public class WeatherUndergroundJsonCurrent { * * @return the observation date and time or null if not defined */ - public ZonedDateTime getObservationTime(ZoneId zoneId) { - return WeatherUndergroundJsonUtils.convertToZonedDateTime(observation_epoch, zoneId); + public Instant getObservationTime() { + return WeatherUndergroundJsonUtils.convertToInstant(observation_epoch); } /** diff --git a/bundles/org.openhab.binding.weatherunderground/src/main/java/org/openhab/binding/weatherunderground/internal/json/WeatherUndergroundJsonForecastDay.java b/bundles/org.openhab.binding.weatherunderground/src/main/java/org/openhab/binding/weatherunderground/internal/json/WeatherUndergroundJsonForecastDay.java index e705fbad843..303babb2c75 100644 --- a/bundles/org.openhab.binding.weatherunderground/src/main/java/org/openhab/binding/weatherunderground/internal/json/WeatherUndergroundJsonForecastDay.java +++ b/bundles/org.openhab.binding.weatherunderground/src/main/java/org/openhab/binding/weatherunderground/internal/json/WeatherUndergroundJsonForecastDay.java @@ -14,8 +14,7 @@ package org.openhab.binding.weatherunderground.internal.json; import java.math.BigDecimal; import java.net.URL; -import java.time.ZoneId; -import java.time.ZonedDateTime; +import java.time.Instant; /** * The {@link WeatherUndergroundJsonForecastDay} is the Java class used @@ -66,8 +65,8 @@ public class WeatherUndergroundJsonForecastDay { * * @return the forecast date and time or null if not defined */ - public ZonedDateTime getForecastTime(ZoneId zoneId) { - return WeatherUndergroundJsonUtils.convertToZonedDateTime((date == null) ? null : date.getEpoch(), zoneId); + public Instant getForecastTime() { + return WeatherUndergroundJsonUtils.convertToInstant((date == null) ? null : date.getEpoch()); } /** diff --git a/bundles/org.openhab.binding.weatherunderground/src/main/java/org/openhab/binding/weatherunderground/internal/json/WeatherUndergroundJsonUtils.java b/bundles/org.openhab.binding.weatherunderground/src/main/java/org/openhab/binding/weatherunderground/internal/json/WeatherUndergroundJsonUtils.java index 0a76bf61e9d..d790fe6165a 100644 --- a/bundles/org.openhab.binding.weatherunderground/src/main/java/org/openhab/binding/weatherunderground/internal/json/WeatherUndergroundJsonUtils.java +++ b/bundles/org.openhab.binding.weatherunderground/src/main/java/org/openhab/binding/weatherunderground/internal/json/WeatherUndergroundJsonUtils.java @@ -17,8 +17,6 @@ import java.net.MalformedURLException; import java.net.URL; import java.time.DateTimeException; import java.time.Instant; -import java.time.ZoneId; -import java.time.ZonedDateTime; import org.slf4j.LoggerFactory; @@ -34,18 +32,17 @@ public class WeatherUndergroundJsonUtils { private static final String TREND_STABLE = "stable"; /** - * Convert a string representing an Epoch value into a Calendar object + * Convert a string representing an Epoch value into an {@link Instant} object * * @param value the Epoch value as a string * - * @return the ZonedDateTime object representing the date and time of the Epoch + * @return the Instant object representing the date and time of the Epoch * or null in case of conversion error */ - public static ZonedDateTime convertToZonedDateTime(String value, ZoneId zoneId) { + public static Instant convertToInstant(String value) { if (isValid(value)) { try { - Instant epochSeconds = Instant.ofEpochSecond(Long.valueOf(value)); - return ZonedDateTime.ofInstant(epochSeconds, zoneId); + return Instant.ofEpochSecond(Long.valueOf(value)); } catch (DateTimeException e) { LoggerFactory.getLogger(WeatherUndergroundJsonUtils.class).debug("Cannot convert {} to ZonedDateTime", value);