]> git.basschouten.com Git - openhab-addons.git/blob
4f01d18c631788c28516c4df6e7742819b0bd309
[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.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.velux.internal.VeluxBindingConstants;
21 import org.openhab.binding.velux.internal.bridge.common.RunProductDiscovery;
22
23 /**
24  * Specific bridge communication message supported by the Velux bridge.
25  * <P>
26  * Message semantic:Action to start discovery of products, i.e. Velux devices.
27  * <P>
28  *
29  * It defines information how to send query and receive answer through the
30  * {@link org.openhab.binding.velux.internal.bridge.VeluxBridgeProvider VeluxBridgeProvider}
31  * as described by the {@link JsonBridgeCommunicationProtocol}.
32  *
33  * @author Guenther Schreiner - Initial contribution.
34  */
35 @NonNullByDefault
36 class JCrunProductDiscovery extends RunProductDiscovery implements JsonBridgeCommunicationProtocol {
37
38     private static final String URL = "/api/v1/products";
39     private static final String DESCRIPTION = "discover products";
40
41     private Request request = new Request();
42     private Response response = new Response();
43
44     /*
45      * Message Objects
46      */
47
48     /**
49      * Bridge I/O Request message used by {@link JsonVeluxBridge}
50      * for serializing.
51      *
52      * Resulting JSON:
53      *
54      * <pre>
55      * {"action":"discover","params":{}}
56      * </pre>
57      *
58      * NOTE: the gateway software is extremely sensitive to this exact JSON structure.
59      * Any modifications (like omitting empty params) will lead to an gateway error.
60      */
61     @NonNullByDefault
62     private static class Request {
63
64         @SuppressWarnings("unused")
65         private String action;
66
67         @SuppressWarnings("unused")
68         private Map<String, String> params;
69
70         public Request() {
71             this.action = "discover";
72             this.params = new HashMap<>();
73         }
74     }
75
76     /**
77      * Bridge I/O Response message used by {@link JsonVeluxBridge} for deserializing with including component access
78      * methods.
79      *
80      * Expected JSON (sample):
81      *
82      * <pre>
83      * {
84      *  "token":"RHIKGlJyZhidI/JSK0a2RQ==",
85      *  "result":true,
86      *  "deviceStatus":"discovering",
87      *  "data":{},
88      *  "errors":[]
89      * }
90      * </pre>
91      */
92     @NonNullByDefault
93     private static class Response {
94         @SuppressWarnings("unused")
95         private String token = VeluxBindingConstants.UNKNOWN;
96         private boolean result;
97         private String deviceStatus = VeluxBindingConstants.UNKNOWN;
98         @SuppressWarnings("unused")
99         private @Nullable Object data = null;
100         private String[] errors = {};
101
102         public boolean getResult() {
103             return result;
104         }
105
106         public String getDeviceStatus() {
107             return deviceStatus;
108         }
109
110         public String[] getErrors() {
111             return errors;
112         }
113     }
114
115     /*
116      * Methods required for interface {@link BridgeCommunicationProtocol}.
117      */
118
119     @Override
120     public String name() {
121         return DESCRIPTION;
122     }
123
124     @Override
125     public String getURL() {
126         return URL;
127     }
128
129     @Override
130     public Object getObjectOfRequest() {
131         return request;
132     }
133
134     @Override
135     public Class<Response> getClassOfResponse() {
136         return Response.class;
137     }
138
139     @Override
140     public void setResponse(Object thisResponse) {
141         response = (Response) thisResponse;
142     }
143
144     @Override
145     public boolean isCommunicationSuccessful() {
146         return response.getResult();
147     }
148
149     @Override
150     public String getDeviceStatus() {
151         return response.getDeviceStatus();
152     }
153
154     @Override
155     public String[] getErrors() {
156         return response.getErrors();
157     }
158 }