]> git.basschouten.com Git - openhab-addons.git/blob
d99f317c98e4012785a9d09388b13b8a9ab1c5b6
[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.boschindego.internal.console;
14
15 import java.util.Arrays;
16 import java.util.List;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.boschindego.internal.BoschIndegoBindingConstants;
21 import org.openhab.binding.boschindego.internal.exceptions.IndegoAuthenticationException;
22 import org.openhab.binding.boschindego.internal.handler.BoschAccountHandler;
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.binding.ThingHandler;
31 import org.osgi.service.component.annotations.Activate;
32 import org.osgi.service.component.annotations.Component;
33 import org.osgi.service.component.annotations.Reference;
34
35 /**
36  * The {@link BoschIndegoCommandExtension} is responsible for handling console commands
37  *
38  * @author Jacob Laursen - Initial contribution
39  */
40 @NonNullByDefault
41 @Component(service = ConsoleCommandExtension.class)
42 public class BoschIndegoCommandExtension extends AbstractConsoleCommandExtension implements ConsoleCommandCompleter {
43
44     private static final String AUTHORIZE = "authorize";
45     private static final StringsCompleter SUBCMD_COMPLETER = new StringsCompleter(List.of(AUTHORIZE), false);
46
47     private final ThingRegistry thingRegistry;
48
49     @Activate
50     public BoschIndegoCommandExtension(final @Reference ThingRegistry thingRegistry) {
51         super(BoschIndegoBindingConstants.BINDING_ID, "Interact with the Bosch Indego binding.");
52         this.thingRegistry = thingRegistry;
53     }
54
55     @Override
56     public void execute(String[] args, Console console) {
57         if (args.length != 2 || !AUTHORIZE.equals(args[0])) {
58             printUsage(console);
59             return;
60         }
61
62         for (Thing thing : thingRegistry.getAll()) {
63             ThingHandler thingHandler = thing.getHandler();
64             if (thingHandler instanceof BoschAccountHandler accountHandler) {
65                 try {
66                     accountHandler.authorize(args[1]);
67                 } catch (IndegoAuthenticationException e) {
68                     console.println("Authorization error: " + e.getMessage());
69                 }
70             }
71         }
72     }
73
74     @Override
75     public List<String> getUsages() {
76         return Arrays.asList(buildCommandUsage(AUTHORIZE, "authorize by authorization code"));
77     }
78
79     @Override
80     public @Nullable ConsoleCommandCompleter getCompleter() {
81         return this;
82     }
83
84     @Override
85     public boolean complete(String[] args, int cursorArgumentIndex, int cursorPosition, List<String> candidates) {
86         if (cursorArgumentIndex <= 0) {
87             return SUBCMD_COMPLETER.complete(args, cursorArgumentIndex, cursorPosition, candidates);
88         }
89         return false;
90     }
91 }