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.loxone.internal.types;
15 import java.lang.reflect.Type;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
20 import com.google.gson.Gson;
21 import com.google.gson.JsonElement;
22 import com.google.gson.JsonParseException;
23 import com.google.gson.annotations.SerializedName;
26 * Response to a command sent to Miniserver's control.
28 * @author Pawel Pieczul - initial contribution
31 public class LxResponse {
34 * A sub-response value structure that is received as a response to config API HTTP request sent to the Miniserver.
36 * @author Pawel Pieczul - initial contribution
39 public class LxResponseCfgApi {
41 public String version;
43 public Integer httpsStatus;
47 * A sub-response structure that is part of a {@link LxResponse} class and contains a response to a command sent to
48 * Miniserver's control.
50 * @author Pawel Pieczul - initial contribution
53 private class LxSubResponse {
54 @SerializedName("control")
55 private String command;
56 @SerializedName(value = "Code", alternate = { "code" })
58 private JsonElement value;
60 private boolean isSubResponseOk() {
61 return (getResponseCode() == LxErrorCode.OK) && (command != null) && (value != null);
64 private LxErrorCode getResponseCode() {
65 return LxErrorCode.getErrorCode(code);
70 public LxSubResponse subResponse;
71 private final Logger logger = LoggerFactory.getLogger(LxResponse.class);
74 * Return true when response has correct syntax and return code was successful
76 * @return true when response is ok
78 public boolean isResponseOk() {
79 return (subResponse != null && subResponse.isSubResponseOk());
83 * Gets command to which this response relates
85 * @return command name
87 public String getCommand() {
88 return (subResponse != null ? subResponse.command : null);
92 * Gets error code from the response in numerical form or null if absent
94 * @return error code value
96 public Integer getResponseCodeNumber() {
97 return (subResponse != null ? subResponse.code : null);
101 * Gets error code from the response as an enumerated value
105 public LxErrorCode getResponseCode() {
106 return LxErrorCode.getErrorCode(getResponseCodeNumber());
110 * Gets response value as a string
112 * @return response value as string
114 public String getValueAsString() {
115 return (subResponse != null && subResponse.value != null ? subResponse.value.getAsString() : null);
119 * Deserializes response value as a given type
121 * @param gson GSON object used for deserialization
122 * @param <T> class to deserialize response to
123 * @param type class type to deserialize response to
124 * @return deserialized response
126 public <T> T getValueAs(Gson gson, Type type) {
127 if (subResponse != null && subResponse.value != null) {
129 return gson.fromJson(subResponse.value, type);
130 } catch (NumberFormatException | JsonParseException e) {
131 logger.debug("Error parsing response value as {}: {}", type, e.getMessage());