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.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 org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.openhab.binding.magentatv.internal.network.MagentaTVOAuth;
25 import org.openhab.core.io.console.Console;
26 import org.openhab.core.io.console.extensions.AbstractConsoleCommandExtension;
27 import org.openhab.core.io.console.extensions.ConsoleCommandExtension;
28 import org.openhab.core.io.net.http.HttpClientFactory;
29 import org.osgi.service.component.annotations.Activate;
30 import org.osgi.service.component.annotations.Component;
31 import org.osgi.service.component.annotations.Reference;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * Console commands for interacting with the MagentaTV binding
38 * @author Markus Michels - Initial contribution
41 @Component(service = ConsoleCommandExtension.class)
42 public class MagentaTVConsoleHandler extends AbstractConsoleCommandExtension {
44 private static final String CMD_LOGIN = "login";
46 private final Logger logger = LoggerFactory.getLogger(MagentaTVConsoleHandler.class);
47 private final MagentaTVOAuth oauth;
50 public MagentaTVConsoleHandler(@Reference HttpClientFactory httpClientFactory) {
51 super(BINDING_ID, "Interact with the " + BINDING_ID + " integration.");
52 oauth = new MagentaTVOAuth(httpClientFactory.getCommonHttpClient());
56 public void execute(String[] args, Console console) {
57 if (args.length > 0) {
58 String subCommand = args[0];
61 if (args.length == 1) {
62 try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
63 console.print("Login Name (email): ");
64 String username = br.readLine();
65 console.print("Password: ");
66 String pwd = br.readLine();
67 console.println("Attempting login...");
68 login(console, username, pwd);
69 } catch (IOException e) {
70 console.println(e.toString());
72 } else if (args.length == 3) {
73 login(console, args[1], args[2]);
80 console.println("Unknown command '" + subCommand + "'");
88 public List<String> getUsages() {
89 return Arrays.asList(buildCommandUsage(CMD_LOGIN + " [<email>] [<password>]",
90 "Logs into the account with the provided credentials and retrieves the User ID."));
93 private void login(Console console, String username, String password) {
95 logger.info("Performing OAuth for user {}", username);
96 String userId = oauth.getUserId(username, password);
97 console.println("Login successful, returned User ID is " + userId);
99 "Edit thing configuration and copy this value to the field User ID or use it as parameter userId for the textual configuration.");
100 logger.info("Login with account {} was successful, returned User ID is {}", username, userId);
101 } catch (MagentaTVException e) {
102 console.println("Login with account " + username + " failed: " + e.getMessage());
103 logger.warn("Unable to login with account {}, check credentials ({})", username, e.getMessage());