|
| 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.pegelonline.internal.config; |
| 14 | + |
| 15 | +import static org.openhab.binding.pegelonline.internal.PegelOnlineBindingConstants.*; |
| 16 | + |
| 17 | +import java.util.Map.Entry; |
| 18 | +import java.util.TreeMap; |
| 19 | + |
| 20 | +import org.eclipse.jdt.annotation.NonNullByDefault; |
| 21 | + |
| 22 | +/** |
| 23 | + * The {@link PegelOnlineConfiguration} class contains fields mapping thing configuration parameters. |
| 24 | + * |
| 25 | + * @author Bernd Weymann - Initial contribution |
| 26 | + */ |
| 27 | +@NonNullByDefault |
| 28 | +public class PegelOnlineConfiguration { |
| 29 | + public String uuid = UNKNOWN; |
| 30 | + public int warningLevel1 = Integer.MAX_VALUE; |
| 31 | + public int warningLevel2 = Integer.MAX_VALUE; |
| 32 | + public int warningLevel3 = Integer.MAX_VALUE; |
| 33 | + public int hq10 = Integer.MAX_VALUE; |
| 34 | + public int hq100 = Integer.MAX_VALUE; |
| 35 | + public int hqExtreme = Integer.MAX_VALUE; |
| 36 | + public int refreshInterval = 15; |
| 37 | + |
| 38 | + public boolean uuidCheck() { |
| 39 | + // https://stackoverflow.com/questions/20041051/how-to-judge-a-string-is-uuid-type |
| 40 | + return uuid.matches("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$"); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Check if configured warning levels are in ascending order |
| 45 | + * |
| 46 | + * @return true if ascending, false otherwise |
| 47 | + */ |
| 48 | + public boolean warningCheck() { |
| 49 | + TreeMap<Integer, Integer> warnMap = this.getWarnings(); |
| 50 | + Entry<Integer, Integer> currentEntry = warnMap.firstEntry(); |
| 51 | + Entry<Integer, Integer> nextEntry = warnMap.higherEntry(currentEntry.getKey()); |
| 52 | + while (nextEntry != null) { |
| 53 | + // ignore non configured values |
| 54 | + if (nextEntry.getKey() != Integer.MAX_VALUE) { |
| 55 | + if (nextEntry.getValue() < currentEntry.getValue()) { |
| 56 | + return false; |
| 57 | + } |
| 58 | + } |
| 59 | + currentEntry = nextEntry; |
| 60 | + nextEntry = warnMap.higherEntry(currentEntry.getKey()); |
| 61 | + } |
| 62 | + return true; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Calculate sorted map with level height and warning level based on configuration |
| 67 | + * |
| 68 | + * @return TreeMap with keys containing level height and values containing warning level |
| 69 | + */ |
| 70 | + public TreeMap<Integer, Integer> getWarnings() { |
| 71 | + TreeMap<Integer, Integer> warnMap = new TreeMap<>(); |
| 72 | + warnMap.put(0, NO_WARNING); |
| 73 | + warnMap.put(warningLevel1, WARN_LEVEL_1); |
| 74 | + warnMap.put(warningLevel2, WARN_LEVEL_2); |
| 75 | + warnMap.put(warningLevel3, WARN_LEVEL_3); |
| 76 | + warnMap.put(hq10, HQ10); |
| 77 | + warnMap.put(hq100, HQ100); |
| 78 | + warnMap.put(hqExtreme, HQ_EXTREME); |
| 79 | + return warnMap; |
| 80 | + } |
| 81 | +} |
0 commit comments