2 * Copyright (c) 2010-2020 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.lutron.internal.protocol;
15 import java.math.BigDecimal;
16 import java.util.regex.Matcher;
17 import java.util.regex.Pattern;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
22 * Holds time durations used by the Lutron protocols
24 * @author Bob Adair - Initial contribution
28 public class LutronDuration {
29 public static final int MAX_SECONDS = 360000 - 1;
30 public static final int MAX_HUNDREDTHS = 99;
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})$");
37 public final Integer seconds;
38 public final Integer hundredths;
41 * Constructor accepting duration in seconds
43 public LutronDuration(Integer seconds) {
44 if (seconds < 0 || seconds > MAX_SECONDS) {
45 throw new IllegalArgumentException("Invalid duration");
47 this.seconds = seconds;
52 * Constructor accepting duration in seconds and hundredths of seconds
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");
58 this.seconds = seconds;
59 this.hundredths = hundredths;
63 * Constructor accepting duration in seconds as a BigDecimal
65 public LutronDuration(BigDecimal seconds) {
66 if (seconds.compareTo(BigDecimal.ZERO) == -1 || seconds.compareTo(new BigDecimal(MAX_SECONDS)) == 1) {
67 new IllegalArgumentException("Invalid duration");
69 this.seconds = seconds.intValue();
70 BigDecimal fractional = seconds.subtract(new BigDecimal(seconds.intValue()));
71 this.hundredths = fractional.movePointRight(2).intValue();
75 * Constructor accepting duration in seconds as a Double
77 public LutronDuration(Double seconds) {
78 this(new BigDecimal(seconds).setScale(2, BigDecimal.ROUND_HALF_UP));
82 * Constructor accepting duration string of the format: SS.ss, SS, MM:SS, or HH:MM:SS
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;
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));
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;
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;
115 throw new IllegalArgumentException("Invalid duration");
118 public String asLipString() {
120 if (hundredths == 0) {
121 return String.valueOf(seconds);
123 return String.format("%d.%02d", seconds, hundredths);
125 } else if (seconds < 3600) {
126 return String.format("%d:%02d", seconds / 60, seconds % 60);
128 return String.format("%d:%02d:%02d", seconds / 3600, (seconds % 3600) / 60, (seconds % 60));
132 public String asLeapString() {
137 public String toString() {
138 return asLipString();