2 * Copyright (c) 2010-2023 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 nanoleafControllerHandler) {
61 if (!handler.getThing().isEnabled()) {
63 "The following Nanoleaf is NOT enabled as a Thing. Enable it first to view its layout.");
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());
73 } else if (args.length == 2) {
75 Thing thing = thingRegistry.get(new ThingUID(uid));
77 ThingHandler handler = thing.getHandler();
78 if (handler instanceof NanoleafControllerHandler nanoleafControllerHandler) {
79 String layout = nanoleafControllerHandler.getLayout();
80 console.println(layout);
83 "Thing with UID '" + uid + "' is not an initialized Nanoleaf controller.");
86 console.println("Thing with UID '" + uid + "' does not exist.");
94 console.println("Unknown command '" + subCommand + "'");
102 public List<String> getUsages() {
103 return Arrays.asList(buildCommandUsage(CMD_LAYOUT + " <thingUID>", "Prints the panel layout on the console."));