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.amazonechocontrol.internal;
15 import java.util.Arrays;
16 import java.util.List;
17 import java.util.Optional;
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;
29 * The {@link ConsoleCommandExtension} class
31 * @author Jan N. Klug - Initial contribution
33 @Component(service = org.openhab.core.io.console.extensions.ConsoleCommandExtension.class)
35 public class ConsoleCommandExtension extends AbstractConsoleCommandExtension {
36 private static final String LIST_ACCOUNTS = "listAccounts";
37 private static final String RESET_ACCOUNT = "resetAccount";
39 private final AmazonEchoControlHandlerFactory handlerFactory;
42 public ConsoleCommandExtension(@Reference AmazonEchoControlHandlerFactory handlerFactory) {
43 super("amazonechocontrol", "Manage the AmazonEchoControl account");
45 this.handlerFactory = handlerFactory;
49 public void execute(String[] args, Console console) {
50 if (args.length > 0) {
51 String command = args[0];
54 listAccounts(console);
57 if (args.length == 2) {
58 resetAccount(console, args[1]);
60 console.println("Invalid use of command '" + command + "'");
65 console.println("Unknown command '" + command + "'");
74 private void listAccounts(Console console) {
75 Set<AccountHandler> accountHandlers = handlerFactory.getAccountHandlers();
77 accountHandlers.forEach(handler -> console.println(
78 "Thing-Id: " + handler.getThing().getUID().getId() + " ('" + handler.getThing().getLabel() + "')"));
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);
88 console.println("Account '" + accountId + "' not found.");
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"));