]> git.basschouten.com Git - openhab-addons.git/blob
93574a106add1660272ecf5071bcef8a7f3c87be
[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.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 a gateway error.
60      */
61     private static class Request {
62
63         @SuppressWarnings("unused")
64         private String action;
65
66         @SuppressWarnings("unused")
67         private Map<String, String> params;
68
69         public Request() {
70             this.action = "discover";
71             this.params = new HashMap<>();
72         }
73     }
74
75     /**
76      * Bridge I/O Response message used by {@link JsonVeluxBridge} for deserializing with including component access
77      * methods.
78      *
79      * Expected JSON (sample):
80      *
81      * <pre>
82      * {
83      *  "token":"RHIKGlJyZhidI/JSK0a2RQ==",
84      *  "result":true,
85      *  "deviceStatus":"discovering",
86      *  "data":{},
87      *  "errors":[]
88      * }
89      * </pre>
90      */
91     private static class Response {
92         @SuppressWarnings("unused")
93         private String token = VeluxBindingConstants.UNKNOWN;
94         private boolean result;
95         private String deviceStatus = VeluxBindingConstants.UNKNOWN;
96         @SuppressWarnings("unused")
97         private @Nullable Object data = null;
98         private String[] errors = {};
99
100         public boolean getResult() {
101             return result;
102         }
103
104         public String getDeviceStatus() {
105             return deviceStatus;
106         }
107
108         public String[] getErrors() {
109             return errors;
110         }
111     }
112
113     /*
114      * Methods required for interface {@link BridgeCommunicationProtocol}.
115      */
116
117     @Override
118     public String name() {
119         return DESCRIPTION;
120     }
121
122     @Override
123     public String getURL() {
124         return URL;
125     }
126
127     @Override
128     public Object getObjectOfRequest() {
129         return request;
130     }
131
132     @Override
133     public Class<Response> getClassOfResponse() {
134         return Response.class;
135     }
136
137     @Override
138     public void setResponse(Object thisResponse) {
139         response = (Response) thisResponse;
140     }
141
142     @Override
143     public boolean isCommunicationSuccessful() {
144         return response.getResult();
145     }
146
147     @Override
148     public String getDeviceStatus() {
149         return response.getDeviceStatus();
150     }
151
152     @Override
153     public String[] getErrors() {
154         return response.getErrors();
155     }
156 }