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.vigicrues.internal.api;
15 import java.io.IOException;
16 import java.time.ZonedDateTime;
17 import java.util.Locale;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.vigicrues.internal.dto.hubeau.HubEauResponse;
21 import org.openhab.binding.vigicrues.internal.dto.opendatasoft.OpenDatasoftResponse;
22 import org.openhab.binding.vigicrues.internal.dto.vigicrues.CdStationHydro;
23 import org.openhab.binding.vigicrues.internal.dto.vigicrues.InfoVigiCru;
24 import org.openhab.binding.vigicrues.internal.dto.vigicrues.TerEntVigiCru;
25 import org.openhab.binding.vigicrues.internal.dto.vigicrues.TronEntVigiCru;
26 import org.openhab.core.i18n.TimeZoneProvider;
27 import org.openhab.core.io.net.http.HttpUtil;
28 import org.openhab.core.library.types.PointType;
29 import org.osgi.service.component.annotations.Activate;
30 import org.osgi.service.component.annotations.Component;
31 import org.osgi.service.component.annotations.Reference;
33 import com.google.gson.Gson;
34 import com.google.gson.GsonBuilder;
35 import com.google.gson.JsonDeserializer;
36 import com.google.gson.JsonSyntaxException;
39 * The {@link ApiHandler} is the responsible to call a given
40 * url and transform the answer in the appropriate dto class
42 * @author Gaƫl L'hopital - Initial contribution
45 @Component(service = ApiHandler.class)
47 public class ApiHandler {
48 private static final int TIMEOUT_MS = 30000;
49 private final Gson gson;
52 public ApiHandler(@Reference TimeZoneProvider timeZoneProvider) {
53 this.gson = new GsonBuilder().registerTypeAdapter(ZonedDateTime.class,
54 (JsonDeserializer<ZonedDateTime>) (json, type, jsonDeserializationContext) -> ZonedDateTime
55 .parse(json.getAsJsonPrimitive().getAsString())
56 .withZoneSameInstant(timeZoneProvider.getTimeZone()))
60 private <T> T execute(String url, Class<T> responseType) throws VigiCruesException {
61 String jsonResponse = "";
63 jsonResponse = HttpUtil.executeUrl("GET", url, TIMEOUT_MS);
64 return gson.fromJson(jsonResponse, responseType);
65 } catch (IOException e) {
66 throw new VigiCruesException(e);
67 } catch (JsonSyntaxException e) {
68 throw new VigiCruesException(e);
72 public InfoVigiCru getTronconStatus(String tronconId) throws VigiCruesException {
73 final String BASE_URL = "https://www.vigicrues.gouv.fr/services/1/InfoVigiCru.jsonld/?TypEntVigiCru=8&CdEntVigiCru=%s";
74 return execute(String.format(BASE_URL, tronconId), InfoVigiCru.class);
77 public TronEntVigiCru getTroncon(String stationId) throws VigiCruesException {
78 final String BASE_URL = "https://www.vigicrues.gouv.fr/services/1/TronEntVigiCru.jsonld/?TypEntVigiCru=8&CdEntVigiCru=%s";
79 return execute(String.format(BASE_URL, stationId), TronEntVigiCru.class);
82 public TerEntVigiCru getTerritoire(String stationId) throws VigiCruesException {
83 final String BASE_URL = "https://www.vigicrues.gouv.fr/services/1/TerEntVigiCru.jsonld/?TypEntVigiCru=5&CdEntVigiCru=%s";
84 return execute(String.format(BASE_URL, stationId), TerEntVigiCru.class);
87 public CdStationHydro getStationDetails(String stationId) throws VigiCruesException {
88 final String BASE_URL = "https://www.vigicrues.gouv.fr/services/station.json/index.php?CdStationHydro=%s";
89 return execute(String.format(BASE_URL, stationId), CdStationHydro.class);
92 public OpenDatasoftResponse getMeasures(String stationId) throws VigiCruesException {
93 final String BASE_URL = "https://public.opendatasoft.com/api/records/1.0/search/?dataset=vigicrues&sort=timestamp&q=%s";
94 return execute(String.format(BASE_URL, stationId), OpenDatasoftResponse.class);
97 public HubEauResponse discoverStations(PointType location, int range) throws VigiCruesException {
98 final String BASE_URL = "https://hubeau.eaufrance.fr/api/v1/hydrometrie/referentiel/stations?format=json&size=2000";
101 BASE_URL + String.format(Locale.US, "&latitude=%.2f&longitude=%.2f&distance=%d",
102 location.getLatitude().floatValue(), location.getLongitude().floatValue(), range),
103 HubEauResponse.class);
106 public HubEauResponse discoverStations(String stationId) throws VigiCruesException {
107 final String BASE_URL = "https://hubeau.eaufrance.fr/api/v1/hydrometrie/referentiel/stations?format=json&size=2000";
108 return execute(BASE_URL + String.format("&code_station=%s", stationId), HubEauResponse.class);