]> git.basschouten.com Git - openhab-addons.git/blob
92a6235efb1a76838a1afb45faa4a587a698305d
[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.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 nanoleafControllerHandler) {
61                                     if (!handler.getThing().isEnabled()) {
62                                         console.println(
63                                                 "The following Nanoleaf is NOT enabled as a Thing. Enable it first to view its layout.");
64                                     }
65                                     String layout = nanoleafControllerHandler.getLayout();
66                                     console.println("Layout of Nanoleaf controller '" + thing.getUID().getAsString()
67                                             + "' with label '" + thing.getLabel() + "':" + System.lineSeparator());
68                                     console.println(layout);
69                                     console.println(System.lineSeparator());
70                                 }
71                             }
72                         });
73                     } else if (args.length == 2) {
74                         String uid = args[1];
75                         Thing thing = thingRegistry.get(new ThingUID(uid));
76                         if (thing != null) {
77                             ThingHandler handler = thing.getHandler();
78                             if (handler instanceof NanoleafControllerHandler nanoleafControllerHandler) {
79                                 String layout = nanoleafControllerHandler.getLayout();
80                                 console.println(layout);
81                             } else {
82                                 console.println(
83                                         "Thing with UID '" + uid + "' is not an initialized Nanoleaf controller.");
84                             }
85                         } else {
86                             console.println("Thing with UID '" + uid + "' does not exist.");
87                         }
88                     } else {
89                         printUsage(console);
90                     }
91                     break;
92
93                 default:
94                     console.println("Unknown command '" + subCommand + "'");
95                     printUsage(console);
96                     break;
97             }
98         }
99     }
100
101     @Override
102     public List<String> getUsages() {
103         return Arrays.asList(buildCommandUsage(CMD_LAYOUT + " <thingUID>", "Prints the panel layout on the console."));
104     }
105 }