]> git.basschouten.com Git - openhab-addons.git/blob
7a9079ae1d6d458cc59d9443e0bbb82849d41293
[openhab-addons.git] /
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.insteon.internal.device;
14
15 import static org.openhab.binding.insteon.internal.InsteonBindingConstants.*;
16
17 import java.text.DecimalFormat;
18 import java.util.Arrays;
19 import java.util.Comparator;
20 import java.util.Map;
21 import java.util.function.Function;
22 import java.util.stream.Collectors;
23
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26
27 /**
28  * The {@link RampRate} represents a ramp rate for Insteon dimmer products
29  *
30  * @author Jeremy Setton - Initial contribution
31  */
32 @NonNullByDefault
33 public enum RampRate {
34     MIN_9(0x00, 540),
35     MIN_8(0x01, 480),
36     MIN_7(0x02, 420),
37     MIN_6(0x03, 360),
38     MIN_5(0x04, 300),
39     MIN_4_5(0x05, 270),
40     MIN_4(0x06, 240),
41     MIN_3_5(0x07, 210),
42     MIN_3(0x08, 180),
43     MIN_2_5(0x09, 150),
44     MIN_2(0x0A, 120),
45     MIN_1_5(0x0B, 90),
46     MIN_1(0x0C, 60),
47     SEC_47(0x0D, 47),
48     SEC_43(0x0E, 43),
49     SEC_38_5(0x0F, 38.5),
50     SEC_34(0x10, 34),
51     SEC_32(0x11, 32),
52     SEC_30(0x12, 30),
53     SEC_28(0x13, 28),
54     SEC_26(0x14, 26),
55     SEC_23_5(0x15, 23.5),
56     SEC_21_5(0x16, 21.5),
57     SEC_19(0x17, 19),
58     SEC_8_5(0x18, 8.5),
59     SLOW(0x19, 6.5),
60     SEC_4_5(0x1A, 4.5),
61     MEDIUM(0x1B, 2),
62     DEFAULT(0x1C, 0.5),
63     FAST(0x1D, 0.3),
64     SEC_0_2(0x1E, 0.2),
65     INSTANT(0x1F, 0.1);
66
67     private static final Map<Integer, RampRate> VALUE_MAP = Arrays.stream(values())
68             .collect(Collectors.toUnmodifiableMap(rate -> rate.value, Function.identity()));
69
70     private final int value;
71     private final double time;
72
73     private RampRate(int value, double time) {
74         this.value = value;
75         this.time = time;
76     }
77
78     public int getValue() {
79         return value;
80     }
81
82     public double getTimeInSeconds() {
83         return time;
84     }
85
86     public long getTimeInMilliseconds() {
87         return (long) (time * 1000);
88     }
89
90     @Override
91     public String toString() {
92         double time = getTimeInSeconds();
93         String unit = "s";
94         if (time >= 60) {
95             time /= 60;
96             unit = "min";
97         }
98         return new DecimalFormat("0.#").format(time) + unit;
99     }
100
101     /**
102      * Factory method for determining if a given feature type supports ramp rate
103      *
104      * @param featureType the feature type
105      * @return true if supported
106      */
107     public static boolean supportsFeatureType(String featureType) {
108         return FEATURE_TYPE_GENERIC_DIMMER.equals(featureType);
109     }
110
111     /**
112      * Factory method for getting a RampRate from a ramp rate value
113      *
114      * @param value the ramp rate value
115      * @return the ramp rate
116      */
117     public static RampRate valueOf(int value) {
118         return VALUE_MAP.getOrDefault(value, RampRate.DEFAULT);
119     }
120
121     /**
122      * Factory method for getting a RampRate from the closest ramp time
123      *
124      * @param time the ramp time
125      * @return the ramp rate
126      */
127     public static RampRate fromTime(double time) {
128         return VALUE_MAP.values().stream().min(Comparator.comparingDouble(rate -> Math.abs(rate.time - time))).get();
129     }
130
131     /**
132      * Factory method for getting a RampRate from a ramp rate string
133      *
134      * @param string the ramp rate string
135      * @return the ramp rate
136      */
137     public static @Nullable RampRate fromString(String string) {
138         try {
139             return fromTime(Double.parseDouble(string));
140         } catch (NumberFormatException e) {
141             return VALUE_MAP.values().stream().filter(rate -> rate.toString().equals(string)).findAny().orElse(null);
142         }
143     }
144 }