]> git.basschouten.com Git - openhab-addons.git/blob
1b673aa22e345ab9f2c6d86f7dd2f1fc202bb25b
[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.tesla.internal.command;
14
15 import static org.openhab.binding.tesla.internal.TeslaBindingConstants.*;
16
17 import java.io.BufferedReader;
18 import java.io.InputStreamReader;
19 import java.util.Arrays;
20 import java.util.List;
21
22 import javax.ws.rs.client.Client;
23 import javax.ws.rs.client.ClientBuilder;
24 import javax.ws.rs.client.Entity;
25 import javax.ws.rs.client.WebTarget;
26 import javax.ws.rs.core.MediaType;
27 import javax.ws.rs.core.Response;
28
29 import org.eclipse.jdt.annotation.NonNullByDefault;
30 import org.eclipse.jdt.annotation.Nullable;
31 import org.openhab.binding.tesla.internal.TeslaBindingConstants;
32 import org.openhab.binding.tesla.internal.discovery.TeslaAccountDiscoveryService;
33 import org.openhab.binding.tesla.internal.protocol.TokenRequest;
34 import org.openhab.binding.tesla.internal.protocol.TokenRequestPassword;
35 import org.openhab.binding.tesla.internal.protocol.TokenResponse;
36 import org.openhab.core.config.discovery.DiscoveryResult;
37 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
38 import org.openhab.core.io.console.Console;
39 import org.openhab.core.io.console.extensions.AbstractConsoleCommandExtension;
40 import org.openhab.core.io.console.extensions.ConsoleCommandExtension;
41 import org.openhab.core.thing.ThingUID;
42 import org.openhab.core.util.UIDUtils;
43 import org.osgi.service.component.annotations.Activate;
44 import org.osgi.service.component.annotations.Component;
45 import org.osgi.service.component.annotations.Reference;
46 import org.osgi.service.component.annotations.ReferenceCardinality;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50 import com.google.gson.Gson;
51
52 /**
53  * Console commands for interacting with the Tesla integration
54  *
55  * @author Nicolai Grødum - Initial contribution
56  * @author Kai Kreuzer - refactored to use Tesla account thing
57  */
58 @NonNullByDefault
59 @Component(service = ConsoleCommandExtension.class)
60 public class TeslaCommandExtension extends AbstractConsoleCommandExtension {
61
62     private static final String CMD_LOGIN = "login";
63
64     private final Logger logger = LoggerFactory.getLogger(TeslaCommandExtension.class);
65
66     @Reference(cardinality = ReferenceCardinality.OPTIONAL)
67     private @Nullable ClientBuilder injectedClientBuilder;
68
69     private @Nullable WebTarget tokenTarget;
70
71     private final TeslaAccountDiscoveryService teslaAccountDiscoveryService;
72
73     @Activate
74     public TeslaCommandExtension(@Reference TeslaAccountDiscoveryService teslaAccountDiscoveryService) {
75         super("tesla", "Interact with the Tesla integration.");
76         this.teslaAccountDiscoveryService = teslaAccountDiscoveryService;
77     }
78
79     @Override
80     public void execute(String[] args, Console console) {
81         if (args.length > 0) {
82             String subCommand = args[0];
83             switch (subCommand) {
84                 case CMD_LOGIN:
85                     if (args.length == 1) {
86                         try {
87                             BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
88                             console.print("Username (email): ");
89                             String username = br.readLine();
90                             console.println(username);
91
92                             console.print("Password: ");
93
94                             String pwd = br.readLine();
95                             console.println("");
96                             console.println("Attempting login...");
97                             login(console, username, pwd);
98                         } catch (Exception e) {
99                             console.println(e.toString());
100                         }
101                     } else if (args.length == 3) {
102                         login(console, args[1], args[2]);
103                     } else {
104                         printUsage(console);
105                     }
106                     break;
107
108                 default:
109                     console.println("Unknown command '" + subCommand + "'");
110                     printUsage(console);
111                     break;
112             }
113         }
114     }
115
116     @Override
117     public List<String> getUsages() {
118         return Arrays.asList(buildCommandUsage(CMD_LOGIN + " [<user email>] [<password>]",
119                 "Authenticates the user and provides a refresh token."));
120     }
121
122     private void login(Console console, String username, String password) {
123         try {
124             Gson gson = new Gson();
125
126             TokenRequest token = new TokenRequestPassword(username, password);
127             String payLoad = gson.toJson(token);
128
129             Response response = getTokenTarget().request()
130                     .post(Entity.entity(payLoad, MediaType.APPLICATION_JSON_TYPE));
131
132             if (response != null) {
133                 if (response.getStatus() == 200 && response.hasEntity()) {
134                     String responsePayLoad = response.readEntity(String.class);
135                     TokenResponse tokenResponse = gson.fromJson(responsePayLoad.trim(), TokenResponse.class);
136                     console.println("Refresh token: " + tokenResponse.refresh_token);
137
138                     ThingUID thingUID = new ThingUID(TeslaBindingConstants.THING_TYPE_ACCOUNT,
139                             UIDUtils.encode(username));
140                     DiscoveryResult result = DiscoveryResultBuilder.create(thingUID).withLabel("Tesla Account")
141                             .withProperty(TeslaBindingConstants.CONFIG_REFRESHTOKEN, tokenResponse.refresh_token)
142                             .withProperty(TeslaBindingConstants.CONFIG_USERNAME, username)
143                             .withRepresentationProperty(TeslaBindingConstants.CONFIG_USERNAME).build();
144                     teslaAccountDiscoveryService.thingDiscovered(result);
145                 } else {
146                     console.println(
147                             "Failure: " + response.getStatus() + " " + response.getStatusInfo().getReasonPhrase());
148                 }
149             }
150         } catch (Exception e) {
151             console.println("Failed to retrieve token: " + e.getMessage());
152             logger.error("Could not get refresh token.", e);
153         }
154     }
155
156     private synchronized WebTarget getTokenTarget() {
157         WebTarget target = this.tokenTarget;
158         if (target != null) {
159             return target;
160         } else {
161             Client client;
162             try {
163                 client = ClientBuilder.newBuilder().build();
164             } catch (Exception e) {
165                 // we seem to have no Jersey, so let's hope for an injected builder by CXF
166                 if (this.injectedClientBuilder != null) {
167                     client = injectedClientBuilder.build();
168                 } else {
169                     throw new IllegalStateException("No JAX RS Client Builder available.");
170                 }
171             }
172             WebTarget teslaTarget = client.target(URI_OWNERS);
173             target = teslaTarget.path(URI_ACCESS_TOKEN);
174             this.tokenTarget = target;
175             return target;
176         }
177     }
178 }