]> git.basschouten.com Git - openhab-addons.git/blob
07fa36870ffc296506d07b221f5dc8edeb6184a5
[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.tankerkoenig.internal.data;
14
15 import java.io.IOException;
16 import java.net.MalformedURLException;
17 import java.util.Properties;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.tankerkoenig.internal.dto.TankerkoenigDetailResult;
22 import org.openhab.binding.tankerkoenig.internal.dto.TankerkoenigListResult;
23 import org.openhab.binding.tankerkoenig.internal.serializer.CustomTankerkoenigDetailResultDeserializer;
24 import org.openhab.binding.tankerkoenig.internal.serializer.CustomTankerkoenigListResultDeserializer;
25 import org.openhab.core.io.net.http.HttpUtil;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 import com.google.gson.Gson;
30 import com.google.gson.GsonBuilder;
31
32 /***
33  * Serivce class requesting data from tankerkoenig api and providing result objects
34  *
35  * @author Dennis Dollinger - Initial contribution
36  * @author Juergen Baginski - Initial contribution
37  */
38 @NonNullByDefault
39 public class TankerkoenigService {
40     private final Logger logger = LoggerFactory.getLogger(TankerkoenigService.class);
41
42     private final Gson gson = new GsonBuilder()
43             .registerTypeAdapter(TankerkoenigListResult.class, new CustomTankerkoenigListResultDeserializer()).create();
44     private final Gson gsonDetail = new GsonBuilder()
45             .registerTypeAdapter(TankerkoenigDetailResult.class, new CustomTankerkoenigDetailResultDeserializer())
46             .create();
47     private static final int REQUEST_TIMEOUT = 5000;
48
49     public @Nullable TankerkoenigListResult getStationListData(String apikey, String locationIDs, String userAgent) {
50         return getTankerkoenigListResult(apikey, locationIDs, userAgent);
51     }
52
53     public @Nullable TankerkoenigDetailResult getStationDetailData(String apikey, String locationID, String userAgent) {
54         return getTankerkoenigDetailResult(apikey, locationID, userAgent);
55     }
56
57     private @Nullable String getResponseString(String apiKey, String locationIDs, String userAgent, boolean detail)
58             throws IOException {
59         StringBuilder sb = new StringBuilder();
60         sb.append("https://creativecommons.tankerkoenig.de/json/");
61         if (detail) {
62             sb.append("detail.php?id=");
63         } else {
64             sb.append("prices.php?ids=");
65         }
66         sb.append(locationIDs);
67         sb.append("&apikey=");
68         sb.append(apiKey);
69         String url = sb.toString();
70         try {
71             Properties urlHeader = new Properties();
72             urlHeader.put("USER-AGENT", userAgent);
73             return HttpUtil.executeUrl("GET", url, urlHeader, null, "", REQUEST_TIMEOUT);
74         } catch (MalformedURLException e) {
75             logger.debug("Error in getResponseString: ", e);
76             return null;
77         }
78     }
79
80     private @Nullable TankerkoenigListResult getTankerkoenigListResult(String apikey, String locationIDs,
81             String userAgent) {
82         try {
83             String jsonData = getResponseString(apikey, locationIDs, userAgent, false);
84             logger.debug("json-String: {}", jsonData);
85             return gson.fromJson(jsonData, TankerkoenigListResult.class);
86         } catch (IOException e) {
87             logger.debug("Error in getTankerkoenigListResult: ", e);
88             // the return of an empty result will force the status-update OFFLINE!
89             return TankerkoenigListResult.emptyResult();
90         }
91     }
92
93     private @Nullable TankerkoenigDetailResult getTankerkoenigDetailResult(String apiKey, String locationID,
94             String userAgent) {
95         try {
96             String jsonData = getResponseString(apiKey, locationID, userAgent, true);
97             logger.debug("getTankerkoenigDetailResult jsonData : {}", jsonData);
98             return gsonDetail.fromJson(jsonData, TankerkoenigDetailResult.class);
99         } catch (IOException e) {
100             logger.debug("getTankerkoenigDetailResult IOException: ", e);
101             // the return of an empty result will force the status-update OFFLINE!
102             return TankerkoenigDetailResult.emptyResult();
103         }
104     }
105 }