]> git.basschouten.com Git - openhab-addons.git/blob
d47e100729d194a0c010b781b65257d399dd08c5
[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.nanoleaf.internal.command;
14
15 import java.util.Arrays;
16 import java.util.List;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.binding.nanoleaf.internal.NanoleafBindingConstants;
20 import org.openhab.binding.nanoleaf.internal.handler.NanoleafControllerHandler;
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  * Console commands for interacting with Nanoleaf integration
34  *
35  * @author Kai Kreuzer - Initial contribution
36  */
37 @NonNullByDefault
38 @Component(service = ConsoleCommandExtension.class)
39 public class NanoleafCommandExtension extends AbstractConsoleCommandExtension {
40
41     private static final String CMD_LAYOUT = "layout";
42     private final ThingRegistry thingRegistry;
43
44     @Activate
45     public NanoleafCommandExtension(@Reference ThingRegistry thingRegistry) {
46         super("nanoleaf", "Interact with the Nanoleaf integration.");
47         this.thingRegistry = thingRegistry;
48     }
49
50     @Override
51     public void execute(String[] args, Console console) {
52         if (args.length > 0) {
53             String subCommand = args[0];
54             switch (subCommand) {
55                 case CMD_LAYOUT:
56                     if (args.length == 1) {
57                         thingRegistry.getAll().forEach(thing -> {
58                             if (thing.getUID().getBindingId().equals(NanoleafBindingConstants.BINDING_ID)) {
59                                 ThingHandler handler = thing.getHandler();
60                                 if (handler instanceof NanoleafControllerHandler) {
61                                     NanoleafControllerHandler nanoleafControllerHandler = (NanoleafControllerHandler) handler;
62                                     String layout = nanoleafControllerHandler.getLayout();
63                                     console.println("Layout of Nanoleaf controller '" + thing.getUID().getAsString()
64                                             + "' with label '" + thing.getLabel() + "':" + System.lineSeparator());
65                                     console.println(layout);
66                                     console.println(System.lineSeparator());
67                                 }
68                             }
69                         });
70                     } else if (args.length == 2) {
71                         String uid = args[1];
72                         Thing thing = thingRegistry.get(new ThingUID(uid));
73                         if (thing != null) {
74                             ThingHandler handler = thing.getHandler();
75                             if (handler instanceof NanoleafControllerHandler) {
76                                 NanoleafControllerHandler nanoleafControllerHandler = (NanoleafControllerHandler) handler;
77                                 String layout = nanoleafControllerHandler.getLayout();
78                                 console.println(layout);
79                             } else {
80                                 console.println("Thing with UID '" + uid.toString()
81                                         + "' is not an initialized Nanoleaf controller.");
82                             }
83                         } else {
84                             console.println("Thing with UID '" + uid.toString() + "' does not exist.");
85                         }
86                     } else {
87                         printUsage(console);
88                     }
89                     break;
90
91                 default:
92                     console.println("Unknown command '" + subCommand + "'");
93                     printUsage(console);
94                     break;
95             }
96         }
97     }
98
99     @Override
100     public List<String> getUsages() {
101         return Arrays.asList(buildCommandUsage(CMD_LAYOUT + " <thingUID>", "Prints the panel layout on the console."));
102     }
103 }