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.ojelectronics.internal.services;
15 import java.util.function.Consumer;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.eclipse.jetty.client.HttpClient;
20 import org.eclipse.jetty.client.api.Request;
21 import org.eclipse.jetty.client.api.Result;
22 import org.eclipse.jetty.client.util.BufferingResponseListener;
23 import org.eclipse.jetty.client.util.StringContentProvider;
24 import org.eclipse.jetty.http.HttpHeader;
25 import org.openhab.binding.ojelectronics.internal.common.OJGSonBuilder;
26 import org.openhab.binding.ojelectronics.internal.config.OJElectronicsBridgeConfiguration;
27 import org.openhab.binding.ojelectronics.internal.models.RequestModelBase;
28 import org.openhab.binding.ojelectronics.internal.models.userprofile.PostSignInQueryModel;
29 import org.openhab.binding.ojelectronics.internal.models.userprofile.PostSignInResponseModel;
31 import com.google.gson.Gson;
34 * Handles the sign in process.
36 * @author Christian Kittel - Initial Contribution
39 public class SignInService {
41 private final Gson gson = OJGSonBuilder.getGSon();
43 private final HttpClient httpClient;
44 private final OJElectronicsBridgeConfiguration config;
47 * Creates a new instance of {@link SignInService}
49 * @param config configuration {@link OJElectronicsBridgeConfiguration}
50 * @param httpClient HTTP client
52 public SignInService(OJElectronicsBridgeConfiguration config, HttpClient httpClient) {
54 this.httpClient = httpClient;
60 * @param signInDone This method is called if sign in process was successful.
61 * @param connectionLosed This method is called if no connection could established.
62 * @param unauthorized This method is called if the result is unauthorized.
64 public void signIn(Consumer<String> signInDone, Runnable connectionLosed, Runnable unauthorized) {
65 Request request = httpClient.POST(config.apiUrl + "/UserProfile/SignIn")
66 .header(HttpHeader.CONTENT_TYPE, "application/json")
67 .content(new StringContentProvider(gson.toJson(getPostSignInQueryModel())));
69 request.send(new BufferingResponseListener() {
71 public void onComplete(@Nullable Result result) {
72 if (result == null || result.isFailed()) {
73 connectionLosed.run();
76 if (result.getResponse().getStatus() == 200) {
77 PostSignInResponseModel signInModel = gson.fromJson(getContentAsString(),
78 PostSignInResponseModel.class);
79 if (signInModel == null || signInModel.errorCode != 0 || signInModel.sessionId.equals("")) {
83 signInDone.accept(signInModel.sessionId);
87 connectionLosed.run();
93 private RequestModelBase getPostSignInQueryModel() {
94 return new PostSignInQueryModel().withClientSWVersion(config.softwareVersion).withCustomerId(config.customerId)
95 .withUserName(config.userName).withPassword(config.password).withApiKey(config.apiKey);