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.freeboxos.internal.console;
15 import static org.openhab.binding.freeboxos.internal.config.FreeboxOsConfiguration.APP_TOKEN;
17 import java.util.Arrays;
18 import java.util.List;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.openhab.binding.freeboxos.internal.FreeboxOsBindingConstants;
22 import org.openhab.binding.freeboxos.internal.handler.FreeboxOsHandler;
23 import org.openhab.core.io.console.Console;
24 import org.openhab.core.io.console.extensions.AbstractConsoleCommandExtension;
25 import org.openhab.core.io.console.extensions.ConsoleCommandExtension;
26 import org.openhab.core.thing.Thing;
27 import org.openhab.core.thing.ThingRegistry;
28 import org.openhab.core.thing.ThingUID;
29 import org.openhab.core.thing.binding.ThingHandler;
30 import org.osgi.service.component.annotations.Activate;
31 import org.osgi.service.component.annotations.Component;
32 import org.osgi.service.component.annotations.Reference;
35 * The {@link FreeboxOsCommandExtension} is responsible for handling console commands
37 * @author Gaƫl L'hopital - Initial contribution
41 @Component(service = ConsoleCommandExtension.class)
42 public class FreeboxOsCommandExtension extends AbstractConsoleCommandExtension {
44 private final ThingRegistry thingRegistry;
47 public FreeboxOsCommandExtension(final @Reference ThingRegistry thingRegistry) {
48 super(FreeboxOsBindingConstants.BINDING_ID, "Interact with the Freebox OS binding.");
49 this.thingRegistry = thingRegistry;
53 public void execute(String[] args, Console console) {
54 if (args.length == 2) {
57 ThingUID thingUID = new ThingUID(args[0]);
58 thing = thingRegistry.get(thingUID);
59 } catch (IllegalArgumentException e) {
62 ThingHandler thingHandler = null;
63 FreeboxOsHandler handler = null;
65 thingHandler = thing.getHandler();
66 if (thingHandler instanceof FreeboxOsHandler osHandler) {
71 console.println("Bad thing id '" + args[0] + "'");
73 } else if (thingHandler == null) {
74 console.println("No handler initialized for the thing id '" + args[0] + "'");
76 } else if (handler == null) {
77 console.println("'" + args[0] + "' is not a freebox bridge id");
82 String token = handler.getConfiguration().appToken;
83 console.println("Your application token is " + (token.isEmpty() ? "undefined" : token));
96 public List<String> getUsages() {
97 return Arrays.asList(buildCommandUsage(String.format("<bridgeUID> %s show the application token", APP_TOKEN)));