]> git.basschouten.com Git - openhab-addons.git/blob
ce5aef384cb494fa0959b92bc226caf0f58db4fe
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.binding.hue.internal.handler.HueBridgeHandler;
20 import org.openhab.binding.hue.internal.handler.HueGroupHandler;
21 import org.openhab.core.io.console.Console;
22 import org.openhab.core.io.console.extensions.AbstractConsoleCommandExtension;
23 import org.openhab.core.io.console.extensions.ConsoleCommandExtension;
24 import org.openhab.core.thing.Thing;
25 import org.openhab.core.thing.ThingRegistry;
26 import org.openhab.core.thing.ThingUID;
27 import org.openhab.core.thing.binding.ThingHandler;
28 import org.osgi.service.component.annotations.Activate;
29 import org.osgi.service.component.annotations.Component;
30 import org.osgi.service.component.annotations.Reference;
31
32 /**
33  * The {@link HueCommandExtension} is responsible for handling console commands
34  *
35  * @author Laurent Garnier - Initial contribution
36  */
37
38 @NonNullByDefault
39 @Component(service = ConsoleCommandExtension.class)
40 public class HueCommandExtension extends AbstractConsoleCommandExtension {
41
42     private static final String USER_NAME = "username";
43     private static final String SCENES = "scenes";
44
45     private final ThingRegistry thingRegistry;
46
47     @Activate
48     public HueCommandExtension(final @Reference ThingRegistry thingRegistry) {
49         super("hue", "Interact with the hue binding.");
50         this.thingRegistry = thingRegistry;
51     }
52
53     @Override
54     public void execute(String[] args, Console console) {
55         if (args.length == 2) {
56             Thing thing = null;
57             try {
58                 ThingUID thingUID = new ThingUID(args[0]);
59                 thing = thingRegistry.get(thingUID);
60             } catch (IllegalArgumentException e) {
61                 thing = null;
62             }
63             ThingHandler thingHandler = null;
64             HueBridgeHandler bridgeHandler = null;
65             HueGroupHandler groupHandler = null;
66             if (thing != null) {
67                 thingHandler = thing.getHandler();
68                 if (thingHandler instanceof HueBridgeHandler) {
69                     bridgeHandler = (HueBridgeHandler) thingHandler;
70                 } else if (thingHandler instanceof HueGroupHandler) {
71                     groupHandler = (HueGroupHandler) thingHandler;
72                 }
73             }
74             if (thing == null) {
75                 console.println("Bad thing id '" + args[0] + "'");
76                 printUsage(console);
77             } else if (thingHandler == null) {
78                 console.println("No handler initialized for the thingUID '" + args[0] + "'");
79                 printUsage(console);
80             } else if (bridgeHandler == null && groupHandler == null) {
81                 console.println("'" + args[0] + "' is neither a Hue bridgeUID nor a Hue groupThingUID");
82                 printUsage(console);
83             } else {
84                 switch (args[1]) {
85                     case USER_NAME:
86                         if (bridgeHandler != null) {
87                             String userName = bridgeHandler.getUserName();
88                             console.println("Your user name is " + (userName != null ? userName : "undefined"));
89                         } else {
90                             console.println("'" + args[0] + "' is not a Hue bridgeUID");
91                             printUsage(console);
92                         }
93                         break;
94                     case SCENES:
95                         if (bridgeHandler != null) {
96                             bridgeHandler.listScenesForConsole().forEach(console::println);
97                         } else if (groupHandler != null) {
98                             groupHandler.listScenesForConsole().forEach(console::println);
99                         }
100                         break;
101                     default:
102                         printUsage(console);
103                         break;
104                 }
105             }
106         } else {
107             printUsage(console);
108         }
109     }
110
111     @Override
112     public List<String> getUsages() {
113         return Arrays.asList(new String[] { buildCommandUsage("<bridgeUID> " + USER_NAME, "show the user name"),
114                 buildCommandUsage("<bridgeUID> " + SCENES, "list all the scenes with their id"),
115                 buildCommandUsage("<groupThingUID> " + SCENES, "list all the scenes from this group with their id") });
116     }
117 }