]> git.basschouten.com Git - openhab-addons.git/blob
06d20e42b559b2a60c5f88690d832535b5f8a329
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.vigicrues.internal.api;
14
15 import java.io.IOException;
16 import java.time.ZonedDateTime;
17 import java.util.Locale;
18
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;
32
33 import com.google.gson.Gson;
34 import com.google.gson.GsonBuilder;
35 import com.google.gson.JsonDeserializer;
36 import com.google.gson.JsonSyntaxException;
37
38 /**
39  * The {@link ApiHandler} is the responsible to call a given
40  * url and transform the answer in the appropriate dto class
41  *
42  * @author GaĆ«l L'hopital - Initial contribution
43  */
44
45 @Component(service = ApiHandler.class)
46 @NonNullByDefault
47 public class ApiHandler {
48     private static final int TIMEOUT_MS = 30000;
49     private final Gson gson;
50
51     @Activate
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()))
57                 .create();
58     }
59
60     private <T> T execute(String url, Class<T> responseType) throws VigiCruesException {
61         String jsonResponse = "";
62         try {
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);
69         }
70     }
71
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);
75     }
76
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);
80     }
81
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);
85     }
86
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);
90     }
91
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);
95     }
96
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";
99
100         return execute(
101                 BASE_URL + String.format(Locale.US, "&latitude=%.2f&longitude=%.2f&distance=%d",
102                         location.getLatitude().floatValue(), location.getLongitude().floatValue(), range),
103                 HubEauResponse.class);
104     }
105
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);
109     }
110 }