2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.velux.internal.bridge.json;
15 import java.util.HashMap;
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;
27 * Specific bridge communication message supported by the Velux bridge.
29 * Message semantic: Retrieval of scene configurations.
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}.
37 * @author Guenther Schreiner - Initial contribution.
40 class JCgetScenes extends GetScenes implements JsonBridgeCommunicationProtocol {
42 private static final String URL = "/api/v1/scenes";
43 private static final String DESCRIPTION = "get Scenes";
45 private Request request = new Request();
46 private Response response = new Response();
49 * Bridge Communication Structure containing the state of a product.
51 * Therefore it includes the typeId and name identifying the product, as well as actuator and status.
53 * Used within structure {@link BCscene} to describe the final states of the products belonging to this scene.
57 * "name": "Rolladen Schlafzimmer",
63 private static class BCproductState {
65 private String name = VeluxBindingConstants.UNKNOWN;
71 * Bridge Communication Structure containing a scene with different states of products.
73 * Therefore it includes the name and id identifying the scene, a flag about silence-mode, as well as the different
76 * These states are defined by an array of {@link BCproductState} as part of this structure.
80 * "name": "V_DG_Shutter_West_100",
83 * "bCproductStates": [
86 * "name": "Rolladen Schlafzimmer",
95 private static class BCscene {
96 private String name = VeluxBindingConstants.UNKNOWN;
98 private boolean silent;
99 private BCproductState[] products = {};
103 * Bridge I/O Request message used by {@link org.openhab.binding.velux.internal.bridge.json.JsonVeluxBridge
110 * {"action":"get","params":{}}
114 private static class Request {
116 @SuppressWarnings("unused")
117 private String action;
118 @SuppressWarnings("unused")
119 private Map<String, String> params;
123 this.params = new HashMap<>();
128 * Bridge Communication Structure describing a response to be received from the Velux Bridge.
132 * "token": "kWwXRQ5mlwgYfvk23g2zXw==",
134 * "deviceStatus": "IDLE",
137 * "name": "V_DG_Shutter_West_100",
140 * "bCproductStates": [
143 * "name": "Rolladen Schlafzimmer",
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 = {};
164 * Methods required for interface {@link BridgeCommunicationProtocol}.
168 public String name() {
173 public String getURL() {
178 public Object getObjectOfRequest() {
183 public Class<Response> getClassOfResponse() {
184 return Response.class;
188 public void setResponse(Object thisResponse) {
189 response = (Response) thisResponse;
193 public boolean isCommunicationSuccessful() {
194 return response.result;
198 public String getDeviceStatus() {
199 return response.deviceStatus;
203 public String[] getErrors() {
204 return response.errors;
208 * Methods in addition to interface {@link JsonBridgeCommunicationProtocol}.
211 public VeluxScene[] getScenes() {
212 VeluxScene[] scenes = new VeluxScene[response.data.length];
213 for (int sceneIdx = 0; sceneIdx < response.data.length; sceneIdx++) {
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);
224 scenes[sceneIdx] = new VeluxScene(response.data[sceneIdx].name, response.data[sceneIdx].id,
225 response.data[sceneIdx].silent, productStates);