]> git.basschouten.com Git - openhab-addons.git/blob
0831033dbf646e56f1debda1b9ef9a7bd397ee84
[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 java.util.HashMap;
16 import java.util.Map;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.binding.velux.internal.VeluxBindingConstants;
20 import org.openhab.binding.velux.internal.bridge.common.GetWLANConfig;
21 import org.openhab.binding.velux.internal.things.VeluxGwWLAN;
22
23 /**
24  * Specific bridge communication message supported by the Velux bridge.
25  * <P>
26  * Message semantic: Retrieval of WLAN configuration.
27  * <P>
28  *
29  * It defines information how to send query and receive answer through the
30  * {@link org.openhab.binding.velux.internal.bridge.VeluxBridgeProvider VeluxBridgeProvider}
31  * as described by the {@link org.openhab.binding.velux.internal.bridge.json.JsonBridgeCommunicationProtocol
32  * BridgeCommunicationProtocol}.
33  *
34  * @author Guenther Schreiner - Initial contribution.
35  */
36 @NonNullByDefault
37 class JCgetWLANConfig extends GetWLANConfig implements JsonBridgeCommunicationProtocol {
38
39     private static final String URL = "/api/v1/settings";
40     private static final String DESCRIPTION = "get WLAN configuration";
41
42     private Request request = new Request();
43     private Response response = new Response();
44
45     /*
46      * Message Objects
47      */
48
49     /**
50      * Bridge I/O Request message used by {@link org.openhab.binding.velux.internal.bridge.json.JsonVeluxBridge
51      * JsonVeluxBridge}
52      * for serializing.
53      * <P>
54      * Resulting JSON:
55      *
56      * <pre>
57      * {"action":"wifi","params":{}}
58      * </pre>
59      */
60     @NonNullByDefault
61     private static class Request {
62
63         @SuppressWarnings("unused")
64         private String action;
65
66         @SuppressWarnings("unused")
67         private Map<String, String> params;
68
69         public Request() {
70             this.action = "wifi";
71             this.params = new HashMap<>();
72         }
73     }
74
75     /**
76      * Bridge Communication Structure containing the version of the firmware.
77      * <P>
78      * Used within structure {@link JCgetWLANConfig} to describe the network connectivity of the Bridge.
79      *
80      * <PRE>
81      * {"password":"Esf56mxqFY","name":"VELUX_KLF_847C"}
82      * </PRE>
83      */
84     @NonNullByDefault
85     private static class BCWLANConfig {
86
87         private String password = VeluxBindingConstants.UNKNOWN;
88         private String name = VeluxBindingConstants.UNKNOWN;
89
90         @Override
91         public String toString() {
92             return String.format("SSID=%s,password=********", this.name);
93         }
94     }
95
96     /**
97      * Bridge I/O Response message used by {@link JsonBridgeCommunicationProtocol} for deserialization with including
98      * component access
99      * methods
100      * <P>
101      * Expected JSON (sample):
102      *
103      * <pre>
104      * {
105      *  "token":"RHIKGlJyZhidI/JSK0a2RQ==",
106      *  "result":true,
107      *  "deviceStatus":"IDLE",
108      *  "data":{"password":"Esf56mxqFY","name":"VELUX_KLF_847C"},
109      *  "errors":[]
110      * }
111      * </pre>
112      */
113     @NonNullByDefault
114     private static class Response {
115         @SuppressWarnings("unused")
116         private String token = VeluxBindingConstants.UNKNOWN;
117         private boolean result;
118         private String deviceStatus = VeluxBindingConstants.UNKNOWN;
119         private BCWLANConfig data = new BCWLANConfig();
120         private String[] errors = {};
121
122         public boolean getResult() {
123             return result;
124         }
125
126         @Override
127         public String toString() {
128             return data.toString();
129         }
130     }
131
132     /*
133      * Methods required for interface {@link BridgeCommunicationProtocol}.
134      */
135
136     @Override
137     public String name() {
138         return DESCRIPTION;
139     }
140
141     @Override
142     public String getURL() {
143         return URL;
144     }
145
146     @Override
147     public Object getObjectOfRequest() {
148         return request;
149     }
150
151     @Override
152     public Class<Response> getClassOfResponse() {
153         return Response.class;
154     }
155
156     @Override
157     public void setResponse(Object response) {
158         this.response = (Response) response;
159     }
160
161     @Override
162     public boolean isCommunicationSuccessful() {
163         return response.getResult();
164     }
165
166     @Override
167     public String getDeviceStatus() {
168         return response.deviceStatus;
169     }
170
171     @Override
172     public String[] getErrors() {
173         return response.errors;
174     }
175
176     /**
177      * Methods in addition to interface {@link JsonBridgeCommunicationProtocol}.
178      */
179     @Override
180     public VeluxGwWLAN getWLANConfig() {
181         VeluxGwWLAN gwWLAN = new VeluxGwWLAN(response.data.name, response.data.password);
182         return gwWLAN;
183     }
184 }