]> git.basschouten.com Git - openhab-addons.git/blob
dcef64f6ba9947db1b4e3ed6cc43d32b4a890f29
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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 SmartWater} class contains the logic to get data the
24  * SmartWater.org.nz website.
25  * 
26  * Waikato Regional Council
27  *
28  * @author Stewart Cossey - Initial contribution
29  */
30 @NonNullByDefault
31 public class SmartWater implements WaterWebService {
32     private final Logger logger = LoggerFactory.getLogger(SmartWater.class);
33
34     private static final String HOSTNAME = "http://www.smartwater.org.nz";
35     private static final String REGION_HAMILTON = "/alert-levels/hamilton-city";
36     private static final String REGION_WAIKATO = "/alert-levels/waikato-district-council";
37     private static final String REGION_WAIPA = "/alert-levels/waipa-district-council";
38
39     private static final String PATTERN = "/assets/Alert-Level-Images/(?:water-alert-([1-4]|no)-large|(save)-wai-logo).svg.*?";
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 "smartwater";
46     }
47
48     @Override
49     public String endpoint(final String region) {
50         switch (region.toLowerCase()) {
51             case "hamilton":
52                 return HOSTNAME + REGION_HAMILTON;
53
54             case "waikato":
55                 return HOSTNAME + REGION_WAIKATO;
56
57             case "waipa":
58                 return HOSTNAME + REGION_WAIPA;
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             String level = matches.group(1);
69             final String altMsgs = matches.group(2);
70
71             logger.debug("Data Level {}", level);
72             if ("no".equalsIgnoreCase(level) || "save".equalsIgnoreCase(altMsgs)) {
73                 logger.debug("Convert Data Level to 0");
74                 level = "0";
75             }
76             return Integer.valueOf(level);
77         }
78         return -1;
79     }
80 }