]> git.basschouten.com Git - openhab-addons.git/blob
00b389815c70e3da196e9b425cc2a20204dae349
[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.io.neeo.internal.models;
14
15 import java.util.Objects;
16
17 import org.apache.commons.lang.StringUtils;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.core.items.Item;
20 import org.openhab.core.library.types.PercentType;
21 import org.openhab.core.types.StateDescription;
22
23 /**
24  * The model representing a Neeo Channel Range used for sliders to specify the min/max and unit (serialize/deserialize
25  * json use only).
26  *
27  * Note that the unit is just a label that is put on the slider itself for the user
28  *
29  * @author Tim Roberts - Initial Contribution
30  */
31 @NonNullByDefault
32 public class NeeoDeviceChannelRange {
33     /** The range unit representing a number */
34     private static final String UNIT_NUMBER = "";
35
36     /** The range unit representing a percentage */
37     private static final String UNIT_PERCENT = "%";
38
39     /**
40      * The default range with a minimum of 0, maximum of 100 and a unit of {@link #UNIT_PERCENT}
41      */
42     static final NeeoDeviceChannelRange DEFAULT = new NeeoDeviceChannelRange(0, 100, UNIT_PERCENT);
43
44     /** The minimum value for the range */
45     private final int minValue;
46
47     /** The maximum value for the range */
48     private final int maxValue;
49
50     /** The unit of the range */
51     private final String unit;
52
53     /**
54      * Create the channel range from the given values
55      *
56      * @param minValue the miminmum value
57      * @param maxValue the maximum value
58      * @param unit a unit for the range. Defaults to {@link #UNIT_NUMBER} if null or empty
59      */
60     public NeeoDeviceChannelRange(int minValue, int maxValue, String unit) {
61         if (minValue > maxValue) {
62             throw new IllegalArgumentException(
63                     "maxValue (" + maxValue + ") is smaller than minValue (" + minValue + ")");
64         }
65         this.minValue = minValue;
66         this.maxValue = maxValue;
67         this.unit = StringUtils.isEmpty(unit) ? UNIT_NUMBER : unit;
68     }
69
70     /**
71      * Returns the minimum value for the range
72      *
73      * @return the minimum value for the range
74      */
75     public int getMinValue() {
76         return minValue;
77     }
78
79     /**
80      * Returns the maximum value for the range
81      *
82      * @return the maximum value for the range
83      */
84     public int getMaxValue() {
85         return maxValue;
86     }
87
88     /**
89      * Returns the range unit
90      *
91      * @return a non-null, non-empty range unit
92      */
93     public String getUnit() {
94         return unit;
95     }
96
97     /**
98      * Helper function to create a {@link NeeoDeviceChannelRange} from a given {@link Item}
99      *
100      * @param item a non-null item
101      * @return a non-null {@link NeeoDeviceChannelRange} representing the {@link Item}
102      */
103     public static NeeoDeviceChannelRange from(Item item) {
104         Objects.requireNonNull(item, "item cannot be null");
105
106         final boolean supportsPercent = item.getAcceptedDataTypes().contains(PercentType.class);
107
108         final StateDescription sd = item.getStateDescription();
109         int min = 0, max = 100;
110
111         if (sd != null && sd.getMinimum() != null) {
112             min = sd.getMinimum().intValue();
113         }
114
115         if (sd != null && sd.getMaximum() != null) {
116             max = sd.getMaximum().intValue();
117         }
118
119         if (max < min) {
120             final int tmp = max;
121             max = min;
122             min = tmp;
123         }
124
125         return new NeeoDeviceChannelRange(min, max, supportsPercent ? UNIT_PERCENT : UNIT_NUMBER);
126     }
127
128     @Override
129     public String toString() {
130         return "NeeoDeviceChannelRange [minValue=" + minValue + ", maxValue=" + maxValue + ", unit=" + unit + "]";
131     }
132 }