]> git.basschouten.com Git - openhab-addons.git/blob
b70c6566f608f47f667aa3304a5d7746c81c4ca2
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.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;
25
26 /**
27  * Specific bridge communication message supported by the Velux bridge.
28  * <P>
29  * Message semantic: Retrieval of products.
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 JCgetProducts extends GetProducts implements JsonBridgeCommunicationProtocol {
41
42     private static final String URL = "/api/v1/products";
43     private static final String DESCRIPTION = "get Products";
44
45     private Request request = new Request();
46     private Response response = new Response();
47
48     /**
49      * Bridge Communication class describing a product
50      *
51      * <PRE>
52      * "name": "Rolladen Bad",
53      * "category": "Roller shutter",
54      * "id": 2,
55      * "typeId": 2,
56      * "subtype": 0,
57      * "scenes": [
58      * "V_DG_Shutter_Mitte_000",
59      * "V_DG_Shutter_Mitte_085",
60      * "V_DG_Shutter_Mitte_100"
61      * ]
62      * </PRE>
63      */
64     @NonNullByDefault
65     private class BCproduct {
66         private String name = VeluxBindingConstants.UNKNOWN;
67         @SuppressWarnings("unused")
68         private String category = VeluxBindingConstants.UNKNOWN;
69         private int id;
70         private int typeId;
71         @SuppressWarnings("unused")
72         private int subtype;
73         @SuppressWarnings("unused")
74         private String[] scenes = {};
75     }
76
77     /**
78      * Bridge I/O Request message used by {@link org.openhab.binding.velux.internal.bridge.json.JsonVeluxBridge
79      * JsonVeluxBridge} for serializing.
80      * <P>
81      * Resulting JSON:
82      *
83      * <pre>
84      * {"action":"get","params":{}}
85      * </pre>
86      */
87     @NonNullByDefault
88     private static class Request {
89
90         @SuppressWarnings("unused")
91         private String action;
92
93         @SuppressWarnings("unused")
94         private Map<String, String> params;
95
96         public Request() {
97             this.action = "get";
98             this.params = new HashMap<>();
99         }
100     }
101
102     /**
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
105      * <P>
106      * Expected JSON (sample):
107      *
108      * <pre>
109      * {
110      * "token": "pESIc/9zDWa1CJR6hCDzLw==",
111      * "result": true,
112      * "deviceStatus": "IDLE",
113      * "data": [
114      *  { "name": "Bad",
115      *    "category": "Window opener",
116      *    "id": 0,
117      *    "typeId": 4,
118      *    "subtype": 1,
119      *    "scenes": [
120      *       "V_DG_Window_Mitte_000",
121      *       "V_DG_Window_Mitte_100"
122      *    ]
123      *  },
124      * ],
125      * "errors": []
126      * }
127      * </pre>
128      */
129     @NonNullByDefault
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 = {};
137     }
138
139     /*
140      * Methods required for interface {@link BridgeCommunicationProtocol}.
141      */
142
143     @Override
144     public String name() {
145         return DESCRIPTION;
146     }
147
148     @Override
149     public String getURL() {
150         return URL;
151     }
152
153     @Override
154     public Object getObjectOfRequest() {
155         return request;
156     }
157
158     @Override
159     public Class<Response> getClassOfResponse() {
160         return Response.class;
161     }
162
163     @Override
164     public void setResponse(Object thisResponse) {
165         response = (Response) thisResponse;
166     }
167
168     @Override
169     public boolean isCommunicationSuccessful() {
170         return response.result;
171     }
172
173     @Override
174     public String getDeviceStatus() {
175         return response.deviceStatus;
176     }
177
178     @Override
179     public String[] getErrors() {
180         return response.errors;
181     }
182
183     /**
184      * Methods in addition to interface {@link JsonBridgeCommunicationProtocol}.
185      */
186     @Override
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));
193         }
194         return products;
195     }
196 }