]> git.basschouten.com Git - openhab-addons.git/blob
0fa89d4e41955150f9789e33fd35c30f413814f3
[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.nzwateralerts.internal.api;
14
15 import static org.openhab.binding.nzwateralerts.internal.NZWaterAlertsBindingConstants.*;
16
17 import java.util.concurrent.ExecutionException;
18 import java.util.concurrent.TimeUnit;
19 import java.util.concurrent.TimeoutException;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.eclipse.jetty.client.HttpClient;
24 import org.eclipse.jetty.client.api.ContentResponse;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * The {@link WaterAlertWebClient} class contains the logic to get data from a URL.
30  *
31  * @author Stewart Cossey - Initial contribution
32  */
33 @NonNullByDefault
34 public class WaterAlertWebClient {
35     private final Logger logger = LoggerFactory.getLogger(WaterAlertWebClient.class);
36
37     private static final int REQUEST_TIMEOUT = 10;
38
39     private final HttpClient httpClient;
40     private final String webService;
41     private final String region;
42     private final String area;
43
44     private @Nullable WaterWebService service = null;
45
46     public WaterAlertWebClient(final HttpClient httpClient, final String location) {
47         this.httpClient = httpClient;
48
49         final String[] locationSegmented = location.split(":", 3);
50         webService = locationSegmented[0];
51         region = locationSegmented[1];
52         area = locationSegmented[2];
53
54         for (final WaterWebService srv : WATER_WEB_SERVICES) {
55             logger.trace("Checking service {}", srv.service());
56             if (locationSegmented[0].equalsIgnoreCase(srv.service())) {
57                 logger.trace("Found service {}", srv.service());
58                 service = srv;
59             }
60         }
61
62         if (service == null) {
63             logger.debug("Service could not be found for {}", locationSegmented[0]);
64         }
65     }
66
67     public @Nullable Integer getLevel() {
68         ContentResponse response;
69         final WaterWebService localService = service;
70         try {
71             if (localService != null) {
72                 logger.debug("Getting Water Level from service {} region {} area {}", webService, region, area);
73
74                 final String endpoint = localService.endpoint(region);
75                 logger.trace("Getting data from endpoint {} with timeout {}", endpoint, REQUEST_TIMEOUT);
76                 response = httpClient.newRequest(endpoint).timeout(REQUEST_TIMEOUT, TimeUnit.SECONDS).send();
77
78                 final int waterLevel = localService.findWaterLevel(response.getContentAsString(), area);
79                 logger.debug("Got water level {}", waterLevel);
80                 return waterLevel;
81             } else {
82                 logger.debug("Service, region is null");
83                 return null;
84             }
85         } catch (InterruptedException | ExecutionException | TimeoutException e) {
86             logger.debug("Error when attempting to get Water Level {}", e.getMessage());
87             return null;
88         }
89     }
90 }