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