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