]> git.basschouten.com Git - openhab-addons.git/blob
76f70c13c9e312ca4b0c689b07a25c016c208836
[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.binding.neeo.internal.handler;
14
15 import java.util.ArrayList;
16 import java.util.List;
17 import java.util.Objects;
18
19 import org.apache.commons.lang.StringUtils;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.openhab.binding.neeo.internal.NeeoConstants;
22 import org.openhab.binding.neeo.internal.UidUtils;
23 import org.openhab.binding.neeo.internal.models.NeeoDevice;
24 import org.openhab.binding.neeo.internal.models.NeeoMacro;
25 import org.openhab.binding.neeo.internal.models.NeeoRecipe;
26 import org.openhab.binding.neeo.internal.models.NeeoRecipes;
27 import org.openhab.binding.neeo.internal.models.NeeoRoom;
28 import org.openhab.binding.neeo.internal.models.NeeoScenario;
29 import org.openhab.binding.neeo.internal.models.NeeoScenarios;
30 import org.openhab.core.thing.Channel;
31 import org.openhab.core.thing.ThingUID;
32 import org.openhab.core.thing.binding.builder.ChannelBuilder;
33
34 /**
35  * Utility class for generating channels
36  *
37  * @author Tim Roberts - Initial Contribution
38  */
39 @NonNullByDefault
40 class ChannelUtils {
41     /**
42      * Generates a list of {@link Channel} s for a specific device. This implementation will generate a channel for each
43      * macro found on the device.
44      *
45      * @param thingUid a non-null thingUID
46      * @param device a non-null device
47      * @return a non-null but possibly empty list of {@link Channel} s
48      */
49     static List<Channel> generateChannels(ThingUID thingUid, NeeoDevice device) {
50         Objects.requireNonNull(thingUid, "thingUid cannot be null");
51         Objects.requireNonNull(device, "device cannot be null");
52
53         final List<Channel> channels = new ArrayList<>();
54         for (NeeoMacro macro : device.getMacros().getMacros()) {
55             final String key = macro.getKey();
56             if (key != null && StringUtils.isNotEmpty(key)) {
57                 final String label = StringUtils.isEmpty(macro.getName()) ? macro.getLabel() : macro.getName();
58                 channels.add(ChannelBuilder
59                         .create(UidUtils.createChannelUID(thingUid, NeeoConstants.DEVICE_GROUP_MACROS_ID,
60                                 NeeoConstants.DEVICE_CHANNEL_STATUS, key), "Switch")
61                         .withLabel(label == null || StringUtils.isEmpty(label) ? key : label)
62                         .withType(NeeoConstants.DEVICE_MACRO_STATUS_UID).build());
63             }
64         }
65         return channels;
66     }
67
68     /**
69      * Generates a list of {@link Channel} s for a specific room. This implementation will generate multiple channels
70      * for each scenario and recipe in the room (in addition to the current step channel)
71      *
72      * @param thingUid a non-null thingUID
73      * @param room a non-null room
74      * @return a non-null but possibly empty list of {@link Channel} s
75      */
76     static List<Channel> generateChannels(ThingUID thingUid, NeeoRoom room) {
77         Objects.requireNonNull(thingUid, "thingUid cannot be null");
78         Objects.requireNonNull(room, "room cannot be null");
79
80         final List<Channel> channels = new ArrayList<>();
81         channels.addAll(generateStateChannels(thingUid));
82         channels.addAll(generateScenarioChannels(thingUid, room.getScenarios()));
83         channels.addAll(generateRecipeChannels(thingUid, room.getRecipes()));
84         return channels;
85     }
86
87     /**
88      * Helper method to generate state channels (the current step)
89      *
90      * @param thingUid a non-null thingUID
91      * @return a non-null but possibly empty list of {@link Channel} s
92      */
93     private static List<Channel> generateStateChannels(ThingUID thingUid) {
94         Objects.requireNonNull(thingUid, "thingUid cannot be null");
95
96         final List<Channel> channels = new ArrayList<>();
97         channels.add(ChannelBuilder
98                 .create(UidUtils.createChannelUID(thingUid, NeeoConstants.ROOM_GROUP_STATE_ID,
99                         NeeoConstants.ROOM_CHANNEL_CURRENTSTEP), "String")
100                 .withType(NeeoConstants.ROOM_STATE_CURRENTSTEP_UID).build());
101         return channels;
102     }
103
104     /**
105      * Helper method to generate scenario channels
106      *
107      * @param thingUid a non-null thingUID
108      * @param scenarios the non-null scenarios
109      * @return a non-null but possibly empty list of {@link Channel} s
110      */
111     private static List<Channel> generateScenarioChannels(ThingUID thingUid, NeeoScenarios scenarios) {
112         Objects.requireNonNull(thingUid, "thingUid cannot be null");
113         Objects.requireNonNull(scenarios, "scenarios cannot be null");
114
115         final List<Channel> channels = new ArrayList<>();
116         for (NeeoScenario scenario : scenarios.getScenarios()) {
117             final String key = scenario.getKey();
118             if (key != null && StringUtils.isNotEmpty(key)) {
119                 final String scenarioLabel = StringUtils.isEmpty(scenario.getName()) ? null : scenario.getName();
120                 final String nameLabel = (scenarioLabel == null || StringUtils.isEmpty(scenarioLabel) ? key
121                         : scenarioLabel) + " Name";
122                 final String configuredLabel = (scenarioLabel == null || StringUtils.isEmpty(scenarioLabel) ? key
123                         : scenarioLabel) + " Configured";
124                 final String statusLabel = (scenarioLabel == null || StringUtils.isEmpty(scenarioLabel) ? key
125                         : scenarioLabel) + " Status";
126
127                 channels.add(ChannelBuilder
128                         .create(UidUtils.createChannelUID(thingUid, NeeoConstants.ROOM_GROUP_SCENARIO_ID,
129                                 NeeoConstants.ROOM_CHANNEL_NAME, key), "String")
130                         .withLabel(nameLabel).withType(NeeoConstants.ROOM_SCENARIO_NAME_UID).build());
131                 channels.add(ChannelBuilder
132                         .create(UidUtils.createChannelUID(thingUid, NeeoConstants.ROOM_GROUP_SCENARIO_ID,
133                                 NeeoConstants.ROOM_CHANNEL_CONFIGURED, key), "Switch")
134                         .withLabel(configuredLabel).withType(NeeoConstants.ROOM_SCENARIO_CONFIGURED_UID).build());
135                 channels.add(ChannelBuilder
136                         .create(UidUtils.createChannelUID(thingUid, NeeoConstants.ROOM_GROUP_SCENARIO_ID,
137                                 NeeoConstants.ROOM_CHANNEL_STATUS, key), "Switch")
138                         .withLabel(statusLabel).withType(NeeoConstants.ROOM_SCENARIO_STATUS_UID).build());
139             }
140         }
141         return channels;
142     }
143
144     /**
145      * Helper method to generate recipe channels
146      *
147      * @param thingUid a non-null thingUID
148      * @param recipes the non-null recipes
149      * @return a non-null but possibly empty list of {@link Channel} s
150      */
151     private static List<Channel> generateRecipeChannels(ThingUID thingUid, NeeoRecipes recipes) {
152         Objects.requireNonNull(thingUid, "thingUid cannot be null");
153         Objects.requireNonNull(recipes, "recipes cannot be null");
154
155         final List<Channel> channels = new ArrayList<>();
156         for (NeeoRecipe recipe : recipes.getRecipes()) {
157             final String key = recipe.getKey();
158             if (key != null && StringUtils.isNotEmpty(key)) {
159                 final String recipeLabel = StringUtils.isEmpty(recipe.getName()) ? null : recipe.getName();
160                 final String nameLabel = (recipeLabel == null || StringUtils.isEmpty(recipeLabel) ? key : recipeLabel)
161                         + " Name (" + recipe.getType() + ")";
162                 final String typeLabel = (recipeLabel == null || StringUtils.isEmpty(recipeLabel) ? key : recipeLabel)
163                         + " Type (" + recipe.getType() + ")";
164                 final String enabledLabel = (recipeLabel == null || StringUtils.isEmpty(recipeLabel) ? key
165                         : recipeLabel) + " Enabled (" + recipe.getType() + ")";
166                 final String statusLabel = (recipeLabel == null || StringUtils.isEmpty(recipeLabel) ? key : recipeLabel)
167                         + " Status (" + recipe.getType() + ")";
168
169                 channels.add(ChannelBuilder
170                         .create(UidUtils.createChannelUID(thingUid, NeeoConstants.ROOM_GROUP_RECIPE_ID,
171                                 NeeoConstants.ROOM_CHANNEL_NAME, key), "String")
172                         .withLabel(nameLabel).withType(NeeoConstants.ROOM_RECIPE_NAME_UID).build());
173                 channels.add(ChannelBuilder
174                         .create(UidUtils.createChannelUID(thingUid, NeeoConstants.ROOM_GROUP_RECIPE_ID,
175                                 NeeoConstants.ROOM_CHANNEL_TYPE, key), "String")
176                         .withLabel(enabledLabel).withType(NeeoConstants.ROOM_RECIPE_TYPE_UID).build());
177                 channels.add(ChannelBuilder
178                         .create(UidUtils.createChannelUID(thingUid, NeeoConstants.ROOM_GROUP_RECIPE_ID,
179                                 NeeoConstants.ROOM_CHANNEL_ENABLED, key), "Switch")
180                         .withLabel(typeLabel).withType(NeeoConstants.ROOM_RECIPE_ENABLED_UID).build());
181                 channels.add(ChannelBuilder
182                         .create(UidUtils.createChannelUID(thingUid, NeeoConstants.ROOM_GROUP_RECIPE_ID,
183                                 NeeoConstants.ROOM_CHANNEL_STATUS, key), "Switch")
184                         .withLabel(statusLabel).withType(NeeoConstants.ROOM_RECIPE_STATUS_UID).build());
185             }
186         }
187         return channels;
188     }
189 }