]> git.basschouten.com Git - openhab-addons.git/blob
1c1900d13c1b890cc8554dc8df791f19b5afaf64
[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.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     private static class Request {
61
62         @SuppressWarnings("unused")
63         private String action;
64
65         @SuppressWarnings("unused")
66         private Map<String, String> params;
67
68         public Request() {
69             this.action = "wifi";
70             this.params = new HashMap<>();
71         }
72     }
73
74     /**
75      * Bridge Communication Structure containing the version of the firmware.
76      * <P>
77      * Used within structure {@link JCgetWLANConfig} to describe the network connectivity of the Bridge.
78      *
79      * <PRE>
80      * {"password":"Esf56mxqFY","name":"VELUX_KLF_847C"}
81      * </PRE>
82      */
83     private static class BCWLANConfig {
84
85         private String password = VeluxBindingConstants.UNKNOWN;
86         private String name = VeluxBindingConstants.UNKNOWN;
87
88         @Override
89         public String toString() {
90             return String.format("SSID=%s,password=********", this.name);
91         }
92     }
93
94     /**
95      * Bridge I/O Response message used by {@link JsonBridgeCommunicationProtocol} for deserialization with including
96      * component access
97      * methods
98      * <P>
99      * Expected JSON (sample):
100      *
101      * <pre>
102      * {
103      *  "token":"RHIKGlJyZhidI/JSK0a2RQ==",
104      *  "result":true,
105      *  "deviceStatus":"IDLE",
106      *  "data":{"password":"Esf56mxqFY","name":"VELUX_KLF_847C"},
107      *  "errors":[]
108      * }
109      * </pre>
110      */
111     private static class Response {
112         @SuppressWarnings("unused")
113         private String token = VeluxBindingConstants.UNKNOWN;
114         private boolean result;
115         private String deviceStatus = VeluxBindingConstants.UNKNOWN;
116         private BCWLANConfig data = new BCWLANConfig();
117         private String[] errors = {};
118
119         public boolean getResult() {
120             return result;
121         }
122
123         @Override
124         public String toString() {
125             return data.toString();
126         }
127     }
128
129     /*
130      * Methods required for interface {@link BridgeCommunicationProtocol}.
131      */
132
133     @Override
134     public String name() {
135         return DESCRIPTION;
136     }
137
138     @Override
139     public String getURL() {
140         return URL;
141     }
142
143     @Override
144     public Object getObjectOfRequest() {
145         return request;
146     }
147
148     @Override
149     public Class<Response> getClassOfResponse() {
150         return Response.class;
151     }
152
153     @Override
154     public void setResponse(Object response) {
155         this.response = (Response) response;
156     }
157
158     @Override
159     public boolean isCommunicationSuccessful() {
160         return response.getResult();
161     }
162
163     @Override
164     public String getDeviceStatus() {
165         return response.deviceStatus;
166     }
167
168     @Override
169     public String[] getErrors() {
170         return response.errors;
171     }
172
173     /**
174      * Methods in addition to interface {@link JsonBridgeCommunicationProtocol}.
175      */
176     @Override
177     public VeluxGwWLAN getWLANConfig() {
178         return new VeluxGwWLAN(response.data.name, response.data.password);
179     }
180 }