]> git.basschouten.com Git - openhab-addons.git/blob
2fdf7b35a77b37bc4ed6dc73cbf83bc023012269
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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 javax.ws.rs.HttpMethod;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.openhab.binding.vigicrues.internal.dto.hubeau.HubEauResponse;
23 import org.openhab.binding.vigicrues.internal.dto.opendatasoft.OpenDatasoftResponse;
24 import org.openhab.binding.vigicrues.internal.dto.vigicrues.CdStationHydro;
25 import org.openhab.binding.vigicrues.internal.dto.vigicrues.InfoVigiCru;
26 import org.openhab.binding.vigicrues.internal.dto.vigicrues.TerEntVigiCru;
27 import org.openhab.binding.vigicrues.internal.dto.vigicrues.TronEntVigiCru;
28 import org.openhab.core.i18n.TimeZoneProvider;
29 import org.openhab.core.io.net.http.HttpUtil;
30 import org.openhab.core.library.types.PointType;
31 import org.osgi.service.component.annotations.Activate;
32 import org.osgi.service.component.annotations.Component;
33 import org.osgi.service.component.annotations.Reference;
34
35 import com.google.gson.Gson;
36 import com.google.gson.GsonBuilder;
37 import com.google.gson.JsonDeserializer;
38 import com.google.gson.JsonSyntaxException;
39
40 /**
41  * The {@link ApiHandler} is the responsible to call a given
42  * url and transform the answer in the appropriate dto class
43  *
44  * @author GaĆ«l L'hopital - Initial contribution
45  */
46
47 @Component(service = ApiHandler.class)
48 @NonNullByDefault
49 public class ApiHandler {
50     private static final String HUBEAU_URL = "https://hubeau.eaufrance.fr/api/v1/hydrometrie/referentiel/stations?format=json&size=2000";
51     private static final int TIMEOUT_MS = 30000;
52
53     private final Gson gson;
54
55     @Activate
56     public ApiHandler(@Reference TimeZoneProvider timeZoneProvider) {
57         this.gson = new GsonBuilder().registerTypeAdapter(ZonedDateTime.class,
58                 (JsonDeserializer<ZonedDateTime>) (json, type, jsonDeserializationContext) -> ZonedDateTime
59                         .parse(json.getAsJsonPrimitive().getAsString())
60                         .withZoneSameInstant(timeZoneProvider.getTimeZone()))
61                 .create();
62     }
63
64     private <T> T execute(String url, Class<T> responseType) throws VigiCruesException {
65         try {
66             String jsonResponse = HttpUtil.executeUrl(HttpMethod.GET, url, TIMEOUT_MS);
67             return gson.fromJson(jsonResponse, responseType);
68         } catch (IOException | JsonSyntaxException e) {
69             throw new VigiCruesException(e);
70         }
71     }
72
73     public InfoVigiCru getTronconStatus(String tronconId) throws VigiCruesException {
74         final String baseUrl = "https://www.vigicrues.gouv.fr/services/1/InfoVigiCru.jsonld/?TypEntVigiCru=8&CdEntVigiCru=%s";
75         return execute(String.format(baseUrl, tronconId), InfoVigiCru.class);
76     }
77
78     public TronEntVigiCru getTroncon(String stationId) throws VigiCruesException {
79         final String baseUrl = "https://www.vigicrues.gouv.fr/services/1/TronEntVigiCru.jsonld/?TypEntVigiCru=8&CdEntVigiCru=%s";
80         return execute(String.format(baseUrl, stationId), TronEntVigiCru.class);
81     }
82
83     public TerEntVigiCru getTerritoire(String stationId) throws VigiCruesException {
84         final String baseUrl = "https://www.vigicrues.gouv.fr/services/1/TerEntVigiCru.jsonld/?TypEntVigiCru=5&CdEntVigiCru=%s";
85         return execute(String.format(baseUrl, stationId), TerEntVigiCru.class);
86     }
87
88     public CdStationHydro getStationDetails(String stationId) throws VigiCruesException {
89         final String baseUrl = "https://www.vigicrues.gouv.fr/services/station.json/index.php?CdStationHydro=%s";
90         return execute(String.format(baseUrl, stationId), CdStationHydro.class);
91     }
92
93     public OpenDatasoftResponse getMeasures(String stationId) throws VigiCruesException {
94         final String baseUrl = "https://public.opendatasoft.com/api/records/1.0/search/?dataset=vigicrues&sort=timestamp&q=%s";
95         return execute(String.format(baseUrl, stationId), OpenDatasoftResponse.class);
96     }
97
98     public HubEauResponse discoverStations(PointType location, int range) throws VigiCruesException {
99         return execute(
100                 String.format(Locale.US, "%s&latitude=%.2f&longitude=%.2f&distance=%d", HUBEAU_URL,
101                         location.getLatitude().floatValue(), location.getLongitude().floatValue(), range),
102                 HubEauResponse.class);
103     }
104
105     public HubEauResponse discoverStations(String stationId) throws VigiCruesException {
106         return execute(String.format("%s&code_station=%s", HUBEAU_URL, stationId), HubEauResponse.class);
107     }
108 }