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.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)) {

View File

@ -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<String, Integer> 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":

View File

@ -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);
}
/**

View File

@ -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());
}
/**

View File

@ -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);