forked from openhab/openhab-addons
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[amazonechocontrol] improve login handling (openhab#8207)
Signed-off-by: Jan N. Klug <[email protected]> Signed-off-by: Daan Meijer <[email protected]>
- Loading branch information
1 parent
14d6a17
commit 29e2cd8
Showing
7 changed files
with
164 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
...src/main/java/org/openhab/binding/amazonechocontrol/internal/ConsoleCommandExtension.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/** | ||
* Copyright (c) 2010-2020 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.binding.amazonechocontrol.internal; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.smarthome.io.console.Console; | ||
import org.eclipse.smarthome.io.console.extensions.AbstractConsoleCommandExtension; | ||
import org.openhab.binding.amazonechocontrol.internal.handler.AccountHandler; | ||
import org.osgi.service.component.annotations.Activate; | ||
import org.osgi.service.component.annotations.Component; | ||
import org.osgi.service.component.annotations.Reference; | ||
|
||
/** | ||
* The {@link ConsoleCommandExtension} class | ||
* | ||
* @author Jan N. Klug - Initial contribution | ||
*/ | ||
@Component(service = org.eclipse.smarthome.io.console.extensions.ConsoleCommandExtension.class) | ||
@NonNullByDefault | ||
public class ConsoleCommandExtension extends AbstractConsoleCommandExtension { | ||
private static final String LIST_ACCOUNTS = "listAccounts"; | ||
private static final String RESET_ACCOUNT = "resetAccount"; | ||
|
||
private final AmazonEchoControlHandlerFactory handlerFactory; | ||
|
||
@Activate | ||
public ConsoleCommandExtension(@Reference AmazonEchoControlHandlerFactory handlerFactory) { | ||
super("amazonechocontrol", "Manage the AmazonEchoControl account"); | ||
|
||
this.handlerFactory = handlerFactory; | ||
} | ||
|
||
@Override | ||
public void execute(String[] args, Console console) { | ||
if (args.length > 0) { | ||
String command = args[0]; | ||
switch (command) { | ||
case LIST_ACCOUNTS: | ||
listAccounts(console); | ||
break; | ||
case RESET_ACCOUNT: | ||
if (args.length == 2) { | ||
resetAccount(console, args[1]); | ||
} else { | ||
console.println("Invalid use of command '" + command + "'"); | ||
printUsage(console); | ||
} | ||
break; | ||
default: | ||
console.println("Unknown command '" + command + "'"); | ||
printUsage(console); | ||
break; | ||
} | ||
} else { | ||
printUsage(console); | ||
} | ||
} | ||
|
||
private void listAccounts(Console console) { | ||
Set<AccountHandler> accountHandlers = handlerFactory.getAccountHandlers(); | ||
|
||
accountHandlers.forEach(handler -> console.println( | ||
"Thing-Id: " + handler.getThing().getUID().getId() + " ('" + handler.getThing().getLabel() + "')")); | ||
} | ||
|
||
private void resetAccount(Console console, String accountId) { | ||
Optional<AccountHandler> accountHandler = handlerFactory.getAccountHandlers().stream() | ||
.filter(handler -> handler.getThing().getUID().getId().equals(accountId)).findAny(); | ||
if (accountHandler.isPresent()) { | ||
console.println("Resetting account '" + accountId + "'"); | ||
accountHandler.get().setConnection(null); | ||
} else { | ||
console.println("Account '" + accountId + "' not found."); | ||
} | ||
} | ||
|
||
@Override | ||
public List<String> getUsages() { | ||
return Arrays.asList(buildCommandUsage(LIST_ACCOUNTS, "list all AmazonEchoControl accounts"), buildCommandUsage( | ||
RESET_ACCOUNT + " <account_id>", | ||
"resets the account connection (clears all authentication data) for the thing with the given id")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters