]> git.basschouten.com Git - openhab-addons.git/blob
53dd187b00c0ba37c0d8bfaae131ceed93f48f6e
[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.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     private static class BCproductState {
63         private int typeId;
64         private String name = VeluxBindingConstants.UNKNOWN;
65         private int actuator;
66         private int status;
67     }
68
69     /**
70      * Bridge Communication Structure containing a scene with different states of products.
71      * <P>
72      * Therefore it includes the name and id identifying the scene, a flag about silence-mode, as well as the different
73      * states.
74      * <P>
75      * These states are defined by an array of {@link BCproductState} as part of this structure.
76      *
77      * <PRE>
78      * {
79      * "name": "V_DG_Shutter_West_100",
80      * "id": 0,
81      * "silent": true,
82      * "bCproductStates": [
83      * {
84      * "typeId": 2,
85      * "name": "Rolladen Schlafzimmer",
86      * "actuator": 0,
87      * "status": 100
88      * }
89      * ]
90      * },
91      * </PRE>
92      */
93     private static class BCscene {
94         private String name = VeluxBindingConstants.UNKNOWN;
95         private int id;
96         private boolean silent;
97         private BCproductState[] products = {};
98     }
99
100     /**
101      * Bridge I/O Request message used by {@link org.openhab.binding.velux.internal.bridge.json.JsonVeluxBridge
102      * JsonVeluxBridge}
103      * for serializing.
104      * <P>
105      * Resulting JSON:
106      *
107      * <pre>
108      * {"action":"get","params":{}}
109      * </pre>
110      */
111     private static class Request {
112
113         @SuppressWarnings("unused")
114         private String action;
115         @SuppressWarnings("unused")
116         private Map<String, String> params;
117
118         public Request() {
119             this.action = "get";
120             this.params = new HashMap<>();
121         }
122     }
123
124     /**
125      * Bridge Communication Structure describing a response to be received from the Velux Bridge.
126      *
127      * <PRE>
128      * {
129      * "token": "kWwXRQ5mlwgYfvk23g2zXw==",
130      * "result": true,
131      * "deviceStatus": "IDLE",
132      * "data": [
133      * {
134      * "name": "V_DG_Shutter_West_100",
135      * "id": 0,
136      * "silent": true,
137      * "bCproductStates": [
138      * {
139      * "typeId": 2,
140      * "name": "Rolladen Schlafzimmer",
141      * "actuator": 0,
142      * "status": 100
143      * }
144      * ]
145      * },
146      * "errors": []
147      * }
148      * </PRE>
149      */
150     private static class Response {
151         @SuppressWarnings("unused")
152         private String token = VeluxBindingConstants.UNKNOWN;
153         private boolean result;
154         private String deviceStatus = VeluxBindingConstants.UNKNOWN;
155         private BCscene[] data = {};
156         private String[] errors = {};
157     }
158
159     /*
160      * Methods required for interface {@link BridgeCommunicationProtocol}.
161      */
162
163     @Override
164     public String name() {
165         return DESCRIPTION;
166     }
167
168     @Override
169     public String getURL() {
170         return URL;
171     }
172
173     @Override
174     public Object getObjectOfRequest() {
175         return request;
176     }
177
178     @Override
179     public Class<Response> getClassOfResponse() {
180         return Response.class;
181     }
182
183     @Override
184     public void setResponse(Object thisResponse) {
185         response = (Response) thisResponse;
186     }
187
188     @Override
189     public boolean isCommunicationSuccessful() {
190         return response.result;
191     }
192
193     @Override
194     public String getDeviceStatus() {
195         return response.deviceStatus;
196     }
197
198     @Override
199     public String[] getErrors() {
200         return response.errors;
201     }
202
203     /**
204      * Methods in addition to interface {@link JsonBridgeCommunicationProtocol}.
205      */
206     @Override
207     public VeluxScene[] getScenes() {
208         VeluxScene[] scenes = new VeluxScene[response.data.length];
209         for (int sceneIdx = 0; sceneIdx < response.data.length; sceneIdx++) {
210
211             VeluxProductState[] productStates = new VeluxProductState[response.data[sceneIdx].products.length];
212             for (int productIdx = 0; productIdx < response.data[sceneIdx].products.length; productIdx++) {
213                 productStates[productIdx] = new VeluxProductState(
214                         new VeluxProductReference(
215                                 new VeluxProductName(response.data[sceneIdx].products[productIdx].name),
216                                 response.data[sceneIdx].products[productIdx].typeId),
217                         response.data[sceneIdx].products[productIdx].actuator,
218                         response.data[sceneIdx].products[productIdx].status);
219             }
220             scenes[sceneIdx] = new VeluxScene(response.data[sceneIdx].name, response.data[sceneIdx].id,
221                     response.data[sceneIdx].silent, productStates);
222         }
223         return scenes;
224     }
225 }