Simplify DateTimeType handling for WeatherUnderground

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
This commit is contained in:
Jacob Laursen 2024-12-16 23:13:34 +01:00 committed by lsiepel
parent 9c19aa70e9
commit 82813cdcd4
5 changed files with 15 additions and 33 deletions

View File

@ -29,7 +29,6 @@ import org.openhab.binding.weatherunderground.internal.handler.WeatherUndergroun
import org.openhab.core.config.discovery.DiscoveryService; import org.openhab.core.config.discovery.DiscoveryService;
import org.openhab.core.i18n.LocaleProvider; import org.openhab.core.i18n.LocaleProvider;
import org.openhab.core.i18n.LocationProvider; import org.openhab.core.i18n.LocationProvider;
import org.openhab.core.i18n.TimeZoneProvider;
import org.openhab.core.i18n.UnitProvider; import org.openhab.core.i18n.UnitProvider;
import org.openhab.core.thing.Bridge; import org.openhab.core.thing.Bridge;
import org.openhab.core.thing.Thing; import org.openhab.core.thing.Thing;
@ -64,16 +63,13 @@ public class WeatherUndergroundHandlerFactory extends BaseThingHandlerFactory {
private final LocaleProvider localeProvider; private final LocaleProvider localeProvider;
private final LocationProvider locationProvider; private final LocationProvider locationProvider;
private final UnitProvider unitProvider; private final UnitProvider unitProvider;
private final TimeZoneProvider timeZoneProvider;
@Activate @Activate
public WeatherUndergroundHandlerFactory(final @Reference LocaleProvider localeProvider, public WeatherUndergroundHandlerFactory(final @Reference LocaleProvider localeProvider,
final @Reference LocationProvider locationProvider, final @Reference UnitProvider unitProvider, final @Reference LocationProvider locationProvider, final @Reference UnitProvider unitProvider) {
final @Reference TimeZoneProvider timeZoneProvider) {
this.localeProvider = localeProvider; this.localeProvider = localeProvider;
this.locationProvider = locationProvider; this.locationProvider = locationProvider;
this.unitProvider = unitProvider; this.unitProvider = unitProvider;
this.timeZoneProvider = timeZoneProvider;
} }
@Override @Override
@ -86,7 +82,7 @@ public class WeatherUndergroundHandlerFactory extends BaseThingHandlerFactory {
ThingTypeUID thingTypeUID = thing.getThingTypeUID(); ThingTypeUID thingTypeUID = thing.getThingTypeUID();
if (thingTypeUID.equals(THING_TYPE_WEATHER)) { 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)) { if (thingTypeUID.equals(THING_TYPE_BRIDGE)) {

View File

@ -16,7 +16,6 @@ import static org.openhab.core.library.unit.MetricPrefix.*;
import java.io.IOException; import java.io.IOException;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.time.ZoneId;
import java.util.HashMap; import java.util.HashMap;
import java.util.Locale; import java.util.Locale;
import java.util.Map; 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.WeatherUndergroundJsonForecast;
import org.openhab.binding.weatherunderground.internal.json.WeatherUndergroundJsonForecastDay; import org.openhab.binding.weatherunderground.internal.json.WeatherUndergroundJsonForecastDay;
import org.openhab.core.i18n.LocaleProvider; import org.openhab.core.i18n.LocaleProvider;
import org.openhab.core.i18n.TimeZoneProvider;
import org.openhab.core.i18n.UnitProvider; import org.openhab.core.i18n.UnitProvider;
import org.openhab.core.io.net.http.HttpUtil; import org.openhab.core.io.net.http.HttpUtil;
import org.openhab.core.library.types.DateTimeType; import org.openhab.core.library.types.DateTimeType;
@ -179,7 +177,6 @@ public class WeatherUndergroundHandler extends BaseThingHandler {
private final LocaleProvider localeProvider; private final LocaleProvider localeProvider;
private final UnitProvider unitProvider; private final UnitProvider unitProvider;
private final TimeZoneProvider timeZoneProvider;
private final Gson gson; private final Gson gson;
private final Map<String, Integer> forecastMap; private final Map<String, Integer> forecastMap;
@ -189,12 +186,10 @@ public class WeatherUndergroundHandler extends BaseThingHandler {
private @Nullable WeatherUndergroundBridgeHandler bridgeHandler; private @Nullable WeatherUndergroundBridgeHandler bridgeHandler;
public WeatherUndergroundHandler(Thing thing, LocaleProvider localeProvider, UnitProvider unitProvider, public WeatherUndergroundHandler(Thing thing, LocaleProvider localeProvider, UnitProvider unitProvider) {
TimeZoneProvider timeZoneProvider) {
super(thing); super(thing);
this.localeProvider = localeProvider; this.localeProvider = localeProvider;
this.unitProvider = unitProvider; this.unitProvider = unitProvider;
this.timeZoneProvider = timeZoneProvider;
gson = new Gson(); gson = new Gson();
forecastMap = initForecastDayMap(); forecastMap = initForecastDayMap();
} }
@ -358,9 +353,7 @@ public class WeatherUndergroundHandler extends BaseThingHandler {
case "stationId": case "stationId":
return undefOrState(current.getStationId(), new StringType(current.getStationId())); return undefOrState(current.getStationId(), new StringType(current.getStationId()));
case "observationTime": case "observationTime":
ZoneId zoneId = timeZoneProvider.getTimeZone(); return undefOrState(current.getObservationTime(), new DateTimeType(current.getObservationTime()));
return undefOrState(current.getObservationTime(zoneId),
new DateTimeType(current.getObservationTime(zoneId)));
case "conditions": case "conditions":
return undefOrState(current.getConditions(), new StringType(current.getConditions())); return undefOrState(current.getConditions(), new StringType(current.getConditions()));
case "temperature": case "temperature":
@ -433,9 +426,7 @@ public class WeatherUndergroundHandler extends BaseThingHandler {
String channelTypeId = getChannelTypeId(channelId); String channelTypeId = getChannelTypeId(channelId);
switch (channelTypeId) { switch (channelTypeId) {
case "forecastTime": case "forecastTime":
ZoneId zoneId = timeZoneProvider.getTimeZone(); return undefOrState(dayForecast.getForecastTime(), new DateTimeType(dayForecast.getForecastTime()));
return undefOrState(dayForecast.getForecastTime(zoneId),
new DateTimeType(dayForecast.getForecastTime(zoneId)));
case "conditions": case "conditions":
return undefOrState(dayForecast.getConditions(), new StringType(dayForecast.getConditions())); return undefOrState(dayForecast.getConditions(), new StringType(dayForecast.getConditions()));
case "minTemperature": case "minTemperature":

View File

@ -14,8 +14,7 @@ package org.openhab.binding.weatherunderground.internal.json;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.net.URL; import java.net.URL;
import java.time.ZoneId; import java.time.Instant;
import java.time.ZonedDateTime;
/** /**
* The {@link WeatherUndergroundJsonCurrent} is the Java class used * 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 * @return the observation date and time or null if not defined
*/ */
public ZonedDateTime getObservationTime(ZoneId zoneId) { public Instant getObservationTime() {
return WeatherUndergroundJsonUtils.convertToZonedDateTime(observation_epoch, zoneId); return WeatherUndergroundJsonUtils.convertToInstant(observation_epoch);
} }
/** /**

View File

@ -14,8 +14,7 @@ package org.openhab.binding.weatherunderground.internal.json;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.net.URL; import java.net.URL;
import java.time.ZoneId; import java.time.Instant;
import java.time.ZonedDateTime;
/** /**
* The {@link WeatherUndergroundJsonForecastDay} is the Java class used * 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 * @return the forecast date and time or null if not defined
*/ */
public ZonedDateTime getForecastTime(ZoneId zoneId) { public Instant getForecastTime() {
return WeatherUndergroundJsonUtils.convertToZonedDateTime((date == null) ? null : date.getEpoch(), zoneId); return WeatherUndergroundJsonUtils.convertToInstant((date == null) ? null : date.getEpoch());
} }
/** /**

View File

@ -17,8 +17,6 @@ import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.time.DateTimeException; import java.time.DateTimeException;
import java.time.Instant; import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -34,18 +32,17 @@ public class WeatherUndergroundJsonUtils {
private static final String TREND_STABLE = "stable"; 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 * @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 * or null in case of conversion error
*/ */
public static ZonedDateTime convertToZonedDateTime(String value, ZoneId zoneId) { public static Instant convertToInstant(String value) {
if (isValid(value)) { if (isValid(value)) {
try { try {
Instant epochSeconds = Instant.ofEpochSecond(Long.valueOf(value)); return Instant.ofEpochSecond(Long.valueOf(value));
return ZonedDateTime.ofInstant(epochSeconds, zoneId);
} catch (DateTimeException e) { } catch (DateTimeException e) {
LoggerFactory.getLogger(WeatherUndergroundJsonUtils.class).debug("Cannot convert {} to ZonedDateTime", LoggerFactory.getLogger(WeatherUndergroundJsonUtils.class).debug("Cannot convert {} to ZonedDateTime",
value); value);