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