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