]> git.basschouten.com Git - openhab-addons.git/blob
340c7ee1a3da4202274b89095a1d514b6b298f11
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.velux.internal.bridge.json;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.velux.internal.VeluxBindingConstants;
18 import org.openhab.binding.velux.internal.bridge.common.Login;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 /**
23  * Specific bridge communication message supported by the Velux bridge.
24  * <P>
25  * Message semantic: Communication to authenticate itself, resulting in a return of current bridge state.
26  * <P>
27  *
28  * It defines information how to send query and receive answer through the
29  * {@link org.openhab.binding.velux.internal.bridge.VeluxBridgeProvider VeluxBridgeProvider}
30  * as described by the {@link org.openhab.binding.velux.internal.bridge.json.JsonBridgeCommunicationProtocol
31  * BridgeCommunicationProtocol}.
32  *
33  * @author Guenther Schreiner - Initial contribution.
34  */
35 @NonNullByDefault
36 class JClogin extends Login implements JsonBridgeCommunicationProtocol {
37     private final Logger logger = LoggerFactory.getLogger(JClogin.class);
38
39     private static final String URL = "/api/v1/auth";
40     private static final String DESCRIPTION = "authenticate / login";
41
42     private Request request = new Request();
43     private Response response = new Response();
44
45     /*
46      * Message Objects
47      */
48     private static class ParamsLogin {
49         @SuppressWarnings("unused")
50         private String password = VeluxBindingConstants.UNKNOWN;
51     }
52
53     /**
54      * Bridge I/O Request message used by {@link JsonVeluxBridge}
55      * for serializing.
56      * <P>
57      * Resulting JSON:
58      *
59      * <pre>
60      * {"action":"login","params":{"password":"PASSWORD"}}
61      * </pre>
62      */
63     private static class Request {
64
65         @SuppressWarnings("unused")
66         private final String action = "login";
67         private ParamsLogin params;
68
69         public Request() {
70             this.params = new ParamsLogin();
71         }
72     }
73
74     /**
75      * Bridge I/O Response message used by {@link JsonVeluxBridge} for deserializing with including component access
76      * methods
77      * <P>
78      * <B>Expected JSON (sample):</B>
79      *
80      * <pre>
81      * '{"token": "PHPnfLda71xfGlxoYEOTGQ==", "result": true, "deviceStatus": "IDLE", "data": {}, "errors": [] }'
82      * </pre>
83      */
84     private static class Response {
85         private String token = VeluxBindingConstants.UNKNOWN;
86         private boolean result;
87         private String deviceStatus = VeluxBindingConstants.UNKNOWN;
88         @SuppressWarnings("unused")
89         private @Nullable Object data;
90         private String[] errors = {};
91
92         public String getToken() {
93             return token;
94         }
95
96         public boolean getResult() {
97             return result;
98         }
99     }
100
101     /*
102      * Constructor Method
103      */
104
105     public JClogin() {
106         logger.trace("JClogin(constructor) called.");
107     }
108
109     /*
110      * Methods required for interface {@link BridgeCommunicationProtocol}.
111      */
112
113     @Override
114     public String name() {
115         return DESCRIPTION;
116     }
117
118     @Override
119     public String getURL() {
120         return URL;
121     }
122
123     @Override
124     public Object getObjectOfRequest() {
125         return request;
126     }
127
128     @Override
129     public Class<Response> getClassOfResponse() {
130         return Response.class;
131     }
132
133     @Override
134     public void setResponse(Object thisResponse) {
135         response = (Response) thisResponse;
136     }
137
138     @Override
139     public boolean isCommunicationSuccessful() {
140         return response.getResult();
141     }
142
143     @Override
144     public String getDeviceStatus() {
145         return response.deviceStatus;
146     }
147
148     @Override
149     public String[] getErrors() {
150         return response.errors;
151     }
152
153     /*
154      * Methods in addition to interface {@link BridgeCommunicationProtocol}.
155      */
156
157     @Override
158     public void setPassword(String thisPassword) {
159         logger.trace("setPassword() called.");
160         request.params.password = thisPassword;
161     }
162
163     @Override
164     public String getAuthToken() {
165         return response.getToken();
166     }
167 }