]> git.basschouten.com Git - openhab-addons.git/blob
7a092fbd9e5f687ee27a501c8b3787cfce48e6cd
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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 NapierCityCouncil} class contains the logic to get data the
24  * www.napier.govt.nz website.
25  * 
26  * Napier City Council
27  *
28  * @author Stewart Cossey - Initial contribution
29  */
30 @NonNullByDefault
31 public class NapierCityCouncil implements WaterWebService {
32     private final Logger logger = LoggerFactory.getLogger(NapierCityCouncil.class);
33
34     private static final String HOSTNAME = "https://www.napier.govt.nz";
35     private static final String REGION_NAPIER = "/services/water/water-restrictions/";
36
37     private static final String PATTERN = "\"waterstat\".*?<p>.*?at (.*?) Restrictions.*?</div>";
38     private static final Pattern REGEX = Pattern.compile(PATTERN,
39             Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
40
41     @Override
42     public String service() {
43         return "napiercitycouncil";
44     }
45
46     @Override
47     public String endpoint(final String region) {
48         switch (region.toLowerCase()) {
49             case "napier":
50                 return HOSTNAME + REGION_NAPIER;
51
52         }
53         return "";
54     }
55
56     @Override
57     public int findWaterLevel(final String data, final String area) {
58         final Matcher matches = REGEX.matcher(data);
59
60         while (matches.find()) {
61             final String level = matches.group(1);
62             logger.debug("Data Level {}", level);
63
64             switch (level.toLowerCase()) {
65                 case "no":
66                     return 0;
67
68                 case "level one":
69                     return 1;
70
71                 case "level two":
72                     return 2;
73
74                 case "level three":
75                     return 3;
76
77                 case "level four":
78                     return 4;
79             }
80
81         }
82         return -1;
83     }
84 }