]> git.basschouten.com Git - openhab-addons.git/blob
a22c51eccb98846d768b4a65cbc5f7a269c927c3
[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.amazonechocontrol.internal;
14
15 import java.util.Arrays;
16 import java.util.List;
17 import java.util.Optional;
18 import java.util.Set;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.openhab.binding.amazonechocontrol.internal.handler.AccountHandler;
22 import org.openhab.core.io.console.Console;
23 import org.openhab.core.io.console.extensions.AbstractConsoleCommandExtension;
24 import org.osgi.service.component.annotations.Activate;
25 import org.osgi.service.component.annotations.Component;
26 import org.osgi.service.component.annotations.Reference;
27
28 /**
29  * The {@link ConsoleCommandExtension} class
30  *
31  * @author Jan N. Klug - Initial contribution
32  */
33 @Component(service = org.openhab.core.io.console.extensions.ConsoleCommandExtension.class)
34 @NonNullByDefault
35 public class ConsoleCommandExtension extends AbstractConsoleCommandExtension {
36     private static final String LIST_ACCOUNTS = "listAccounts";
37     private static final String RESET_ACCOUNT = "resetAccount";
38
39     private final AmazonEchoControlHandlerFactory handlerFactory;
40
41     @Activate
42     public ConsoleCommandExtension(@Reference AmazonEchoControlHandlerFactory handlerFactory) {
43         super("amazonechocontrol", "Manage the AmazonEchoControl account");
44
45         this.handlerFactory = handlerFactory;
46     }
47
48     @Override
49     public void execute(String[] args, Console console) {
50         if (args.length > 0) {
51             String command = args[0];
52             switch (command) {
53                 case LIST_ACCOUNTS:
54                     listAccounts(console);
55                     break;
56                 case RESET_ACCOUNT:
57                     if (args.length == 2) {
58                         resetAccount(console, args[1]);
59                     } else {
60                         console.println("Invalid use of command '" + command + "'");
61                         printUsage(console);
62                     }
63                     break;
64                 default:
65                     console.println("Unknown command '" + command + "'");
66                     printUsage(console);
67                     break;
68             }
69         } else {
70             printUsage(console);
71         }
72     }
73
74     private void listAccounts(Console console) {
75         Set<AccountHandler> accountHandlers = handlerFactory.getAccountHandlers();
76
77         accountHandlers.forEach(handler -> console.println(
78                 "Thing-Id: " + handler.getThing().getUID().getId() + " ('" + handler.getThing().getLabel() + "')"));
79     }
80
81     private void resetAccount(Console console, String accountId) {
82         Optional<AccountHandler> accountHandler = handlerFactory.getAccountHandlers().stream()
83                 .filter(handler -> handler.getThing().getUID().getId().equals(accountId)).findAny();
84         if (accountHandler.isPresent()) {
85             console.println("Resetting account '" + accountId + "'");
86             accountHandler.get().setConnection(null);
87         } else {
88             console.println("Account '" + accountId + "' not found.");
89         }
90     }
91
92     @Override
93     public List<String> getUsages() {
94         return Arrays.asList(buildCommandUsage(LIST_ACCOUNTS, "list all AmazonEchoControl accounts"), buildCommandUsage(
95                 RESET_ACCOUNT + " <account_id>",
96                 "resets the account connection (clears all authentication data) for the thing with the given id"));
97     }
98 }