]> git.basschouten.com Git - openhab-addons.git/blob
eb9c1b60ecfb1e6adad265bdb84ed7bf9aae85cc
[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.io.neeo.internal.models;
14
15 import java.util.Arrays;
16 import java.util.List;
17 import java.util.Objects;
18 import java.util.Set;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.core.items.Item;
23 import org.openhab.core.library.types.HSBType;
24 import org.openhab.core.thing.Channel;
25 import org.openhab.core.thing.ChannelUID;
26 import org.openhab.core.thing.type.ChannelKind;
27 import org.openhab.core.thing.type.ChannelType;
28 import org.openhab.io.neeo.internal.NeeoUtil;
29
30 /**
31  * The model representing a Neeo Channel (serialize/deserialize json use only).
32  *
33  * @author Tim Roberts - Initial Contribution
34  */
35 @NonNullByDefault
36 public class NeeoDeviceChannel {
37
38     /** The channel kind */
39     private final NeeoDeviceChannelKind kind;
40
41     /** The item name */
42     private final String itemName;
43
44     /** The channel number */
45     private final int channelNbr;
46
47     /** The item subtype id (the subtype allows multiple channels for the same itemName/channelNbr) */
48     private final ItemSubType subType;
49
50     /** The capability type */
51     private final NeeoCapabilityType type;
52
53     /** The label */
54     private final String label;
55
56     /** The action/text value */
57     @Nullable
58     private final String value; // could be either a format (text label) or a value to send (button)
59
60     /** The device channel range */
61     private final NeeoDeviceChannelRange range;
62
63     /**
64      * Create a new channel based on the parms
65      *
66      * @param kind the non-null kind of channel
67      * @param itemName the non-empty item name
68      * @param channelNbr the channel number (must be >= 0)
69      * @param type the non-null type
70      * @param subType the non-null subtype
71      * @param label the non-empty label
72      * @param value the possibly null, possibly empty value
73      * @param range the possibly null range
74      */
75     public NeeoDeviceChannel(NeeoDeviceChannelKind kind, String itemName, int channelNbr, NeeoCapabilityType type,
76             ItemSubType subType, String label, @Nullable String value, @Nullable NeeoDeviceChannelRange range) {
77         Objects.requireNonNull(kind, "kind cannot be null");
78         NeeoUtil.requireNotEmpty(itemName, "itemName is required");
79         Objects.requireNonNull(type, "type is required");
80         Objects.requireNonNull(subType, "subType is required");
81         if (channelNbr < 0) {
82             throw new IllegalArgumentException("channelNbr must be >= 0");
83         }
84
85         this.kind = kind;
86         this.itemName = itemName;
87         this.channelNbr = channelNbr;
88         this.type = type;
89         this.subType = subType;
90         this.label = label;
91         this.value = value;
92         this.range = range == null ? NeeoDeviceChannelRange.DEFAULT : range;
93     }
94
95     /**
96      * Create a list of {@link NeeoDeviceChannel} from the given channel, capability type, sub type and labels
97      *
98      * @param channel a non-null channel
99      * @param capabilityType a non-null capability type
100      * @param subType a non-null sub type
101      * @param existingLabels a non-null, possibly empty set of existing labels
102      * @return a non-null, possibly empty list of device channels
103      */
104     public static List<NeeoDeviceChannel> from(Channel channel, NeeoCapabilityType capabilityType, ItemSubType subType,
105             Set<String> existingLabels) {
106         Objects.requireNonNull(channel);
107         Objects.requireNonNull(capabilityType);
108         Objects.requireNonNull(subType);
109         Objects.requireNonNull(existingLabels);
110
111         final ChannelUID uid = channel.getUID();
112         return Arrays.asList(new NeeoDeviceChannel(NeeoDeviceChannelKind.get(channel.getKind()), uid.getId(), 1,
113                 capabilityType, subType, NeeoUtil.getUniqueLabel(existingLabels, uid.getIdWithoutGroup()), "", null));
114     }
115
116     /**
117      * Create a list of {@link NeeoDeviceChannel} from the given item, channel, channel type, capability type and labels
118      *
119      * @param item a non-null item
120      * @param channel a possibly null channel
121      * @param channelType a possibly null channel type
122      * @param capabilityType a non-null capability type
123      * @param existingLabels a non-null, possibly empty set of existing labels
124      * @return a non-null, possibly empty list of device channels
125      */
126     public static List<NeeoDeviceChannel> from(Item item, @Nullable Channel channel, @Nullable ChannelType channelType,
127             NeeoCapabilityType capabilityType, Set<String> existingLabels) {
128         Objects.requireNonNull(item);
129         Objects.requireNonNull(capabilityType);
130         Objects.requireNonNull(existingLabels);
131
132         if (item.getAcceptedDataTypes().contains(HSBType.class)) {
133             return Arrays.asList(
134                     new NeeoDeviceChannel(
135                             NeeoDeviceChannelKind.get(channel == null ? ChannelKind.STATE : channel.getKind()),
136                             item.getName(), 1, capabilityType, ItemSubType.NONE,
137                             NeeoUtil.getUniqueLabel(existingLabels, NeeoUtil.getLabel(item, channelType)),
138                             NeeoUtil.getPattern(item, channelType), NeeoDeviceChannelRange.from(item)),
139                     new NeeoDeviceChannel(
140                             NeeoDeviceChannelKind.get(channel == null ? ChannelKind.STATE : channel.getKind()),
141                             item.getName(), 1, capabilityType, ItemSubType.HUE,
142                             NeeoUtil.getUniqueLabel(existingLabels, NeeoUtil.getLabel(item, channelType) + " (Hue)"),
143                             NeeoUtil.getPattern(item, channelType), NeeoDeviceChannelRange.from(item)),
144                     new NeeoDeviceChannel(
145                             NeeoDeviceChannelKind.get(channel == null ? ChannelKind.STATE : channel.getKind()),
146                             item.getName(), 1, capabilityType, ItemSubType.SATURATION,
147                             NeeoUtil.getUniqueLabel(existingLabels,
148                                     NeeoUtil.getLabel(item, channelType) + " (Saturation)"),
149                             NeeoUtil.getPattern(item, channelType), NeeoDeviceChannelRange.from(item)),
150                     new NeeoDeviceChannel(
151                             NeeoDeviceChannelKind.get(channel == null ? ChannelKind.STATE : channel.getKind()),
152                             item.getName(), 1, capabilityType, ItemSubType.BRIGHTNESS,
153                             NeeoUtil.getUniqueLabel(existingLabels,
154                                     NeeoUtil.getLabel(item, channelType) + " (Brightness)"),
155                             NeeoUtil.getPattern(item, channelType), NeeoDeviceChannelRange.from(item)));
156         } else {
157             return Arrays.asList(new NeeoDeviceChannel(
158                     NeeoDeviceChannelKind.get(channel == null ? ChannelKind.STATE : channel.getKind()), item.getName(),
159                     1, capabilityType, ItemSubType.NONE,
160                     NeeoUtil.getUniqueLabel(existingLabels, NeeoUtil.getLabel(item, channelType)),
161                     NeeoUtil.getPattern(item, channelType), NeeoDeviceChannelRange.from(item)));
162         }
163     }
164
165     /**
166      * Returns the kind of channel
167      *
168      * @return the non-null kind of channel
169      */
170     public NeeoDeviceChannelKind getKind() {
171         return kind;
172     }
173
174     /**
175      * Gets the unique item name (which may include the channel number)
176      *
177      * @return the unique item name
178      */
179     public String getUniqueItemName() {
180         if (isPowerState()) {
181             return "powerstate";
182         }
183         return itemName + "-" + subType + (channelNbr > 1 ? ("-" + channelNbr) : "");
184     }
185
186     /**
187      * Gets the channel number
188      *
189      * @return the channel number
190      */
191     public int getChannelNbr() {
192         return channelNbr;
193     }
194
195     /**
196      * Gets the {@link NeeoCapabilityType}
197      *
198      * @return the {@link NeeoCapabilityType}
199      */
200     public NeeoCapabilityType getType() {
201         return type;
202     }
203
204     /**
205      * Gets the sub type
206      *
207      * @return the sub type
208      */
209     public ItemSubType getSubType() {
210         return subType;
211     }
212
213     /**
214      * Gets the item name.
215      *
216      * @return the item name
217      */
218     public String getItemName() {
219         return itemName;
220     }
221
222     /**
223      * Gets the label.
224      *
225      * @return the label
226      */
227     public String getLabel() {
228         if (isPowerState()) {
229             return "powerstate";
230         }
231         return label;
232     }
233
234     /**
235      * Gets the value.
236      *
237      * @return the value
238      */
239     @Nullable
240     public String getValue() {
241         return value;
242     }
243
244     /**
245      * Gets the device channel range. If the range is null, the {@link NeeoDeviceChannelRange#DEFAULT} will be returned
246      *
247      * @return the possibly null {@link NeeoDeviceChannelRange}
248      */
249     public NeeoDeviceChannelRange getRange() {
250         return range;
251     }
252
253     /**
254      * Helper method to determine if the channel is a powerstate channel or not
255      *
256      * @return true if powerstate, false otherwise
257      */
258     private boolean isPowerState() {
259         return NeeoCapabilityType.SENSOR_POWER.equals(type);
260     }
261
262     @Override
263     public String toString() {
264         return "NeeoDeviceChannel [kind=" + kind + ", itemName=" + itemName + ", channelNbr=" + channelNbr + ", type="
265                 + type + ", subType=" + subType + ", label=" + label + ", value=" + value + ", range=" + range + "]";
266     }
267 }