Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[weatherunderground] Consider TimeZoneProvider to get the timezone #7878

Merged
merged 2 commits into from
Jun 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.eclipse.smarthome.config.discovery.DiscoveryService;
import org.eclipse.smarthome.core.i18n.LocaleProvider;
import org.eclipse.smarthome.core.i18n.LocationProvider;
import org.eclipse.smarthome.core.i18n.TimeZoneProvider;
import org.eclipse.smarthome.core.i18n.UnitProvider;
import org.eclipse.smarthome.core.thing.Bridge;
import org.eclipse.smarthome.core.thing.Thing;
Expand Down Expand Up @@ -58,18 +59,21 @@ public class WeatherUndergroundHandlerFactory extends BaseThingHandlerFactory {
.of(BRIDGE_THING_TYPES_UIDS, WeatherUndergroundBindingConstants.SUPPORTED_THING_TYPES_UIDS)
.flatMap(x -> x.stream()).collect(Collectors.toSet());

private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
private final Map<ThingUID, @Nullable ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();

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 LocationProvider locationProvider, final @Reference UnitProvider unitProvider,
final @Reference TimeZoneProvider timeZoneProvider) {
this.localeProvider = localeProvider;
this.locationProvider = locationProvider;
this.unitProvider = unitProvider;
this.timeZoneProvider = timeZoneProvider;
}

