2 * Copyright (c) 2010-2021 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;
45 * A sub-response structure that is part of a {@link LxResponse} class and contains a response to a command sent to
46 * Miniserver's control.
48 * @author Pawel Pieczul - initial contribution
51 private class LxSubResponse {
52 @SerializedName("control")
53 private String command;
54 @SerializedName(value = "Code", alternate = { "code" })
56 private JsonElement value;
58 private boolean isSubResponseOk() {
59 return (getResponseCode() == LxErrorCode.OK) && (command != null) && (value != null);
62 private LxErrorCode getResponseCode() {
63 return LxErrorCode.getErrorCode(code);
68 public LxSubResponse subResponse;
69 private final Logger logger = LoggerFactory.getLogger(LxResponse.class);
72 * Return true when response has correct syntax and return code was successful
74 * @return true when response is ok
76 public boolean isResponseOk() {
77 return (subResponse != null && subResponse.isSubResponseOk());
81 * Gets command to which this response relates
83 * @return command name
85 public String getCommand() {
86 return (subResponse != null ? subResponse.command : null);
90 * Gets error code from the response in numerical form or null if absent
92 * @return error code value
94 public Integer getResponseCodeNumber() {
95 return (subResponse != null ? subResponse.code : null);
99 * Gets error code from the response as an enumerated value
103 public LxErrorCode getResponseCode() {
104 return LxErrorCode.getErrorCode(getResponseCodeNumber());
108 * Gets response value as a string
110 * @return response value as string
112 public String getValueAsString() {
113 return (subResponse != null && subResponse.value != null ? subResponse.value.getAsString() : null);
117 * Deserializes response value as a given type
119 * @param gson GSON object used for deserialization
120 * @param <T> class to deserialize response to
121 * @param type class type to deserialize response to
122 * @return deserialized response
124 public <T> T getValueAs(Gson gson, Type type) {
125 if (subResponse != null && subResponse.value != null) {
127 return gson.fromJson(subResponse.value, type);
128 } catch (NumberFormatException | JsonParseException e) {
129 logger.debug("Error parsing response value as {}: {}", type, e.getMessage());