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