]> git.basschouten.com Git - openhab-addons.git/blob
ef1c1aded81ca9e87065466d1c9a48c897c38522
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.lutron.internal.protocol;
14
15 import java.math.BigDecimal;
16 import java.util.regex.Matcher;
17 import java.util.regex.Pattern;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20
21 /**
22  * Holds time durations used by the Lutron protocols
23  *
24  * @author Bob Adair - Initial contribution
25  *
26  */
27 @NonNullByDefault
28 public class LutronDuration {
29     public static final int MAX_SECONDS = 360000 - 1;
30     public static final int MAX_HUNDREDTHS = 99;
31
32     private static final Pattern PATTERN_SS = Pattern.compile("^(\\d{1,2})$");
33     private static final Pattern PATTERN_SSDEC = Pattern.compile("^(\\d{1,2})\\.(\\d{2})$");
34     private static final Pattern PATTERN_MMSS = Pattern.compile("^(\\d{1,2}):(\\d{2})$");
35     private static final Pattern PATTERN_HHMMSS = Pattern.compile("^(\\d{1,2}):(\\d{2}):(\\d{2})$");
36
37     public final Integer seconds;
38     public final Integer hundredths;
39
40     /**
41      * Constructor accepting duration in seconds
42      */
43     public LutronDuration(Integer seconds) {
44         if (seconds < 0 || seconds > MAX_SECONDS) {
45             throw new IllegalArgumentException("Invalid duration");
46         }
47         this.seconds = seconds;
48         this.hundredths = 0;
49     }
50
51     /**
52      * Constructor accepting duration in seconds and hundredths of seconds
53      */
54     public LutronDuration(Integer seconds, Integer hundredths) {
55         if (seconds < 0 || seconds > MAX_SECONDS || hundredths < 0 || hundredths > MAX_HUNDREDTHS) {
56             throw new IllegalArgumentException("Invalid duration");
57         }
58         this.seconds = seconds;
59         this.hundredths = hundredths;
60     }
61
62     /**
63      * Constructor accepting duration in seconds as a BigDecimal
64      */
65     public LutronDuration(BigDecimal seconds) {
66         if (seconds.compareTo(BigDecimal.ZERO) == -1 || seconds.compareTo(new BigDecimal(MAX_SECONDS)) == 1) {
67             new IllegalArgumentException("Invalid duration");
68         }
69         this.seconds = seconds.intValue();
70         BigDecimal fractional = seconds.subtract(new BigDecimal(seconds.intValue()));
71         this.hundredths = fractional.movePointRight(2).intValue();
72     }
73
74     /**
75      * Constructor accepting duration in seconds as a Double
76      */
77     public LutronDuration(Double seconds) {
78         this(new BigDecimal(seconds).setScale(2, BigDecimal.ROUND_HALF_UP));
79     }
80
81     /**
82      * Constructor accepting duration string of the format: SS.ss, SS, MM:SS, or HH:MM:SS
83      */
84     public LutronDuration(String duration) {
85         Matcher matcherSS = PATTERN_SS.matcher(duration);
86         if (matcherSS.find()) {
87             Integer seconds = Integer.valueOf(matcherSS.group(1));
88             this.seconds = seconds;
89             this.hundredths = 0;
90             return;
91         }
92         Matcher matcherSSDec = PATTERN_SSDEC.matcher(duration);
93         if (matcherSSDec.find()) {
94             this.seconds = Integer.valueOf(matcherSSDec.group(1));
95             this.hundredths = Integer.valueOf(matcherSSDec.group(2));
96             return;
97         }
98         Matcher matcherMMSS = PATTERN_MMSS.matcher(duration);
99         if (matcherMMSS.find()) {
100             Integer minutes = Integer.valueOf(matcherMMSS.group(1));
101             Integer seconds = Integer.valueOf(matcherMMSS.group(2));
102             this.seconds = minutes * 60 + seconds;
103             this.hundredths = 0;
104             return;
105         }
106         Matcher matcherHHMMSS = PATTERN_HHMMSS.matcher(duration);
107         if (matcherHHMMSS.find()) {
108             Integer hours = Integer.valueOf(matcherHHMMSS.group(1));
109             Integer minutes = Integer.valueOf(matcherHHMMSS.group(2));
110             Integer seconds = Integer.valueOf(matcherHHMMSS.group(3));
111             this.seconds = hours * 60 * 60 + minutes * 60 + seconds;
112             this.hundredths = 0;
113             return;
114         }
115         throw new IllegalArgumentException("Invalid duration");
116     }
117
118     public String asLipString() {
119         if (seconds < 100) {
120             if (hundredths == 0) {
121                 return String.valueOf(seconds);
122             } else {
123                 return String.format("%d.%02d", seconds, hundredths);
124             }
125         } else if (seconds < 3600) {
126             return String.format("%d:%02d", seconds / 60, seconds % 60);
127         } else {
128             return String.format("%d:%02d:%02d", seconds / 3600, (seconds % 3600) / 60, (seconds % 60));
129         }
130     }
131
132     public String asLeapString() {
133         return ""; // TBD
134     }
135
136     @Override
137     public String toString() {
138         return asLipString();
139     }
140 }