]> git.basschouten.com Git - openhab-addons.git/blob
88ed7b7ff2c46fcb7218b5d0919cd160ba68945f
[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 TaupoDistrictCouncil} class contains the logic to get data the
24  * www.taupodc.govt.nz website.
25  * 
26  * Taupo District Council
27  * 
28  * NOTE: This is currently a placeholder. This region needs to go into restrictions
29  * first so I can parse the correct output. -Stewart Cossey
30  *
31  * @author Stewart Cossey - Initial contribution
32  */
33 @NonNullByDefault
34 public class TaupoDistrictCouncil implements WaterWebService {
35     private final Logger logger = LoggerFactory.getLogger(TaupoDistrictCouncil.class);
36
37     private static final String HOSTNAME = "https://www.taupodc.govt.nz";
38     private static final String REGION_TAUPO = "/transport-and-water/water-conservation";
39
40     private static final String PATTERN = "div class=\"rc\".*?<tbody>.*?<strong>.*?<strong>(.*?) restrictions</strong>";
41     private static final Pattern REGEX = Pattern.compile(PATTERN,
42             Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
43
44     @Override
45     public String service() {
46         return "taupodistrictcouncil";
47     }
48
49     @Override
50     public String endpoint(final String region) {
51         switch (region.toLowerCase()) {
52             case "taupo":
53                 return HOSTNAME + REGION_TAUPO;
54
55         }
56         return "";
57     }
58
59     @Override
60     public int findWaterLevel(final String data, final String area) {
61         final Matcher matches = REGEX.matcher(data);
62
63         while (matches.find()) {
64             final String level = matches.group(1);
65             logger.debug("Data Level {}", level);
66
67             switch (level.toLowerCase()) {
68                 case "no":
69                     return 0;
70
71                 case "level one":
72                 case "level 1":
73                     return 1;
74
75                 case "level two":
76                 case "level 2":
77                     return 2;
78
79                 case "level three":
80                 case "level 3":
81                     return 3;
82
83                 case "level four":
84                 case "level 4":
85                     return 4;
86             }
87
88         }
89         return -1;
90     }
91 }