diff --git a/bundles/org.openhab.binding.visualcrossing/src/main/java/org/openhab/binding/visualcrossing/internal/VisualCrossingHandler.java b/bundles/org.openhab.binding.visualcrossing/src/main/java/org/openhab/binding/visualcrossing/internal/VisualCrossingHandler.java index c495d185e8fc6..6d458d13603b6 100644 --- a/bundles/org.openhab.binding.visualcrossing/src/main/java/org/openhab/binding/visualcrossing/internal/VisualCrossingHandler.java +++ b/bundles/org.openhab.binding.visualcrossing/src/main/java/org/openhab/binding/visualcrossing/internal/VisualCrossingHandler.java @@ -1,36 +1,18 @@ /** * Copyright (c) 2010-2024 Contributors to the openHAB project - * + *

* See the NOTICE file(s) distributed with this work for additional * information. - * + *

* This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at * http://www.eclipse.org/legal/epl-2.0 - * + *

* SPDX-License-Identifier: EPL-2.0 */ package org.openhab.binding.visualcrossing.internal; -import static java.util.Objects.*; -import static java.util.Optional.ofNullable; -import static java.util.concurrent.TimeUnit.SECONDS; -import static org.openhab.binding.visualcrossing.internal.TypeBuilder.*; -import static org.openhab.binding.visualcrossing.internal.VisualCrossingBindingConstants.Channels.BasicChannelGroup.*; -import static org.openhab.binding.visualcrossing.internal.VisualCrossingBindingConstants.Channels.CurrentConditions.*; -import static org.openhab.binding.visualcrossing.internal.VisualCrossingBindingConstants.SUPPORTED_LANGUAGES; -import static org.openhab.core.thing.ThingStatus.OFFLINE; -import static org.openhab.core.thing.ThingStatus.ONLINE; -import static org.openhab.core.thing.ThingStatusDetail.*; -import static org.openhab.core.types.RefreshType.REFRESH; -import static org.openhab.core.types.UnDefType.*; - -import java.net.MalformedURLException; -import java.net.URL; -import java.util.List; -import java.util.concurrent.ScheduledFuture; -import java.util.concurrent.atomic.AtomicReference; - +import com.google.gson.Gson; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.openhab.binding.visualcrossing.internal.VisualCrossingBindingConstants.Channels.ChannelDay; @@ -46,8 +28,11 @@ import org.openhab.binding.visualcrossing.internal.api.rest.HttpClient; import org.openhab.binding.visualcrossing.internal.api.rest.RestClient; import org.openhab.binding.visualcrossing.internal.api.rest.RetryHttpClient; +import org.openhab.core.i18n.LocaleProvider; +import org.openhab.core.i18n.LocationProvider; import org.openhab.core.io.net.http.HttpClientFactory; import org.openhab.core.library.types.DecimalType; +import org.openhab.core.library.types.PointType; import org.openhab.core.thing.ChannelUID; import org.openhab.core.thing.Thing; import org.openhab.core.thing.binding.BaseThingHandler; @@ -57,7 +42,24 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.google.gson.Gson; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.List; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.atomic.AtomicReference; + +import static java.util.Objects.requireNonNull; +import static java.util.Optional.ofNullable; +import static java.util.concurrent.TimeUnit.SECONDS; +import static org.openhab.binding.visualcrossing.internal.TypeBuilder.*; +import static org.openhab.binding.visualcrossing.internal.VisualCrossingBindingConstants.Channels.BasicChannelGroup.*; +import static org.openhab.binding.visualcrossing.internal.VisualCrossingBindingConstants.Channels.CurrentConditions.*; +import static org.openhab.binding.visualcrossing.internal.VisualCrossingBindingConstants.SUPPORTED_LANGUAGES; +import static org.openhab.core.thing.ThingStatus.OFFLINE; +import static org.openhab.core.thing.ThingStatus.ONLINE; +import static org.openhab.core.thing.ThingStatusDetail.*; +import static org.openhab.core.types.RefreshType.REFRESH; +import static org.openhab.core.types.UnDefType.UNDEF; /** * The {@link VisualCrossingHandler} is responsible for handling commands, which are @@ -70,6 +72,8 @@ public class VisualCrossingHandler extends BaseThingHandler { private final Logger logger = LoggerFactory.getLogger(VisualCrossingHandler.class); private final HttpClientFactory httpClientFactory; + private final LocaleProvider localeProvider; + private final LocationProvider locationProvider; private final AtomicReference<@Nullable WeatherResponse> weatherResponse = new AtomicReference<>(); @Nullable private VisualCrossingApi api; @@ -80,9 +84,11 @@ public class VisualCrossingHandler extends BaseThingHandler { @Nullable private ScheduledFuture schedule; - public VisualCrossingHandler(Thing thing, HttpClientFactory httpClientFactory) { + public VisualCrossingHandler(Thing thing, HttpClientFactory httpClientFactory, LocaleProvider localeProvider, LocationProvider locationProvider) { super(thing); this.httpClientFactory = httpClientFactory; + this.localeProvider = localeProvider; + this.locationProvider = locationProvider; } @Override @@ -475,15 +481,20 @@ public void initialize() { if (config.location != null && !config.location.isBlank()) { location = config.location; } else { - // todo location from OH + var pointType = locationProvider.getLocation(); + if(pointType != null) { + var latitude = pointType.getLatitude(); + var longitude = pointType.getLongitude(); + location = "%s,%s".formatted(latitude, longitude); + } } if (config.lang != null && !config.lang.isBlank()) { lang = config.lang; } else { - // todo location from OH + lang = localeProvider.getLocale().getLanguage().toLowerCase(); } - if (lang != null && !SUPPORTED_LANGUAGES.contains(lang)) { + if (!SUPPORTED_LANGUAGES.contains(lang)) { updateStatus(OFFLINE, CONFIGURATION_ERROR, "@text/addon.visualcrossing.weather.error.bad-language [\"%s\"]".formatted(lang)); return; @@ -539,7 +550,7 @@ public void dispose() { @Nullable public WeatherResponse timeline(@Nullable String location, @Nullable UnitGroup unitGroup, @Nullable String lang, - @Nullable String dateFrom, @Nullable String dateTo) + @Nullable String dateFrom, @Nullable String dateTo) throws VisualCrossingAuthException, VisualCrossingApiException, VisualCrossingRateException { var localApi = api; if (localApi == null) { diff --git a/bundles/org.openhab.binding.visualcrossing/src/main/java/org/openhab/binding/visualcrossing/internal/VisualCrossingHandlerFactory.java b/bundles/org.openhab.binding.visualcrossing/src/main/java/org/openhab/binding/visualcrossing/internal/VisualCrossingHandlerFactory.java index 563886a06c691..43bb5362f857c 100644 --- a/bundles/org.openhab.binding.visualcrossing/src/main/java/org/openhab/binding/visualcrossing/internal/VisualCrossingHandlerFactory.java +++ b/bundles/org.openhab.binding.visualcrossing/src/main/java/org/openhab/binding/visualcrossing/internal/VisualCrossingHandlerFactory.java @@ -18,6 +18,8 @@ import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; +import org.openhab.core.i18n.LocaleProvider; +import org.openhab.core.i18n.LocationProvider; import org.openhab.core.io.net.http.HttpClientFactory; import org.openhab.core.thing.Thing; import org.openhab.core.thing.ThingTypeUID; @@ -40,10 +42,14 @@ public class VisualCrossingHandlerFactory extends BaseThingHandlerFactory { private static final Set SUPPORTED_THING_TYPES_UIDS = Set.of(WEATHER_THING_TYPE); protected final @NonNullByDefault({}) HttpClientFactory httpClientFactory; + protected final @NonNullByDefault({}) LocaleProvider localeProvider; + protected final @NonNullByDefault({}) LocationProvider locationProvider; @Activate - public VisualCrossingHandlerFactory(@Reference HttpClientFactory httpClientFactory) { + public VisualCrossingHandlerFactory(@Reference HttpClientFactory httpClientFactory, @Reference LocaleProvider localeProvider, @Reference LocationProvider locationProvider) { this.httpClientFactory = httpClientFactory; + this.localeProvider = localeProvider; + this.locationProvider = locationProvider; } @Override @@ -56,7 +62,7 @@ public boolean supportsThingType(ThingTypeUID thingTypeUID) { ThingTypeUID thingTypeUID = thing.getThingTypeUID(); if (WEATHER_THING_TYPE.equals(thingTypeUID)) { - return new VisualCrossingHandler(thing, httpClientFactory); + return new VisualCrossingHandler(thing, httpClientFactory, localeProvider, locationProvider); } return null;