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