]> git.basschouten.com Git - openhab-addons.git/blob
280dbbd961464554583a9e1face815d1aa6f101c
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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     private class BCproduct {
65         private String name = VeluxBindingConstants.UNKNOWN;
66         @SuppressWarnings("unused")
67         private String category = VeluxBindingConstants.UNKNOWN;
68         private int id;
69         private int typeId;
70         @SuppressWarnings("unused")
71         private int subtype;
72         @SuppressWarnings("unused")
73         private String[] scenes = {};
74     }
75
76     /**
77      * Bridge I/O Request message used by {@link org.openhab.binding.velux.internal.bridge.json.JsonVeluxBridge
78      * JsonVeluxBridge} for serializing.
79      * <P>
80      * Resulting JSON:
81      *
82      * <pre>
83      * {"action":"get","params":{}}
84      * </pre>
85      */
86     private static class Request {
87
88         @SuppressWarnings("unused")
89         private String action;
90
91         @SuppressWarnings("unused")
92         private Map<String, String> params;
93
94         public Request() {
95             this.action = "get";
96             this.params = new HashMap<>();
97         }
98     }
99
100     /**
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
103      * <P>
104      * Expected JSON (sample):
105      *
106      * <pre>
107      * {
108      * "token": "pESIc/9zDWa1CJR6hCDzLw==",
109      * "result": true,
110      * "deviceStatus": "IDLE",
111      * "data": [
112      *  { "name": "Bad",
113      *    "category": "Window opener",
114      *    "id": 0,
115      *    "typeId": 4,
116      *    "subtype": 1,
117      *    "scenes": [
118      *       "V_DG_Window_Mitte_000",
119      *       "V_DG_Window_Mitte_100"
120      *    ]
121      *  },
122      * ],
123      * "errors": []
124      * }
125      * </pre>
126      */
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 = {};
134     }
135
136     /*
137      * Methods required for interface {@link BridgeCommunicationProtocol}.
138      */
139
140     @Override
141     public String name() {
142         return DESCRIPTION;
143     }
144
145     @Override
146     public String getURL() {
147         return URL;
148     }
149
150     @Override
151     public Object getObjectOfRequest() {
152         return request;
153     }
154
155     @Override
156     public Class<Response> getClassOfResponse() {
157         return Response.class;
158     }
159
160     @Override
161     public void setResponse(Object thisResponse) {
162         response = (Response) thisResponse;
163     }
164
165     @Override
166     public boolean isCommunicationSuccessful() {
167         return response.result;
168     }
169
170     @Override
171     public String getDeviceStatus() {
172         return response.deviceStatus;
173     }
174
175     @Override
176     public String[] getErrors() {
177         return response.errors;
178     }
179
180     /**
181      * Methods in addition to interface {@link JsonBridgeCommunicationProtocol}.
182      */
183     @Override
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));
190         }
191         return products;
192     }
193 }