2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.velux.internal.bridge.json;
15 import java.util.HashMap;
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;
27 * Specific bridge communication message supported by the Velux bridge.
29 * Message semantic: Retrieval of current bridge state.
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}.
37 * @author Guenther Schreiner - Initial contribution.
40 class JCgetDeviceStatus extends GetDeviceStatus implements JsonBridgeCommunicationProtocol {
42 private static final String URL = "/api/v1/device";
43 private static final String DESCRIPTION = "get device status";
45 private Request request = new Request();
46 private Response response = new Response();
53 * Bridge I/O Request message used by {@link org.openhab.binding.velux.internal.bridge.json.JsonVeluxBridge
54 * JsonVeluxBridge} for serializing.
59 * {"action":"getDeviceStatus","params":{}}
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.
65 private static class Request {
67 @SuppressWarnings("unused")
68 private String action;
70 @SuppressWarnings("unused")
71 private Map<String, String> params;
74 this.action = "getDeviceStatus";
75 this.params = new HashMap<>();
80 * Bridge I/O Response message used by {@link JsonVeluxBridge} for deserializing with including component access
83 * Expected JSON (sample):
87 * "token":"RHIKGlJyZhidI/JSK0a2RQ==",
89 * "deviceStatus":"discovering", or "IDLE"
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 = {};
106 * Methods required for interface {@link BridgeCommunicationProtocol}.
110 public String name() {
115 public String getURL() {
120 public Object getObjectOfRequest() {
125 public Class<Response> getClassOfResponse() {
126 return Response.class;
130 public void setResponse(Object thisResponse) {
131 response = (Response) thisResponse;
135 public boolean isCommunicationSuccessful() {
136 return response.result;
140 public String getDeviceStatus() {
141 return response.deviceStatus;
145 public String[] getErrors() {
146 return response.errors;
150 * Methods in addition to interface {@link BridgeCommunicationProtocol}.
154 public VeluxGwState getState() {
155 String deviceStatus = this.getDeviceStatus();
156 byte stateValue = (byte) VeluxGatewayState.GW_S_GWM.getStateValue();
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();
163 subStateValue = (byte) VeluxGatewaySubState.GW_SS_P2.getStateValue();
165 return new VeluxGwState(stateValue, subStateValue);