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