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.freebox.internal.console;
15 import java.util.Arrays;
16 import java.util.List;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.binding.freebox.internal.handler.FreeboxHandler;
20 import org.openhab.core.io.console.Console;
21 import org.openhab.core.io.console.extensions.AbstractConsoleCommandExtension;
22 import org.openhab.core.io.console.extensions.ConsoleCommandExtension;
23 import org.openhab.core.thing.Thing;
24 import org.openhab.core.thing.ThingRegistry;
25 import org.openhab.core.thing.ThingUID;
26 import org.openhab.core.thing.binding.ThingHandler;
27 import org.osgi.service.component.annotations.Activate;
28 import org.osgi.service.component.annotations.Component;
29 import org.osgi.service.component.annotations.Reference;
32 * The {@link FreeboxCommandExtension} is responsible for handling console commands
34 * @author Laurent Garnier - Initial contribution
38 @Component(service = ConsoleCommandExtension.class)
39 public class FreeboxCommandExtension extends AbstractConsoleCommandExtension {
41 private static final String APP_TOKEN = "apptoken";
43 private final ThingRegistry thingRegistry;
46 public FreeboxCommandExtension(final @Reference ThingRegistry thingRegistry) {
47 super("freebox", "Interact with the freebox binding.");
48 this.thingRegistry = thingRegistry;
52 public void execute(String[] args, Console console) {
53 if (args.length == 2) {
56 ThingUID thingUID = new ThingUID(args[0]);
57 thing = thingRegistry.get(thingUID);
58 } catch (IllegalArgumentException e) {
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"));