2 * Copyright (c) 2010-2023 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",
62 private static class BCproductState {
64 private String name = VeluxBindingConstants.UNKNOWN;
70 * Bridge Communication Structure containing a scene with different states of products.
72 * Therefore it includes the name and id identifying the scene, a flag about silence-mode, as well as the different
75 * These states are defined by an array of {@link BCproductState} as part of this structure.
79 * "name": "V_DG_Shutter_West_100",
82 * "bCproductStates": [
85 * "name": "Rolladen Schlafzimmer",
93 private static class BCscene {
94 private String name = VeluxBindingConstants.UNKNOWN;
96 private boolean silent;
97 private BCproductState[] products = {};
101 * Bridge I/O Request message used by {@link org.openhab.binding.velux.internal.bridge.json.JsonVeluxBridge
108 * {"action":"get","params":{}}
111 private static class Request {
113 @SuppressWarnings("unused")
114 private String action;
115 @SuppressWarnings("unused")
116 private Map<String, String> params;
120 this.params = new HashMap<>();
125 * Bridge Communication Structure describing a response to be received from the Velux Bridge.
129 * "token": "kWwXRQ5mlwgYfvk23g2zXw==",
131 * "deviceStatus": "IDLE",
134 * "name": "V_DG_Shutter_West_100",
137 * "bCproductStates": [
140 * "name": "Rolladen Schlafzimmer",
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 = {};
160 * Methods required for interface {@link BridgeCommunicationProtocol}.
164 public String name() {
169 public String getURL() {
174 public Object getObjectOfRequest() {
179 public Class<Response> getClassOfResponse() {
180 return Response.class;
184 public void setResponse(Object thisResponse) {
185 response = (Response) thisResponse;
189 public boolean isCommunicationSuccessful() {
190 return response.result;
194 public String getDeviceStatus() {
195 return response.deviceStatus;
199 public String[] getErrors() {
200 return response.errors;
204 * Methods in addition to interface {@link JsonBridgeCommunicationProtocol}.
207 public VeluxScene[] getScenes() {
208 VeluxScene[] scenes = new VeluxScene[response.data.length];
209 for (int sceneIdx = 0; sceneIdx < response.data.length; sceneIdx++) {
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);
220 scenes[sceneIdx] = new VeluxScene(response.data[sceneIdx].name, response.data[sceneIdx].id,
221 response.data[sceneIdx].silent, productStates);