2 * Copyright (c) 2010-2021 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.tesla.internal.command;
15 import java.io.BufferedReader;
16 import java.io.InputStreamReader;
17 import java.util.Arrays;
18 import java.util.List;
20 import javax.ws.rs.client.ClientBuilder;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.tesla.internal.TeslaBindingConstants;
25 import org.openhab.binding.tesla.internal.discovery.TeslaAccountDiscoveryService;
26 import org.openhab.binding.tesla.internal.handler.TeslaSSOHandler;
27 import org.openhab.core.config.discovery.DiscoveryResult;
28 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
29 import org.openhab.core.io.console.Console;
30 import org.openhab.core.io.console.extensions.AbstractConsoleCommandExtension;
31 import org.openhab.core.io.console.extensions.ConsoleCommandExtension;
32 import org.openhab.core.io.net.http.HttpClientFactory;
33 import org.openhab.core.thing.ThingUID;
34 import org.openhab.core.util.UIDUtils;
35 import org.osgi.service.component.annotations.Activate;
36 import org.osgi.service.component.annotations.Component;
37 import org.osgi.service.component.annotations.Reference;
38 import org.osgi.service.component.annotations.ReferenceCardinality;
41 * Console commands for interacting with the Tesla integration
43 * @author Nicolai Grødum - Initial contribution
44 * @author Kai Kreuzer - refactored to use Tesla account thing
47 @Component(service = ConsoleCommandExtension.class)
48 public class TeslaCommandExtension extends AbstractConsoleCommandExtension {
50 private static final String CMD_LOGIN = "login";
52 @Reference(cardinality = ReferenceCardinality.OPTIONAL)
53 private @Nullable ClientBuilder injectedClientBuilder;
55 private final TeslaAccountDiscoveryService teslaAccountDiscoveryService;
56 private final HttpClientFactory httpClientFactory;
59 public TeslaCommandExtension(@Reference TeslaAccountDiscoveryService teslaAccountDiscoveryService,
60 @Reference HttpClientFactory httpClientFactory) {
61 super("tesla", "Interact with the Tesla integration.");
62 this.teslaAccountDiscoveryService = teslaAccountDiscoveryService;
63 this.httpClientFactory = httpClientFactory;
67 public void execute(String[] args, Console console) {
68 if (args.length > 0) {
69 String subCommand = args[0];
72 if (args.length == 1) {
74 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
75 console.print("Username (email): ");
76 String username = br.readLine();
77 console.println(username);
79 console.print("Password: ");
81 String pwd = br.readLine();
83 console.println("Attempting login...");
84 login(console, username, pwd);
85 } catch (Exception e) {
86 console.println(e.toString());
88 } else if (args.length == 3) {
89 login(console, args[1], args[2]);
96 console.println("Unknown command '" + subCommand + "'");
104 public List<String> getUsages() {
105 return Arrays.asList(buildCommandUsage(CMD_LOGIN + " [<user email>] [<password>]",
106 "Authenticates the user and provides a refresh token."));
109 private void login(Console console, String username, String password) {
110 TeslaSSOHandler ssoHandler = new TeslaSSOHandler(httpClientFactory.getCommonHttpClient());
112 String refreshToken = ssoHandler.authenticate(username, password);
113 if (refreshToken != null) {
114 console.println("Refresh token: " + refreshToken);
116 ThingUID thingUID = new ThingUID(TeslaBindingConstants.THING_TYPE_ACCOUNT, UIDUtils.encode(username));
117 DiscoveryResult result = DiscoveryResultBuilder.create(thingUID).withLabel("Tesla Account")
118 .withProperty(TeslaBindingConstants.CONFIG_REFRESHTOKEN, refreshToken)
119 .withProperty(TeslaBindingConstants.CONFIG_USERNAME, username)
120 .withRepresentationProperty(TeslaBindingConstants.CONFIG_USERNAME).build();
121 teslaAccountDiscoveryService.thingDiscovered(result);
123 console.println("Failed to retrieve refresh token");