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.Objects;
16 import java.util.concurrent.TimeUnit;
17 import java.util.function.Consumer;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.eclipse.jetty.client.HttpClient;
22 import org.eclipse.jetty.client.api.Request;
23 import org.eclipse.jetty.client.api.Result;
24 import org.eclipse.jetty.client.util.BufferingResponseListener;
25 import org.eclipse.jetty.client.util.StringContentProvider;
26 import org.eclipse.jetty.http.HttpHeader;
27 import org.openhab.binding.ojelectronics.internal.common.OJGSonBuilder;
28 import org.openhab.binding.ojelectronics.internal.config.OJElectronicsBridgeConfiguration;
29 import org.openhab.binding.ojelectronics.internal.models.RequestModelBase;
30 import org.openhab.binding.ojelectronics.internal.models.userprofile.PostSignInQueryModel;
31 import org.openhab.binding.ojelectronics.internal.models.userprofile.PostSignInResponseModel;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 import com.google.gson.Gson;
38 * Handles the sign in process.
40 * @author Christian Kittel - Initial Contribution
43 public class SignInService {
45 private final Gson gson = OJGSonBuilder.getGSon();
46 private final Logger logger = LoggerFactory.getLogger(SignInService.class);
47 private final HttpClient httpClient;
48 private final OJElectronicsBridgeConfiguration config;
51 * Creates a new instance of {@link SignInService}
53 * @param config configuration {@link OJElectronicsBridgeConfiguration}
54 * @param httpClient HTTP client
56 public SignInService(OJElectronicsBridgeConfiguration config, HttpClient httpClient) {
58 this.httpClient = httpClient;
64 * @param signInDone This method is called if sign in process was successful.
65 * @param connectionLosed This method is called if no connection could established.
66 * @param unauthorized This method is called if the result is unauthorized.
68 public void signIn(Consumer<String> signInDone, Consumer<@Nullable String> connectionLosed, Runnable unauthorized) {
69 logger.trace("Trying to sign in");
71 Request request = httpClient.POST(config.getRestApiUrl() + "/UserProfile/SignIn")
72 .header(HttpHeader.CONTENT_TYPE, "application/json")
73 .content(new StringContentProvider(gson.toJson(getPostSignInQueryModel())))
74 .timeout(1, TimeUnit.MINUTES);
76 request.send(new BufferingResponseListener() {
78 public void onComplete(@Nullable Result result) {
83 if (result.isFailed()) {
84 final Throwable failure = result.getFailure();
85 logger.error("Signing in failed", failure);
86 connectionLosed.accept(failure.getLocalizedMessage());
90 if (result.getResponse().getStatus() == 200) {
91 PostSignInResponseModel signInModel = Objects
92 .requireNonNull(gson.fromJson(getContentAsString(), PostSignInResponseModel.class));
93 if (signInModel.errorCode != 0 || "".equals(signInModel.sessionId)) {
97 logger.trace("Signing in successful {}", getContentAsString());
98 signInDone.accept(signInModel.sessionId);
102 connectionLosed.accept(null);
108 private RequestModelBase getPostSignInQueryModel() {
109 return new PostSignInQueryModel().withClientSWVersion(config.softwareVersion).withCustomerId(config.customerId)
110 .withUserName(config.userName).withPassword(config.password).withApiKey(config.apiKey);