]> git.basschouten.com Git - openhab-addons.git/blob
dd3751fca53b0cc81668eede3c3ca7d38d4b5e99
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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 javax.ws.rs.client.ClientBuilder;
24
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;
37
38 /**
39  * Console commands for interacting with the MagentaTV binding
40  *
41  * @author Markus Michels - Initial contribution
42  */
43 @NonNullByDefault
44 @Component(service = ConsoleCommandExtension.class)
45 public class MagentaTVConsoleHandler extends AbstractConsoleCommandExtension {
46
47     private static final String CMD_LOGIN = "login";
48
49     private final Logger logger = LoggerFactory.getLogger(MagentaTVConsoleHandler.class);
50     private final MagentaTVOAuth oauth = new MagentaTVOAuth();
51
52     @Reference(cardinality = ReferenceCardinality.OPTIONAL)
53     private @Nullable ClientBuilder injectedClientBuilder;
54
55     @Activate
56     public MagentaTVConsoleHandler() {
57         super(BINDING_ID, "Interact with the " + BINDING_ID + " integration.");
58     }
59
60     @Override
61     public void execute(String[] args, Console console) {
62         if (args.length > 0) {
63             String subCommand = args[0];
64             switch (subCommand) {
65                 case CMD_LOGIN:
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());
76                         }
77                     } else if (args.length == 3) {
78                         login(console, args[1], args[2]);
79                     } else {
80                         printUsage(console);
81                     }
82                     break;
83
84                 default:
85                     console.println("Unknown command '" + subCommand + "'");
86                     printUsage(console);
87                     break;
88             }
89         }
90     }
91
92     @Override
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."));
96     }
97
98     private void login(Console console, String username, String password) {
99         try {
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);
103             console.println(
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());
109         }
110     }
111 }