]> git.basschouten.com Git - openhab-addons.git/blob
48fd8c21df3900e68f87c8a49b0a934899a37297
[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.freebox.internal.console;
14
15 import java.util.Arrays;
16 import java.util.List;
17 import java.util.stream.Collectors;
18
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;
35
36 /**
37  * The {@link FreeboxCommandExtension} is responsible for handling console commands
38  *
39  * @author Laurent Garnier - Initial contribution
40  */
41
42 @NonNullByDefault
43 @Component(service = ConsoleCommandExtension.class)
44 public class FreeboxCommandExtension extends AbstractConsoleCommandExtension implements ConsoleCommandCompleter {
45
46     private static final String APP_TOKEN = "apptoken";
47     private static final StringsCompleter SUBCMD_COMPLETER = new StringsCompleter(List.of(APP_TOKEN), false);
48
49     private final ThingRegistry thingRegistry;
50
51     @Activate
52     public FreeboxCommandExtension(final @Reference ThingRegistry thingRegistry) {
53         super("freebox", "Interact with the freebox binding.");
54         this.thingRegistry = thingRegistry;
55     }
56
57     @Override
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;
63             if (thing != null) {
64                 thingHandler = thing.getHandler();
65                 if (thingHandler instanceof FreeboxHandler) {
66                     handler = (FreeboxHandler) thingHandler;
67                 }
68             }
69             if (thing == null) {
70                 console.println("Bad thing id '" + args[0] + "'");
71                 printUsage(console);
72             } else if (thingHandler == null) {
73                 console.println("No handler initialized for the thing id '" + args[0] + "'");
74                 printUsage(console);
75             } else if (handler == null) {
76                 console.println("'" + args[0] + "' is not a freebox bridge id");
77                 printUsage(console);
78             } else {
79                 switch (args[1]) {
80                     case APP_TOKEN:
81                         String token = handler.getAppToken();
82                         console.println("Your application token is " + (token != null ? token : "undefined"));
83                         break;
84                     default:
85                         printUsage(console);
86                         break;
87                 }
88             }
89         } else {
90             printUsage(console);
91         }
92     }
93
94     @Override
95     public List<String> getUsages() {
96         return Arrays.asList(buildCommandUsage("<bridgeUID> " + APP_TOKEN, "show the application token"));
97     }
98
99     @Override
100     public @Nullable ConsoleCommandCompleter getCompleter() {
101         return this;
102     }
103
104     @Override
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).complete(args,
110                             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);
115             }
116         }
117         return false;
118     }
119
120     private @Nullable Thing getThing(String uid) {
121         Thing thing = null;
122         try {
123             ThingUID thingUID = new ThingUID(uid);
124             thing = thingRegistry.get(thingUID);
125         } catch (IllegalArgumentException e) {
126             thing = null;
127         }
128         return thing;
129     }
130 }