]> git.basschouten.com Git - openhab-addons.git/blob
cf6826e3e383080e1c078a8ea5039a81e0031ddb
[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.easee.internal.command.account;
14
15 import static org.openhab.binding.easee.internal.EaseeBindingConstants.*;
16
17 import java.nio.charset.StandardCharsets;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.eclipse.jetty.client.api.Request;
22 import org.eclipse.jetty.client.api.Result;
23 import org.eclipse.jetty.client.util.StringContentProvider;
24 import org.eclipse.jetty.http.HttpMethod;
25 import org.openhab.binding.easee.internal.command.AbstractCommand;
26 import org.openhab.binding.easee.internal.handler.EaseeBridgeHandler;
27
28 import com.google.gson.JsonObject;
29
30 /**
31  * implements the login to the webinterface
32  *
33  * @author Alexander Friese - initial contribution
34  */
35 @NonNullByDefault
36 public class Login extends AbstractCommand {
37
38     class LoginData {
39         final String userName;
40         final String password;
41
42         public LoginData(String userName, String password) {
43             this.userName = userName;
44             this.password = password;
45         }
46     }
47
48     private final LoginData loginData;
49
50     public Login(EaseeBridgeHandler handler) {
51         // flags do not matter as "onComplete" is overwritten in this class.
52         super(handler, RetryOnFailure.NO, ProcessFailureResponse.NO);
53         loginData = new LoginData(handler.getBridgeConfiguration().getUsername(),
54                 handler.getBridgeConfiguration().getPassword());
55     }
56
57     @Override
58     protected Request prepareRequest(Request requestToPrepare) {
59         StringContentProvider cp = new StringContentProvider(gson.toJson(loginData));
60         requestToPrepare.content(cp);
61         requestToPrepare.method(HttpMethod.POST);
62
63         return requestToPrepare;
64     }
65
66     @Override
67     protected String getURL() {
68         return LOGIN_URL;
69     }
70
71     @Override
72     public void onComplete(@Nullable Result result) {
73         String json = getContentAsString(StandardCharsets.UTF_8);
74         JsonObject jsonObject = gson.fromJson(json, JsonObject.class);
75
76         if (jsonObject != null) {
77             processResult(jsonObject);
78         }
79     }
80
81     @Override
82     protected String getChannelGroup() {
83         return CHANNEL_GROUP_NONE;
84     }
85 }