2 * Copyright (c) 2010-2021 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.openuv.internal.handler;
15 import java.io.IOException;
16 import java.time.Duration;
17 import java.time.LocalDate;
18 import java.time.LocalDateTime;
19 import java.util.Collection;
20 import java.util.Collections;
21 import java.util.Properties;
22 import java.util.concurrent.ScheduledFuture;
23 import java.util.concurrent.TimeUnit;
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.openhab.binding.openuv.internal.OpenUVException;
28 import org.openhab.binding.openuv.internal.config.BridgeConfiguration;
29 import org.openhab.binding.openuv.internal.discovery.OpenUVDiscoveryService;
30 import org.openhab.binding.openuv.internal.json.OpenUVResponse;
31 import org.openhab.binding.openuv.internal.json.OpenUVResult;
32 import org.openhab.core.i18n.LocationProvider;
33 import org.openhab.core.io.net.http.HttpUtil;
34 import org.openhab.core.library.types.PointType;
35 import org.openhab.core.thing.Bridge;
36 import org.openhab.core.thing.ChannelUID;
37 import org.openhab.core.thing.ThingStatus;
38 import org.openhab.core.thing.ThingStatusDetail;
39 import org.openhab.core.thing.binding.BaseBridgeHandler;
40 import org.openhab.core.thing.binding.ThingHandlerService;
41 import org.openhab.core.types.Command;
42 import org.openhab.core.types.RefreshType;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
46 import com.google.gson.Gson;
49 * {@link OpenUVBridgeHandler} is the handler for OpenUV API and connects it
52 * @author Gaƫl L'hopital - Initial contribution
56 public class OpenUVBridgeHandler extends BaseBridgeHandler {
57 private static final String QUERY_URL = "https://api.openuv.io/api/v1/uv?lat=%s&lng=%s&alt=%s";
58 private static final int REQUEST_TIMEOUT_MS = (int) TimeUnit.SECONDS.toMillis(30);
60 private final Logger logger = LoggerFactory.getLogger(OpenUVBridgeHandler.class);
61 private final Properties header = new Properties();
62 private final Gson gson;
63 private final LocationProvider locationProvider;
65 private @Nullable ScheduledFuture<?> reconnectJob;
67 public OpenUVBridgeHandler(Bridge bridge, LocationProvider locationProvider, Gson gson) {
70 this.locationProvider = locationProvider;
74 public void initialize() {
75 logger.debug("Initializing OpenUV API bridge handler.");
76 BridgeConfiguration config = getConfigAs(BridgeConfiguration.class);
77 if (config.apikey.isEmpty()) {
78 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
79 "@text/offline.config-error-unknown-apikey");
82 header.put("x-access-token", config.apikey);
87 public void dispose() {
88 ScheduledFuture<?> job = this.reconnectJob;
89 if (job != null && !job.isCancelled()) {
96 public void handleCommand(ChannelUID channelUID, Command command) {
97 if (command instanceof RefreshType) {
101 logger.debug("The OpenUV bridge only handles Refresh command and not '{}'", command);
104 private void initiateConnexion() {
105 // Just checking if the provided api key is a valid one by making a fake call
106 getUVData("0", "0", "0");
109 public @Nullable OpenUVResult getUVData(String latitude, String longitude, String altitude) {
111 String jsonData = HttpUtil.executeUrl("GET", String.format(QUERY_URL, latitude, longitude, altitude),
112 header, null, null, REQUEST_TIMEOUT_MS);
113 OpenUVResponse uvResponse = gson.fromJson(jsonData, OpenUVResponse.class);
114 if (uvResponse != null) {
115 String error = uvResponse.getError();
117 updateStatus(ThingStatus.ONLINE);
118 return uvResponse.getResult();
120 throw new OpenUVException(error);
122 } catch (IOException e) {
123 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
124 } catch (OpenUVException e) {
125 if (e.isQuotaError()) {
126 LocalDate today = LocalDate.now();
127 LocalDate tomorrow = today.plusDays(1);
128 LocalDateTime tomorrowMidnight = tomorrow.atStartOfDay().plusMinutes(2);
130 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, String
131 .format("@text/offline.comm-error-quota-exceeded [ \"%s\" ]", tomorrowMidnight.toString()));
133 reconnectJob = scheduler.schedule(this::initiateConnexion,
134 Duration.between(LocalDateTime.now(), tomorrowMidnight).toMinutes(), TimeUnit.MINUTES);
136 updateStatus(ThingStatus.OFFLINE,
137 e.isApiKeyError() ? ThingStatusDetail.CONFIGURATION_ERROR : ThingStatusDetail.NONE,
145 public Collection<Class<? extends ThingHandlerService>> getServices() {
146 return Collections.singleton(OpenUVDiscoveryService.class);
149 public @Nullable PointType getLocation() {
150 return locationProvider.getLocation();