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.freebox.internal.console;
15 import java.util.Arrays;
16 import java.util.List;
17 import java.util.stream.Collectors;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.freebox.internal.FreeboxBindingConstants;
22 import org.openhab.binding.freebox.internal.handler.FreeboxHandler;
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 FreeboxCommandExtension} is responsible for handling console commands
39 * @author Laurent Garnier - Initial contribution
43 @Component(service = ConsoleCommandExtension.class)
44 public class FreeboxCommandExtension extends AbstractConsoleCommandExtension implements ConsoleCommandCompleter {
46 private static final String APP_TOKEN = "apptoken";
47 private static final StringsCompleter SUBCMD_COMPLETER = new StringsCompleter(List.of(APP_TOKEN), false);
49 private final ThingRegistry thingRegistry;
52 public FreeboxCommandExtension(final @Reference ThingRegistry thingRegistry) {
53 super("freebox", "Interact with the freebox 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 FreeboxHandler handler = null;
64 thingHandler = thing.getHandler();
65 if (thingHandler instanceof FreeboxHandler) {
66 handler = (FreeboxHandler) thingHandler;
70 console.println("Bad thing id '" + args[0] + "'");
72 } else if (thingHandler == null) {
73 console.println("No handler initialized for the thing id '" + args[0] + "'");
75 } else if (handler == null) {
76 console.println("'" + args[0] + "' is not a freebox bridge id");
81 String token = handler.getAppToken();
82 console.println("Your application token is " + (token != null ? token : "undefined"));
95 public List<String> getUsages() {
96 return Arrays.asList(buildCommandUsage("<bridgeUID> " + APP_TOKEN, "show the application token"));
100 public @Nullable ConsoleCommandCompleter getCompleter() {
105 public boolean complete(String[] args, int cursorArgumentIndex, int cursorPosition, List<String> candidates) {
106 if (cursorArgumentIndex <= 0) {
107 return new StringsCompleter(thingRegistry.getAll().stream()
108 .filter(t -> FreeboxBindingConstants.FREEBOX_BRIDGE_TYPE_SERVER.equals(t.getThingTypeUID()))
109 .map(t -> t.getUID().getAsString()).collect(Collectors.toList()), true)
110 .complete(args, cursorArgumentIndex, cursorPosition, candidates);
111 } else if (cursorArgumentIndex == 1) {
112 Thing thing = getThing(args[0]);
113 if (thing != null && FreeboxBindingConstants.FREEBOX_BRIDGE_TYPE_SERVER.equals(thing.getThingTypeUID())) {
114 return SUBCMD_COMPLETER.complete(args, cursorArgumentIndex, cursorPosition, candidates);
120 private @Nullable Thing getThing(String uid) {
123 ThingUID thingUID = new ThingUID(uid);
124 thing = thingRegistry.get(thingUID);
125 } catch (IllegalArgumentException e) {