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