@Override
Expand All @@ -82,7 +86,7 @@ public boolean supportsThingType(ThingTypeUID thingTypeUID) {
ThingTypeUID thingTypeUID = thing.getThingTypeUID();

if (thingTypeUID.equals(THING_TYPE_WEATHER)) {
return new WeatherUndergroundHandler(thing, localeProvider, unitProvider);
return new WeatherUndergroundHandler(thing, localeProvider, unitProvider, timeZoneProvider);
}

if (thingTypeUID.equals(THING_TYPE_BRIDGE)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import java.io.IOException;
import java.math.BigDecimal;
import java.time.ZoneId;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
Expand All @@ -31,6 +32,7 @@
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.core.i18n.LocaleProvider;
import org.eclipse.smarthome.core.i18n.TimeZoneProvider;
import org.eclipse.smarthome.core.i18n.UnitProvider;
import org.eclipse.smarthome.core.library.types.DateTimeType;
import org.eclipse.smarthome.core.library.types.DecimalType;
Expand Down Expand Up @@ -85,7 +87,7 @@ public class WeatherUndergroundHandler extends BaseThingHandler {
private static final Set<String> USUAL_FEATURES = Stream.of(FEATURE_CONDITIONS, FEATURE_FORECAST10DAY)
.collect(Collectors.toSet());

private static final Map<String, String> LANG_ISO_TO_WU_CODES = new HashMap<>();
private static final Map<String, @Nullable String> LANG_ISO_TO_WU_CODES = new HashMap<>();
// Codes from https://www.wunderground.com/weather/api/d/docs?d=language-support
static {
LANG_ISO_TO_WU_CODES.put("AF", "AF");
Expand Down Expand Up @@ -169,27 +171,30 @@ public class WeatherUndergroundHandler extends BaseThingHandler {
// Yiddish - transliterated => JI
LANG_ISO_TO_WU_CODES.put("YI", "YI");
}
private static final Map<String, String> LANG_COUNTRY_TO_WU_CODES = new HashMap<>();
private static final Map<String, @Nullable String> LANG_COUNTRY_TO_WU_CODES = new HashMap<>();
static {
LANG_COUNTRY_TO_WU_CODES.put("en-GB", "LI"); // British English
LANG_COUNTRY_TO_WU_CODES.put("fr-CA", "FC"); // French Canadian
}

private final LocaleProvider localeProvider;
private final UnitProvider unitProvider;
private final TimeZoneProvider timeZoneProvider;
private final Gson gson;
private final Map<String, Integer> forecastMap;
@Nullable
private ScheduledFuture<?> refreshJob;
@Nullable
private WeatherUndergroundJsonData weatherData;
@Nullable
private WeatherUndergroundBridgeHandler bridgeHandler;

public WeatherUndergroundHandler(Thing thing, LocaleProvider localeProvider, UnitProvider unitProvider) {

private @Nullable ScheduledFuture<?> refreshJob;

private @Nullable WeatherUndergroundJsonData weatherData;

private @Nullable WeatherUndergroundBridgeHandler bridgeHandler;

public WeatherUndergroundHandler(Thing thing, LocaleProvider localeProvider, UnitProvider unitProvider,
TimeZoneProvider timeZoneProvider) {
super(thing);
this.localeProvider = localeProvider;
this.unitProvider = unitProvider;
this.timeZoneProvider = timeZoneProvider;
gson = new Gson();
forecastMap = initForecastDayMap();
}
Expand Down Expand Up @@ -270,7 +275,8 @@ private void initializeThingHandler(@Nullable ThingHandler bridgeHandler, @Nulla
* Start the job refreshing the weather data
*/
private void startAutomaticRefresh() {
if (refreshJob == null || refreshJob.isCancelled()) {
ScheduledFuture<?> job = refreshJob;
if (job == null || job.isCancelled()) {
Runnable runnable = new Runnable() {
@Override
public void run() {
Expand Down Expand Up @@ -298,10 +304,11 @@ public void run() {
public void dispose() {
logger.debug("Disposing WeatherUnderground handler.");

if (refreshJob != null && !refreshJob.isCancelled()) {
refreshJob.cancel(true);
refreshJob = null;
ScheduledFuture<?> job = refreshJob;
if (job != null) {
job.cancel(true);
}
refreshJob = null;
}

@Override
Expand All @@ -322,11 +329,12 @@ public void handleCommand(ChannelUID channelUID, Command command) {
private void updateChannel(String channelId) {
if (isLinked(channelId)) {
State state = null;
if (weatherData != null) {
WeatherUndergroundJsonData data = weatherData;
if (data != null) {
if (channelId.startsWith("current")) {
state = updateCurrentObservationChannel(channelId, weatherData.getCurrent());
state = updateCurrentObservationChannel(channelId, data.getCurrent());
} else if (channelId.startsWith("forecast")) {
state = updateForecastChannel(channelId, weatherData.getForecast());
state = updateForecastChannel(channelId, data.getForecast());
}
}

Expand All @@ -350,7 +358,9 @@ private void updateChannel(String channelId) {
case "stationId":
return undefOrState(current.getStationId(), new StringType(current.getStationId()));
case "observationTime":
return undefOrState(current.getObservationTime(), new DateTimeType(current.getObservationTime()));
ZoneId zoneId = timeZoneProvider.getTimeZone();
return undefOrState(current.getObservationTime(zoneId),
new DateTimeType(current.getObservationTime(zoneId)));
case "conditions":
return undefOrState(current.getConditions(), new StringType(current.getConditions()));
case "temperature":
Expand Down Expand Up @@ -423,7 +433,9 @@ private void updateChannel(String channelId) {
String channelTypeId = getChannelTypeId(channelId);
switch (channelTypeId) {
case "forecastTime":
return undefOrState(dayForecast.getForecastTime(), new DateTimeType(dayForecast.getForecastTime()));
ZoneId zoneId = timeZoneProvider.getTimeZone();
return undefOrState(dayForecast.getForecastTime(zoneId),
new DateTimeType(dayForecast.getForecastTime(zoneId)));
case "conditions":
return undefOrState(dayForecast.getConditions(), new StringType(dayForecast.getConditions()));
case "minTemperature":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.math.BigDecimal;
import java.net.URL;
import java.time.ZoneId;
import java.time.ZonedDateTime;

/**
Expand Down Expand Up @@ -133,8 +134,8 @@ public String getStationId() {
*
* @return the observation date and time or null if not defined
*/
public ZonedDateTime getObservationTime() {
return WeatherUndergroundJsonUtils.convertToZonedDateTime(observation_epoch);
public ZonedDateTime getObservationTime(ZoneId zoneId) {
return WeatherUndergroundJsonUtils.convertToZonedDateTime(observation_epoch, zoneId);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.math.BigDecimal;
import java.net.URL;
import java.time.ZoneId;
import java.time.ZonedDateTime;

/**
Expand Down Expand Up @@ -65,8 +66,8 @@ public WeatherUndergroundJsonForecastDay() {
*
* @return the forecast date and time or null if not defined
*/
public ZonedDateTime getForecastTime() {
return WeatherUndergroundJsonUtils.convertToZonedDateTime((date == null) ? null : date.getEpoch());
public ZonedDateTime getForecastTime(ZoneId zoneId) {
return WeatherUndergroundJsonUtils.convertToZonedDateTime((date == null) ? null : date.getEpoch(), zoneId);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
import java.net.URL;
import java.time.DateTimeException;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.TimeZone;

import org.slf4j.LoggerFactory;

Expand All @@ -41,11 +41,11 @@ public class WeatherUndergroundJsonUtils {
* @return the ZonedDateTime object representing the date and time of the Epoch
* or null in case of conversion error
*/
public static ZonedDateTime convertToZonedDateTime(String value) {
public static ZonedDateTime convertToZonedDateTime(String value, ZoneId zoneId) {
if (isValid(value)) {
try {
Instant epochSeconds = Instant.ofEpochSecond(Long.valueOf(value));
return ZonedDateTime.ofInstant(epochSeconds, TimeZone.getDefault().toZoneId());
return ZonedDateTime.ofInstant(epochSeconds, zoneId);
} catch (DateTimeException e) {
LoggerFactory.getLogger(WeatherUndergroundJsonUtils.class).debug("Cannot convert {} to ZonedDateTime",
value);
Expand Down