Skip to content

Commit

Permalink
locale/location
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Grześlowski <martin.grzeslowski@gmail.com>
  • Loading branch information
magx2 committed Aug 13, 2024
1 parent 4492bb2 commit c5a6871
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -1,36 +1,18 @@
/**
* Copyright (c) 2010-2024 Contributors to the openHAB project
*
* <p>
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* <p>
* 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
*
* <p>
* 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;
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -40,10 +42,14 @@ public class VisualCrossingHandlerFactory extends BaseThingHandlerFactory {

private static final Set<ThingTypeUID> 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
Expand All @@ -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;
Expand Down

0 comments on commit c5a6871

Please sign in to comment.