2 * Copyright (c) 2010-2020 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.magentatv.internal;
15 import static org.openhab.binding.magentatv.internal.MagentaTVBindingConstants.BINDING_ID;
17 import java.io.BufferedReader;
18 import java.io.IOException;
19 import java.io.InputStreamReader;
20 import java.util.Arrays;
21 import java.util.List;
23 import javax.ws.rs.client.ClientBuilder;
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.openhab.binding.magentatv.internal.network.MagentaTVOAuth;
28 import org.openhab.core.io.console.Console;
29 import org.openhab.core.io.console.extensions.AbstractConsoleCommandExtension;
30 import org.openhab.core.io.console.extensions.ConsoleCommandExtension;
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 import org.osgi.service.component.annotations.ReferenceCardinality;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 * Console commands for interacting with the MagentaTV binding
41 * @author Markus Michels - Initial contribution
44 @Component(service = ConsoleCommandExtension.class)
45 public class MagentaTVConsoleHandler extends AbstractConsoleCommandExtension {
47 private static final String CMD_LOGIN = "login";
49 private final Logger logger = LoggerFactory.getLogger(MagentaTVConsoleHandler.class);
50 private final MagentaTVOAuth oauth = new MagentaTVOAuth();
52 @Reference(cardinality = ReferenceCardinality.OPTIONAL)
53 private @Nullable ClientBuilder injectedClientBuilder;
56 public MagentaTVConsoleHandler() {
57 super(BINDING_ID, "Interact with the " + BINDING_ID + " integration.");
61 public void execute(String[] args, Console console) {
62 if (args.length > 0) {
63 String subCommand = args[0];
66 if (args.length == 1) {
67 try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
68 console.print("Login Name (email): ");
69 String username = br.readLine();
70 console.print("Password: ");
71 String pwd = br.readLine();
72 console.println("Attempting login...");
73 login(console, username, pwd);
74 } catch (IOException e) {
75 console.println(e.toString());
77 } else if (args.length == 3) {
78 login(console, args[1], args[2]);
85 console.println("Unknown command '" + subCommand + "'");
93 public List<String> getUsages() {
94 return Arrays.asList(buildCommandUsage(CMD_LOGIN + " [<email>] [<password>]",
95 "Logs into the account with the provided credentials and retrieves the User ID."));
98 private void login(Console console, String username, String password) {
100 logger.info("Performing OAuth for user {}", username);
101 String userId = oauth.getUserId(username, password);
102 console.println("Login successful, returned User ID is " + userId);
104 "Edit thing configuration and copy this value to the field User ID or use it as parameter userId for the textual configuration.");
105 logger.info("Login with account {} was successful, returned User ID is {}", username, userId);
106 } catch (MagentaTVException e) {
107 console.println("Login with account " + username + " failed: " + e.getMessage());
108 logger.warn("Unable to login with account {}, check credentials ({})", username, e.getMessage());