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.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"
64 private class BCproduct {
65 private String name = VeluxBindingConstants.UNKNOWN;
66 @SuppressWarnings("unused")
67 private String category = VeluxBindingConstants.UNKNOWN;
70 @SuppressWarnings("unused")
72 @SuppressWarnings("unused")
73 private String[] scenes = {};
77 * Bridge I/O Request message used by {@link org.openhab.binding.velux.internal.bridge.json.JsonVeluxBridge
78 * JsonVeluxBridge} for serializing.
83 * {"action":"get","params":{}}
86 private static class Request {
88 @SuppressWarnings("unused")
89 private String action;
91 @SuppressWarnings("unused")
92 private Map<String, String> params;
96 this.params = new HashMap<>();
101 * Bridge I/O Response message used by {@link org.openhab.binding.velux.internal.bridge.VeluxBridge VeluxBridge} for
102 * deserialization with including component access methods
104 * Expected JSON (sample):
108 * "token": "pESIc/9zDWa1CJR6hCDzLw==",
110 * "deviceStatus": "IDLE",
113 * "category": "Window opener",
118 * "V_DG_Window_Mitte_000",
119 * "V_DG_Window_Mitte_100"
127 private static class Response {
128 @SuppressWarnings("unused")
129 private String token = VeluxBindingConstants.UNKNOWN;
130 private boolean result;
131 private String deviceStatus = VeluxBindingConstants.UNKNOWN;
132 private JCgetProducts.BCproduct[] data = {};
133 private String[] errors = {};
137 * Methods required for interface {@link BridgeCommunicationProtocol}.
141 public String name() {
146 public String getURL() {
151 public Object getObjectOfRequest() {
156 public Class<Response> getClassOfResponse() {
157 return Response.class;
161 public void setResponse(Object thisResponse) {
162 response = (Response) thisResponse;
166 public boolean isCommunicationSuccessful() {
167 return response.result;
171 public String getDeviceStatus() {
172 return response.deviceStatus;
176 public String[] getErrors() {
177 return response.errors;
181 * Methods in addition to interface {@link JsonBridgeCommunicationProtocol}.
184 public VeluxProduct[] getProducts() {
185 VeluxProduct[] products = new VeluxProduct[response.data.length];
186 for (int productIdx = 0; productIdx < response.data.length; productIdx++) {
187 products[productIdx] = new VeluxProduct(new VeluxProductName(response.data[productIdx].name),
188 VeluxProductType.get(response.data[productIdx].typeId),
189 new ProductBridgeIndex(response.data[productIdx].id));