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.somfytahoma.internal.console;
15 import java.util.List;
16 import java.util.stream.Collectors;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.somfytahoma.internal.SomfyTahomaBindingConstants;
21 import org.openhab.binding.somfytahoma.internal.handler.SomfyTahomaBridgeHandler;
22 import org.openhab.binding.somfytahoma.internal.model.SomfyTahomaActionGroup;
23 import org.openhab.core.io.console.Console;
24 import org.openhab.core.io.console.ConsoleCommandCompleter;
25 import org.openhab.core.io.console.StringsCompleter;
26 import org.openhab.core.io.console.extensions.AbstractConsoleCommandExtension;
27 import org.openhab.core.io.console.extensions.ConsoleCommandExtension;
28 import org.openhab.core.thing.Thing;
29 import org.openhab.core.thing.ThingRegistry;
30 import org.openhab.core.thing.ThingUID;
31 import org.openhab.core.thing.binding.ThingHandler;
32 import org.osgi.service.component.annotations.Activate;
33 import org.osgi.service.component.annotations.Component;
34 import org.osgi.service.component.annotations.Reference;
37 * The {@link SomfyTahomaCommandExtension} is responsible for handling console commands
39 * @author Laurent Garnier - Initial contribution
43 @Component(service = ConsoleCommandExtension.class)
44 public class SomfyTahomaCommandExtension extends AbstractConsoleCommandExtension implements ConsoleCommandCompleter {
46 private static final String SCENARIOS = "scenarios";
47 private static final StringsCompleter SUBCMD_COMPLETER = new StringsCompleter(List.of(SCENARIOS), false);
49 private final ThingRegistry thingRegistry;
52 public SomfyTahomaCommandExtension(final @Reference ThingRegistry thingRegistry) {
53 super("somfytahoma", "Interact with the Somfy Tahoma binding.");
54 this.thingRegistry = thingRegistry;
58 public void execute(String[] args, Console console) {
59 if (args.length == 2) {
60 Thing thing = getThing(args[0]);
61 ThingHandler thingHandler = null;
62 SomfyTahomaBridgeHandler bridgeHandler = null;
64 thingHandler = thing.getHandler();
65 if (thingHandler instanceof SomfyTahomaBridgeHandler) {
66 bridgeHandler = (SomfyTahomaBridgeHandler) thingHandler;
70 console.println("Bad thing id '" + args[0] + "'");
72 } else if (thingHandler == null) {
73 console.println("No handler initialized for the thingUID '" + args[0] + "'");
75 } else if (bridgeHandler == null) {
76 console.println("'" + args[0] + "' is not a Somfy Tahoma bridgeUID");
78 } else if (args[1].equals(SCENARIOS)) {
79 for (SomfyTahomaActionGroup actionGroup : bridgeHandler.listActionGroups()) {
80 console.println("Id is \"" + actionGroup.getOid() + "\" for the scenario \""
81 + actionGroup.getLabel() + "\"");
92 public List<String> getUsages() {
93 return List.of(buildCommandUsage("<bridgeUID> " + SCENARIOS, "list all the scenarios with their id"));
97 public @Nullable ConsoleCommandCompleter getCompleter() {
102 public boolean complete(String[] args, int cursorArgumentIndex, int cursorPosition, List<String> candidates) {
103 if (cursorArgumentIndex <= 0) {
104 return new StringsCompleter(thingRegistry.getAll().stream()
105 .filter(t -> SomfyTahomaBindingConstants.THING_TYPE_BRIDGE.equals(t.getThingTypeUID()))
106 .map(t -> t.getUID().getAsString()).collect(Collectors.toList()), true)
107 .complete(args, cursorArgumentIndex, cursorPosition, candidates);
108 } else if (cursorArgumentIndex == 1) {
109 Thing thing = getThing(args[0]);
110 if (thing != null && SomfyTahomaBindingConstants.THING_TYPE_BRIDGE.equals(thing.getThingTypeUID())) {
111 return SUBCMD_COMPLETER.complete(args, cursorArgumentIndex, cursorPosition, candidates);
117 private @Nullable Thing getThing(String uid) {
120 ThingUID thingUID = new ThingUID(uid);
121 thing = thingRegistry.get(thingUID);
122 } catch (IllegalArgumentException e) {