]> git.basschouten.com Git - openhab-addons.git/blob
e4d713759731476675802551d7d3f07e4c7a6daa
[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.darksky.internal.config;
14
15 import java.time.Duration;
16 import java.util.regex.Pattern;
17 import java.util.regex.PatternSyntaxException;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * The {@link DarkSkyChannelConfiguration} is the class used to match the sunrise and sunset event configuration.
26  *
27  * @author Christoph Weitkamp - Initial contribution
28  */
29 @NonNullByDefault
30 public class DarkSkyChannelConfiguration {
31
32     private final Logger logger = LoggerFactory.getLogger(DarkSkyChannelConfiguration.class);
33
34     private static final Pattern HHMM_PATTERN = Pattern.compile("^([0-1][0-9]|2[0-3])(:[0-5][0-9])$");
35     private static final String TIME_SEPARATOR = ":";
36
37     private int offset;
38     private @Nullable String earliest;
39     private @Nullable String latest;
40
41     public int getOffset() {
42         return offset;
43     }
44
45     public void setOffset(int offset) {
46         this.offset = offset;
47     }
48
49     public @Nullable String getEarliest() {
50         return earliest;
51     }
52
53     public long getEarliestInMinutes() {
54         return getMinutesFromTime(earliest);
55     }
56
57     public void setEarliest(String earliest) {
58         this.earliest = earliest;
59     }
60
61     public @Nullable String getLatest() {
62         return latest;
63     }
64
65     public long getLatestInMinutes() {
66         return getMinutesFromTime(latest);
67     }
68
69     public void setLatest(String latest) {
70         this.latest = latest;
71     }
72
73     @Override
74     public String toString() {
75         return String.format("[offset=%d, earliest='%s', latest='%s']", offset, earliest, latest);
76     }
77
78     /**
79      * Parses a hh:mm string and returns the minutes.
80      */
81     private long getMinutesFromTime(@Nullable String configTime) {
82         String time = configTime;
83         if (time != null && !(time = time.trim()).isEmpty()) {
84             try {
85                 if (!HHMM_PATTERN.matcher(time).matches()) {
86                     throw new NumberFormatException();
87                 } else {
88                     String[] splittedConfigTime = time.split(TIME_SEPARATOR);
89                     if (splittedConfigTime.length < 2) {
90                         throw new NumberFormatException();
91                     }
92                     int hour = Integer.parseInt(splittedConfigTime[0]);
93                     int minutes = Integer.parseInt(splittedConfigTime[1]);
94                     return Duration.ofMinutes(minutes).plusHours(hour).toMinutes();
95                 }
96             } catch (PatternSyntaxException | NumberFormatException | ArithmeticException ex) {
97                 logger.warn("Cannot parse channel configuration '{}' to hour and minutes, use pattern hh:mm, ignoring!",
98                         time);
99             }
100         }
101         return 0;
102     }
103 }