2 * Copyright (c) 2010-2022 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.nanoleaf.internal.command;
15 import java.util.Arrays;
16 import java.util.List;
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;
33 * Console commands for interacting with Nanoleaf integration
35 * @author Kai Kreuzer - Initial contribution
38 @Component(service = ConsoleCommandExtension.class)
39 public class NanoleafCommandExtension extends AbstractConsoleCommandExtension {
41 private static final String CMD_LAYOUT = "layout";
42 private final ThingRegistry thingRegistry;
45 public NanoleafCommandExtension(@Reference ThingRegistry thingRegistry) {
46 super("nanoleaf", "Interact with the Nanoleaf integration.");
47 this.thingRegistry = thingRegistry;
51 public void execute(String[] args, Console console) {
52 if (args.length > 0) {
53 String subCommand = args[0];
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()) {
64 "The following Nanoleaf is NOT enabled as a Thing. Enable it first to view its layout.");
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());
74 } else if (args.length == 2) {
76 Thing thing = thingRegistry.get(new ThingUID(uid));
78 ThingHandler handler = thing.getHandler();
79 if (handler instanceof NanoleafControllerHandler) {
80 NanoleafControllerHandler nanoleafControllerHandler = (NanoleafControllerHandler) handler;
81 String layout = nanoleafControllerHandler.getLayout();
82 console.println(layout);
84 console.println("Thing with UID '" + uid.toString()
85 + "' is not an initialized Nanoleaf controller.");
88 console.println("Thing with UID '" + uid.toString() + "' does not exist.");
96 console.println("Unknown command '" + subCommand + "'");
104 public List<String> getUsages() {
105 return Arrays.asList(buildCommandUsage(CMD_LAYOUT + " <thingUID>", "Prints the panel layout on the console."));