]> git.basschouten.com Git - openhab-addons.git/blob
8b9615802c0fb8c30909d69729228de5a1f91bbc
[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.ojelectronics.internal.services;
14
15 import java.util.Objects;
16 import java.util.concurrent.TimeUnit;
17 import java.util.function.Consumer;
18
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;
34
35 import com.google.gson.Gson;
36
37 /**
38  * Handles the sign in process.
39  *
40  * @author Christian Kittel - Initial Contribution
41  */
42 @NonNullByDefault
43 public class SignInService {
44
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;
49
50     /**
51      * Creates a new instance of {@link SignInService}
52      *
53      * @param config configuration {@link OJElectronicsBridgeConfiguration}
54      * @param httpClient HTTP client
55      */
56     public SignInService(OJElectronicsBridgeConfiguration config, HttpClient httpClient) {
57         this.config = config;
58         this.httpClient = httpClient;
59     }
60
61     /**
62      * Signing in
63      *
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.
67      */
68     public void signIn(Consumer<String> signInDone, Consumer<@Nullable String> connectionLosed, Runnable unauthorized) {
69         logger.trace("Trying to sign in");
70
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);
75
76         request.send(new BufferingResponseListener() {
77             @Override
78             public void onComplete(@Nullable Result result) {
79                 if (result == null) {
80                     return;
81                 }
82
83                 if (result.isFailed()) {
84                     final Throwable failure = result.getFailure();
85                     logger.error("Signing in failed", failure);
86                     connectionLosed.accept(failure.getLocalizedMessage());
87                     return;
88                 }
89
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)) {
94                         unauthorized.run();
95                         return;
96                     }
97                     logger.trace("Signing in successful {}", getContentAsString());
98                     signInDone.accept(signInModel.sessionId);
99                     return;
100                 }
101
102                 connectionLosed.accept(null);
103                 return;
104             }
105         });
106     }
107
108     private RequestModelBase getPostSignInQueryModel() {
109         return new PostSignInQueryModel().withClientSWVersion(config.softwareVersion).withCustomerId(config.customerId)
110                 .withUserName(config.userName).withPassword(config.password).withApiKey(config.apiKey);
111     }
112 }