]> git.basschouten.com Git - openhab-addons.git/blob
f59143e4fa659d532116b86129a8c0322b802b5e
[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.GetDeviceStatus;
22 import org.openhab.binding.velux.internal.things.VeluxGwState;
23 import org.openhab.binding.velux.internal.things.VeluxGwState.VeluxGatewayState;
24 import org.openhab.binding.velux.internal.things.VeluxGwState.VeluxGatewaySubState;
25
26 /**
27  * Specific bridge communication message supported by the Velux bridge.
28  * <P>
29  * Message semantic: Retrieval of current bridge state.
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 JCgetDeviceStatus extends GetDeviceStatus implements JsonBridgeCommunicationProtocol {
41
42     private static final String URL = "/api/v1/device";
43     private static final String DESCRIPTION = "get device status";
44
45     private Request request = new Request();
46     private Response response = new Response();
47
48     /*
49      * Message Objects
50      */
51
52     /**
53      * Bridge I/O Request message used by {@link org.openhab.binding.velux.internal.bridge.json.JsonVeluxBridge
54      * JsonVeluxBridge} for serializing.
55      *
56      * Resulting JSON:
57      *
58      * <pre>
59      * {"action":"getDeviceStatus","params":{}}
60      * </pre>
61      *
62      * NOTE: the gateway software is extremely sensitive to this exact JSON structure.
63      * Any modifications (like omitting empty params) will lead to a gateway error.
64      */
65     private static class Request {
66
67         @SuppressWarnings("unused")
68         private String action;
69
70         @SuppressWarnings("unused")
71         private Map<String, String> params;
72
73         public Request() {
74             this.action = "getDeviceStatus";
75             this.params = new HashMap<>();
76         }
77     }
78
79     /**
80      * Bridge I/O Response message used by {@link JsonVeluxBridge} for deserializing with including component access
81      * methods
82      * <P>
83      * Expected JSON (sample):
84      *
85      * <pre>
86      * {
87      *  "token":"RHIKGlJyZhidI/JSK0a2RQ==",
88      *  "result":true,
89      *  "deviceStatus":"discovering",       or "IDLE"
90      *  "data":{},
91      *  "errors":[]
92      * }
93      * </pre>
94      */
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 @Nullable Object data = null;
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
149     /*
150      * Methods in addition to interface {@link BridgeCommunicationProtocol}.
151      */
152
153     @Override
154     public VeluxGwState getState() {
155         String deviceStatus = this.getDeviceStatus();
156         byte stateValue = (byte) VeluxGatewayState.GW_S_GWM.getStateValue();
157         byte subStateValue;
158         if ("discovering".equals(deviceStatus)) {
159             subStateValue = (byte) VeluxGatewaySubState.GW_SS_P1.getStateValue();
160         } else if ("IDLE".equals(deviceStatus)) {
161             subStateValue = (byte) VeluxGatewaySubState.GW_SS_IDLE.getStateValue();
162         } else {
163             subStateValue = (byte) VeluxGatewaySubState.GW_SS_P2.getStateValue();
164         }
165         return new VeluxGwState(stateValue, subStateValue);
166     }
167 }