2 * Copyright (c) 2010-2022 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.nzwateralerts.internal.api;
15 import java.util.regex.Matcher;
16 import java.util.regex.Pattern;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
23 * The {@link SmartWater} class contains the logic to get data the
24 * SmartWater.org.nz website.
26 * Waikato Regional Council
28 * @author Stewart Cossey - Initial contribution
31 public class SmartWater implements WaterWebService {
32 private final Logger logger = LoggerFactory.getLogger(SmartWater.class);
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";
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);
44 public String service() {
49 public String endpoint(final String region) {
50 switch (region.toLowerCase()) {
52 return HOSTNAME + REGION_HAMILTON;
55 return HOSTNAME + REGION_WAIKATO;
58 return HOSTNAME + REGION_WAIPA;
64 public int findWaterLevel(final String data, final String area) {
65 final Matcher matches = REGEX.matcher(data);
67 while (matches.find()) {
68 String level = matches.group(1);
69 final String altMsgs = matches.group(2);
71 logger.debug("Data Level {}", level);
72 if ("no".equalsIgnoreCase(level) || "save".equalsIgnoreCase(altMsgs)) {
73 logger.debug("Convert Data Level to 0");
76 return Integer.valueOf(level);