2 * Copyright (c) 2010-2023 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.easee.internal.command.account;
15 import static org.openhab.binding.easee.internal.EaseeBindingConstants.*;
17 import java.nio.charset.StandardCharsets;
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;
28 import com.google.gson.JsonObject;
31 * implements the login to the webinterface
33 * @author Alexander Friese - initial contribution
36 public class Login extends AbstractCommand {
39 final String userName;
40 final String password;
42 public LoginData(String userName, String password) {
43 this.userName = userName;
44 this.password = password;
48 private final LoginData loginData;
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());
58 protected Request prepareRequest(Request requestToPrepare) {
59 StringContentProvider cp = new StringContentProvider(gson.toJson(loginData));
60 requestToPrepare.content(cp);
61 requestToPrepare.method(HttpMethod.POST);
63 return requestToPrepare;
67 protected String getURL() {
72 public void onComplete(@Nullable Result result) {
73 String json = getContentAsString(StandardCharsets.UTF_8);
74 JsonObject jsonObject = gson.fromJson(json, JsonObject.class);
76 if (jsonObject != null) {
77 processResult(jsonObject);
82 protected String getChannelGroup() {
83 return CHANNEL_GROUP_NONE;