]> git.basschouten.com Git - openhab-addons.git/blob
b8a7651d79a615bd00c07221f6bd2104a583829f
[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.magentatv.internal;
14
15 import static org.openhab.binding.magentatv.internal.MagentaTVBindingConstants.BINDING_ID;
16
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;
22
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;
34
35 /**
36  * Console commands for interacting with the MagentaTV binding
37  *
38  * @author Markus Michels - Initial contribution
39  */
40 @NonNullByDefault
41 @Component(service = ConsoleCommandExtension.class)
42 public class MagentaTVConsoleHandler extends AbstractConsoleCommandExtension {
43
44     private static final String CMD_LOGIN = "login";
45
46     private final Logger logger = LoggerFactory.getLogger(MagentaTVConsoleHandler.class);
47     private final MagentaTVOAuth oauth;
48
49     @Activate
50     public MagentaTVConsoleHandler(@Reference HttpClientFactory httpClientFactory) {
51         super(BINDING_ID, "Interact with the " + BINDING_ID + " integration.");
52         oauth = new MagentaTVOAuth(httpClientFactory.getCommonHttpClient());
53     }
54
55     @Override
56     public void execute(String[] args, Console console) {
57         if (args.length > 0) {
58             String subCommand = args[0];
59             switch (subCommand) {
60                 case CMD_LOGIN:
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());
71                         }
72                     } else if (args.length == 3) {
73                         login(console, args[1], args[2]);
74                     } else {
75                         printUsage(console);
76                     }
77                     break;
78
79                 default:
80                     console.println("Unknown command '" + subCommand + "'");
81                     printUsage(console);
82                     break;
83             }
84         }
85     }
86
87     @Override
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."));
91     }
92
93     private void login(Console console, String username, String password) {
94         try {
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);
98             console.println(
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());
104         }
105     }
106 }