]> git.basschouten.com Git - openhab-addons.git/blob
284a28579d4ad964dfeb6dc6955b66fdfc184ebe
[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.GetScenes;
21 import org.openhab.binding.velux.internal.things.VeluxProductName;
22 import org.openhab.binding.velux.internal.things.VeluxProductReference;
23 import org.openhab.binding.velux.internal.things.VeluxProductState;
24 import org.openhab.binding.velux.internal.things.VeluxScene;
25
26 /**
27  * Specific bridge communication message supported by the Velux bridge.
28  * <P>
29  * Message semantic: Retrieval of scene configurations.
30  * <P>
31  *
32  * It defines information how to send query and receive answer through the
33  * {@link org.openhab.binding.velux.internal.bridge.VeluxBridgeProvider VeluxBridgeProvider}
34  * as described by the {@link org.openhab.binding.velux.internal.bridge.json.JsonBridgeCommunicationProtocol
35  * BridgeCommunicationProtocol}.
36  *
37  * @author Guenther Schreiner - Initial contribution.
38  */
39 @NonNullByDefault
40 class JCgetScenes extends GetScenes implements JsonBridgeCommunicationProtocol {
41
42     private static final String URL = "/api/v1/scenes";
43     private static final String DESCRIPTION = "get Scenes";
44
45     private Request request = new Request();
46     private Response response = new Response();
47
48     /**
49      * Bridge Communication Structure containing the state of a product.
50      * <P>
51      * Therefore it includes the typeId and name identifying the product, as well as actuator and status.
52      * <P>
53      * Used within structure {@link BCscene} to describe the final states of the products belonging to this scene.
54      *
55      * <PRE>
56      * "typeId": 2,
57      * "name": "Rolladen Schlafzimmer",
58      * "actuator": 0,
59      * "status": 0
60      * </PRE>
61      */
62     @NonNullByDefault
63     private static class BCproductState {
64         private int typeId;
65         private String name = VeluxBindingConstants.UNKNOWN;
66         private int actuator;
67         private int status;
68     }
69
70     /**
71      * Bridge Communication Structure containing a scene with different states of products.
72      * <P>
73      * Therefore it includes the name and id identifying the scene, a flag about silence-mode, as well as the different
74      * states.
75      * <P>
76      * These states are defined by an array of {@link BCproductState} as part of this structure.
77      *
78      * <PRE>
79      * {
80      * "name": "V_DG_Shutter_West_100",
81      * "id": 0,
82      * "silent": true,
83      * "bCproductStates": [
84      * {
85      * "typeId": 2,
86      * "name": "Rolladen Schlafzimmer",
87      * "actuator": 0,
88      * "status": 100
89      * }
90      * ]
91      * },
92      * </PRE>
93      */
94     @NonNullByDefault
95     private static class BCscene {
96         private String name = VeluxBindingConstants.UNKNOWN;
97         private int id;
98         private boolean silent;
99         private BCproductState[] products = {};
100     }
101
102     /**
103      * Bridge I/O Request message used by {@link org.openhab.binding.velux.internal.bridge.json.JsonVeluxBridge
104      * JsonVeluxBridge}
105      * for serializing.
106      * <P>
107      * Resulting JSON:
108      *
109      * <pre>
110      * {"action":"get","params":{}}
111      * </pre>
112      */
113     @NonNullByDefault
114     private static class Request {
115
116         @SuppressWarnings("unused")
117         private String action;
118         @SuppressWarnings("unused")
119         private Map<String, String> params;
120
121         public Request() {
122             this.action = "get";
123             this.params = new HashMap<>();
124         }
125     }
126
127     /**
128      * Bridge Communication Structure describing a response to be received from the Velux Bridge.
129      *
130      * <PRE>
131      * {
132      * "token": "kWwXRQ5mlwgYfvk23g2zXw==",
133      * "result": true,
134      * "deviceStatus": "IDLE",
135      * "data": [
136      * {
137      * "name": "V_DG_Shutter_West_100",
138      * "id": 0,
139      * "silent": true,
140      * "bCproductStates": [
141      * {
142      * "typeId": 2,
143      * "name": "Rolladen Schlafzimmer",
144      * "actuator": 0,
145      * "status": 100
146      * }
147      * ]
148      * },
149      * "errors": []
150      * }
151      * </PRE>
152      */
153     @NonNullByDefault
154     private static class Response {
155         @SuppressWarnings("unused")
156         private String token = VeluxBindingConstants.UNKNOWN;
157         private boolean result;
158         private String deviceStatus = VeluxBindingConstants.UNKNOWN;
159         private BCscene[] data = {};
160         private String[] errors = {};
161     }
162
163     /*
164      * Methods required for interface {@link BridgeCommunicationProtocol}.
165      */
166
167     @Override
168     public String name() {
169         return DESCRIPTION;
170     }
171
172     @Override
173     public String getURL() {
174         return URL;
175     }
176
177     @Override
178     public Object getObjectOfRequest() {
179         return request;
180     }
181
182     @Override
183     public Class<Response> getClassOfResponse() {
184         return Response.class;
185     }
186
187     @Override
188     public void setResponse(Object thisResponse) {
189         response = (Response) thisResponse;
190     }
191
192     @Override
193     public boolean isCommunicationSuccessful() {
194         return response.result;
195     }
196
197     @Override
198     public String getDeviceStatus() {
199         return response.deviceStatus;
200     }
201
202     @Override
203     public String[] getErrors() {
204         return response.errors;
205     }
206
207     /**
208      * Methods in addition to interface {@link JsonBridgeCommunicationProtocol}.
209      */
210     @Override
211     public VeluxScene[] getScenes() {
212         VeluxScene[] scenes = new VeluxScene[response.data.length];
213         for (int sceneIdx = 0; sceneIdx < response.data.length; sceneIdx++) {
214
215             VeluxProductState[] productStates = new VeluxProductState[response.data[sceneIdx].products.length];
216             for (int productIdx = 0; productIdx < response.data[sceneIdx].products.length; productIdx++) {
217                 productStates[productIdx] = new VeluxProductState(
218                         new VeluxProductReference(
219                                 new VeluxProductName(response.data[sceneIdx].products[productIdx].name),
220                                 response.data[sceneIdx].products[productIdx].typeId),
221                         response.data[sceneIdx].products[productIdx].actuator,
222                         response.data[sceneIdx].products[productIdx].status);
223             }
224             scenes[sceneIdx] = new VeluxScene(response.data[sceneIdx].name, response.data[sceneIdx].id,
225                     response.data[sceneIdx].silent, productStates);
226         }
227         return scenes;
228     }
229 }