]> git.basschouten.com Git - openhab-addons.git/blob
dbc95dd3e615a567697529127376f95dadc8d6d7
[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.function.Consumer;
16
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;
30
31 import com.google.gson.Gson;
32
33 /**
34  * Handles the sign in process.
35  *
36  * @author Christian Kittel - Initial Contribution
37  */
38 @NonNullByDefault
39 public class SignInService {
40
41     private final Gson gson = OJGSonBuilder.getGSon();
42
43     private final HttpClient httpClient;
44     private final OJElectronicsBridgeConfiguration config;
45
46     /**
47      * Creates a new instance of {@link SignInService}
48      *
49      * @param config configuration {@link OJElectronicsBridgeConfiguration}
50      * @param httpClient HTTP client
51      */
52     public SignInService(OJElectronicsBridgeConfiguration config, HttpClient httpClient) {
53         this.config = config;
54         this.httpClient = httpClient;
55     }
56
57     /**
58      * Signing in
59      *
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.
63      */
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())));
68
69         request.send(new BufferingResponseListener() {
70             @Override
71             public void onComplete(@Nullable Result result) {
72                 if (result == null || result.isFailed()) {
73                     connectionLosed.run();
74                     return;
75                 }
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("")) {
80                         unauthorized.run();
81                         return;
82                     }
83                     signInDone.accept(signInModel.sessionId);
84                     return;
85                 }
86
87                 connectionLosed.run();
88                 return;
89             }
90         });
91     }
92
93     private RequestModelBase getPostSignInQueryModel() {
94         return new PostSignInQueryModel().withClientSWVersion(config.softwareVersion).withCustomerId(config.customerId)
95                 .withUserName(config.userName).withPassword(config.password).withApiKey(config.apiKey);
96     }
97 }