]> git.basschouten.com Git - openhab-addons.git/blob
ff3371e273ce85b92eb3034fac158d618826f4d6
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.freeboxos.internal.console;
14
15 import static org.openhab.binding.freeboxos.internal.config.FreeboxOsConfiguration.APP_TOKEN;
16
17 import java.util.Arrays;
18 import java.util.List;
19
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;
33
34 /**
35  * The {@link FreeboxOsCommandExtension} is responsible for handling console commands
36  *
37  * @author GaĆ«l L'hopital - Initial contribution
38  */
39
40 @NonNullByDefault
41 @Component(service = ConsoleCommandExtension.class)
42 public class FreeboxOsCommandExtension extends AbstractConsoleCommandExtension {
43
44     private final ThingRegistry thingRegistry;
45
46     @Activate
47     public FreeboxOsCommandExtension(final @Reference ThingRegistry thingRegistry) {
48         super(FreeboxOsBindingConstants.BINDING_ID, "Interact with the Freebox OS binding.");
49         this.thingRegistry = thingRegistry;
50     }
51
52     @Override
53     public void execute(String[] args, Console console) {
54         if (args.length == 2) {
55             Thing thing = null;
56             try {
57                 ThingUID thingUID = new ThingUID(args[0]);
58                 thing = thingRegistry.get(thingUID);
59             } catch (IllegalArgumentException e) {
60                 thing = null;
61             }
62             ThingHandler thingHandler = null;
63             FreeboxOsHandler handler = null;
64             if (thing != null) {
65                 thingHandler = thing.getHandler();
66                 if (thingHandler instanceof FreeboxOsHandler osHandler) {
67                     handler = osHandler;
68                 }
69             }
70             if (thing == null) {
71                 console.println("Bad thing id '" + args[0] + "'");
72                 printUsage(console);
73             } else if (thingHandler == null) {
74                 console.println("No handler initialized for the thing id '" + args[0] + "'");
75                 printUsage(console);
76             } else if (handler == null) {
77                 console.println("'" + args[0] + "' is not a freebox bridge id");
78                 printUsage(console);
79             } else {
80                 switch (args[1]) {
81                     case APP_TOKEN:
82                         String token = handler.getConfiguration().appToken;
83                         console.println("Your application token is " + (token.isEmpty() ? "undefined" : token));
84                         break;
85                     default:
86                         printUsage(console);
87                         break;
88                 }
89             }
90         } else {
91             printUsage(console);
92         }
93     }
94
95     @Override
96     public List<String> getUsages() {
97         return Arrays.asList(buildCommandUsage(String.format("<bridgeUID> %s show the application token", APP_TOKEN)));
98     }
99 }