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.GetProducts;
21 import org.openhab.binding.velux.internal.things.VeluxProduct;
22 import org.openhab.binding.velux.internal.things.VeluxProduct.ProductBridgeIndex;
23 import org.openhab.binding.velux.internal.things.VeluxProductName;
24 import org.openhab.binding.velux.internal.things.VeluxProductType;
27 * Specific bridge communication message supported by the Velux bridge.
29 * Message semantic: Retrieval of products.
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 JCgetProducts extends GetProducts implements JsonBridgeCommunicationProtocol {
42 private static final String URL = "/api/v1/products";
43 private static final String DESCRIPTION = "get Products";
45 private Request request = new Request();
46 private Response response = new Response();
49 * Bridge Communication class describing a product
52 * "name": "Rolladen Bad",
53 * "category": "Roller shutter",
58 * "V_DG_Shutter_Mitte_000",
59 * "V_DG_Shutter_Mitte_085",
60 * "V_DG_Shutter_Mitte_100"
65 private class BCproduct {
66 private String name = VeluxBindingConstants.UNKNOWN;
67 @SuppressWarnings("unused")
68 private String category = VeluxBindingConstants.UNKNOWN;
71 @SuppressWarnings("unused")
73 @SuppressWarnings("unused")
74 private String[] scenes = {};
78 * Bridge I/O Request message used by {@link org.openhab.binding.velux.internal.bridge.json.JsonVeluxBridge
79 * JsonVeluxBridge} for serializing.
84 * {"action":"get","params":{}}
88 private static class Request {
90 @SuppressWarnings("unused")
91 private String action;
93 @SuppressWarnings("unused")
94 private Map<String, String> params;
98 this.params = new HashMap<>();
103 * Bridge I/O Response message used by {@link org.openhab.binding.velux.internal.bridge.VeluxBridge VeluxBridge} for
104 * deserialization with including component access methods
106 * Expected JSON (sample):
110 * "token": "pESIc/9zDWa1CJR6hCDzLw==",
112 * "deviceStatus": "IDLE",
115 * "category": "Window opener",
120 * "V_DG_Window_Mitte_000",
121 * "V_DG_Window_Mitte_100"
130 private static class Response {
131 @SuppressWarnings("unused")
132 private String token = VeluxBindingConstants.UNKNOWN;
133 private boolean result;
134 private String deviceStatus = VeluxBindingConstants.UNKNOWN;
135 private JCgetProducts.BCproduct[] data = {};
136 private String[] errors = {};
140 * Methods required for interface {@link BridgeCommunicationProtocol}.
144 public String name() {
149 public String getURL() {
154 public Object getObjectOfRequest() {
159 public Class<Response> getClassOfResponse() {
160 return Response.class;
164 public void setResponse(Object thisResponse) {
165 response = (Response) thisResponse;
169 public boolean isCommunicationSuccessful() {
170 return response.result;
174 public String getDeviceStatus() {
175 return response.deviceStatus;
179 public String[] getErrors() {
180 return response.errors;
184 * Methods in addition to interface {@link JsonBridgeCommunicationProtocol}.
187 public VeluxProduct[] getProducts() {
188 VeluxProduct[] products = new VeluxProduct[response.data.length];
189 for (int productIdx = 0; productIdx < response.data.length; productIdx++) {
190 products[productIdx] = new VeluxProduct(new VeluxProductName(response.data[productIdx].name),
191 VeluxProductType.get(response.data[productIdx].typeId),
192 new ProductBridgeIndex(response.data[productIdx].id));