]> git.basschouten.com Git - openhab-addons.git/blob
424f57c52d40f68ff1440e12d8e2ee3ff692abde
[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.binding.hue.internal.console;
14
15 import java.util.Arrays;
16 import java.util.List;
17 import java.util.stream.Collectors;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.hue.internal.HueBindingConstants;
22 import org.openhab.binding.hue.internal.handler.HueBridgeHandler;
23 import org.openhab.binding.hue.internal.handler.HueGroupHandler;
24 import org.openhab.core.io.console.Console;
25 import org.openhab.core.io.console.ConsoleCommandCompleter;
26 import org.openhab.core.io.console.StringsCompleter;
27 import org.openhab.core.io.console.extensions.AbstractConsoleCommandExtension;
28 import org.openhab.core.io.console.extensions.ConsoleCommandExtension;
29 import org.openhab.core.thing.Thing;
30 import org.openhab.core.thing.ThingRegistry;
31 import org.openhab.core.thing.ThingUID;
32 import org.openhab.core.thing.binding.ThingHandler;
33 import org.osgi.service.component.annotations.Activate;
34 import org.osgi.service.component.annotations.Component;
35 import org.osgi.service.component.annotations.Reference;
36
37 /**
38  * The {@link HueCommandExtension} is responsible for handling console commands
39  *
40  * @author Laurent Garnier - Initial contribution
41  */
42
43 @NonNullByDefault
44 @Component(service = ConsoleCommandExtension.class)
45 public class HueCommandExtension extends AbstractConsoleCommandExtension implements ConsoleCommandCompleter {
46
47     private static final String USER_NAME = "username";
48     private static final String SCENES = "scenes";
49     private static final StringsCompleter SUBCMD_COMPLETER = new StringsCompleter(List.of(USER_NAME, SCENES), false);
50     private static final StringsCompleter SCENES_COMPLETER = new StringsCompleter(List.of(SCENES), false);
51
52     private final ThingRegistry thingRegistry;
53
54     @Activate
55     public HueCommandExtension(final @Reference ThingRegistry thingRegistry) {
56         super("hue", "Interact with the Hue binding.");
57         this.thingRegistry = thingRegistry;
58     }
59
60     @Override
61     public void execute(String[] args, Console console) {
62         if (args.length == 2) {
63             Thing thing = getThing(args[0]);
64             ThingHandler thingHandler = null;
65             HueBridgeHandler bridgeHandler = null;
66             HueGroupHandler groupHandler = null;
67             if (thing != null) {
68                 thingHandler = thing.getHandler();
69                 if (thingHandler instanceof HueBridgeHandler) {
70                     bridgeHandler = (HueBridgeHandler) thingHandler;
71                 } else if (thingHandler instanceof HueGroupHandler) {
72                     groupHandler = (HueGroupHandler) thingHandler;
73                 }
74             }
75             if (thing == null) {
76                 console.println("Bad thing id '" + args[0] + "'");
77                 printUsage(console);
78             } else if (thingHandler == null) {
79                 console.println("No handler initialized for the thingUID '" + args[0] + "'");
80                 printUsage(console);
81             } else if (bridgeHandler == null && groupHandler == null) {
82                 console.println("'" + args[0] + "' is neither a Hue BridgeUID nor a Hue groupThingUID");
83                 printUsage(console);
84             } else {
85                 switch (args[1]) {
86                     case USER_NAME:
87                         if (bridgeHandler != null) {
88                             String userName = bridgeHandler.getUserName();
89                             console.println("Your user name is " + (userName != null ? userName : "undefined"));
90                         } else {
91                             console.println("'" + args[0] + "' is not a Hue BridgeUID");
92                             printUsage(console);
93                         }
94                         break;
95                     case SCENES:
96                         if (bridgeHandler != null) {
97                             bridgeHandler.listScenesForConsole().forEach(console::println);
98                         } else if (groupHandler != null) {
99                             groupHandler.listScenesForConsole().forEach(console::println);
100                         }
101                         break;
102                     default:
103                         printUsage(console);
104                         break;
105                 }
106             }
107         } else {
108             printUsage(console);
109         }
110     }
111
112     @Override
113     public List<String> getUsages() {
114         return Arrays.asList(new String[] { buildCommandUsage("<bridgeUID> " + USER_NAME, "show the user name"),
115                 buildCommandUsage("<bridgeUID> " + SCENES, "list all the scenes with their id"),
116                 buildCommandUsage("<groupThingUID> " + SCENES, "list all the scenes from this group with their id") });
117     }
118
119     @Override
120     public @Nullable ConsoleCommandCompleter getCompleter() {
121         return this;
122     }
123
124     @Override
125     public boolean complete(String[] args, int cursorArgumentIndex, int cursorPosition, List<String> candidates) {
126         if (cursorArgumentIndex <= 0) {
127             return new StringsCompleter(thingRegistry.getAll().stream()
128                     .filter(t -> HueBindingConstants.THING_TYPE_BRIDGE.equals(t.getThingTypeUID())
129                             || HueBindingConstants.THING_TYPE_GROUP.equals(t.getThingTypeUID()))
130                     .map(t -> t.getUID().getAsString()).collect(Collectors.toList()), true).complete(args,
131                             cursorArgumentIndex, cursorPosition, candidates);
132         } else if (cursorArgumentIndex == 1) {
133             Thing thing = getThing(args[0]);
134             if (thing != null && HueBindingConstants.THING_TYPE_BRIDGE.equals(thing.getThingTypeUID())) {
135                 return SUBCMD_COMPLETER.complete(args, cursorArgumentIndex, cursorPosition, candidates);
136             } else if (thing != null && HueBindingConstants.THING_TYPE_GROUP.equals(thing.getThingTypeUID())) {
137                 return SCENES_COMPLETER.complete(args, cursorArgumentIndex, cursorPosition, candidates);
138             }
139         }
140         return false;
141     }
142
143     private @Nullable Thing getThing(String uid) {
144         Thing thing = null;
145         try {
146             ThingUID thingUID = new ThingUID(uid);
147             thing = thingRegistry.get(thingUID);
148         } catch (IllegalArgumentException e) {
149             thing = null;
150         }
151         return thing;
152     }
153 }