]> git.basschouten.com Git - openhab-addons.git/blob
3e8880dc047af8a2cfe4a9ad00c00813c719ea9c
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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     @NonNullByDefault
49     private static class ParamsLogin {
50         @SuppressWarnings("unused")
51         private String password = VeluxBindingConstants.UNKNOWN;
52     }
53
54     /**
55      * Bridge I/O Request message used by {@link JsonVeluxBridge}
56      * for serializing.
57      * <P>
58      * Resulting JSON:
59      *
60      * <pre>
61      * {"action":"login","params":{"password":"PASSWORD"}}
62      * </pre>
63      */
64     @NonNullByDefault
65     private static class Request {
66
67         @SuppressWarnings("unused")
68         private final String action = "login";
69         private ParamsLogin params;
70
71         public Request() {
72             this.params = new ParamsLogin();
73         }
74     }
75
76     /**
77      * Bridge I/O Response message used by {@link JsonVeluxBridge} for deserializing with including component access
78      * methods
79      * <P>
80      * <B>Expected JSON (sample):</B>
81      *
82      * <pre>
83      * '{"token": "PHPnfLda71xfGlxoYEOTGQ==", "result": true, "deviceStatus": "IDLE", "data": {}, "errors": [] }'
84      * </pre>
85      */
86     @NonNullByDefault
87     private static class Response {
88         private String token = VeluxBindingConstants.UNKNOWN;
89         private boolean result;
90         private String deviceStatus = VeluxBindingConstants.UNKNOWN;
91         @SuppressWarnings("unused")
92         private @Nullable Object data;
93         private String[] errors = {};
94
95         public String getToken() {
96             return token;
97         }
98
99         public boolean getResult() {
100             return result;
101         }
102     }
103
104     /*
105      * Constructor Method
106      */
107
108     public JClogin() {
109         logger.trace("JClogin(constructor) called.");
110     }
111
112     /*
113      * Methods required for interface {@link BridgeCommunicationProtocol}.
114      */
115
116     @Override
117     public String name() {
118         return DESCRIPTION;
119     }
120
121     @Override
122     public String getURL() {
123         return URL;
124     }
125
126     @Override
127     public Object getObjectOfRequest() {
128         return request;
129     }
130
131     @Override
132     public Class<Response> getClassOfResponse() {
133         return Response.class;
134     }
135
136     @Override
137     public void setResponse(Object thisResponse) {
138         response = (Response) thisResponse;
139     }
140
141     @Override
142     public boolean isCommunicationSuccessful() {
143         return response.getResult();
144     }
145
146     @Override
147     public String getDeviceStatus() {
148         return response.deviceStatus;
149     }
150
151     @Override
152     public String[] getErrors() {
153         return response.errors;
154     }
155
156     /*
157      * Methods in addition to interface {@link BridgeCommunicationProtocol}.
158      */
159
160     @Override
161     public void setPassword(String thisPassword) {
162         logger.trace("setPassword() called.");
163         request.params.password = thisPassword;
164     }
165
166     @Override
167     public String getAuthToken() {
168         return response.getToken();
169     }
170 }