]> git.basschouten.com Git - openhab-addons.git/blob
33b7b535a69614171ad9364f82d7b217be3ed85e
[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 java.util.regex.Matcher;
16 import java.util.regex.Pattern;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 /**
23  * The {@link BeWaterWise} class contains the logic to get data the
24  * bewaterwise.org.nz website.
25  * 
26  * Northland Regional Council
27  *
28  * @author Stewart Cossey - Initial contribution
29  */
30 @NonNullByDefault
31 public class BeWaterWise implements WaterWebService {
32     private final Logger logger = LoggerFactory.getLogger(BeWaterWise.class);
33
34     private static final String HOSTNAME = "https://bewaterwise.org.nz";
35     private static final String REGION_FARNORTH = "/current-water-levels_far-north/";
36     private static final String REGION_WHANGAREI = "/current-water-levels_whangarei/";
37     private static final String REGION_KAIPARA = "/current-water-levels_kaipara/";
38
39     private static final String PATTERN = "vc_text_separator.*?<span>(.*?)<\\/span>.*?water-level-([0-4]).*?";
40     private static final Pattern REGEX = Pattern.compile(PATTERN,
41             Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
42
43     @Override
44     public String service() {
45         return "bewaterwise";
46     }
47
48     @Override
49     public String endpoint(final String region) {
50         switch (region.toLowerCase()) {
51             case "farnorth":
52                 return HOSTNAME + REGION_FARNORTH;
53
54             case "whangarei":
55                 return HOSTNAME + REGION_WHANGAREI;
56
57             case "kaipara":
58                 return HOSTNAME + REGION_KAIPARA;
59         }
60         return "";
61     }
62
63     @Override
64     public int findWaterLevel(final String data, final String area) {
65         final Matcher matches = REGEX.matcher(data);
66
67         while (matches.find()) {
68             final String dataArea = matches.group(1).replaceAll("\\W", "");
69             final String level = matches.group(2);
70             logger.debug("Data Area {} Level {}", dataArea, level);
71             if (dataArea.equalsIgnoreCase(area)) {
72                 return Integer.valueOf(level);
73             }
74         }
75         return -1;
76     }
77 }