]> git.basschouten.com Git - openhab-addons.git/blob
f21def91588b2c7f185933cafda50e922c7a94a1
[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.loxone.internal.types;
14
15 import java.lang.reflect.Type;
16
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
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;
24
25 /**
26  * Response to a command sent to Miniserver's control.
27  *
28  * @author Pawel Pieczul - initial contribution
29  *
30  */
31 public class LxResponse {
32
33     /**
34      * A sub-response value structure that is received as a response to config API HTTP request sent to the Miniserver.
35      *
36      * @author Pawel Pieczul - initial contribution
37      *
38      */
39     public class LxResponseCfgApi {
40         public String snr;
41         public String version;
42         public String key;
43         public Integer httpsStatus;
44     }
45
46     /**
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.
49      *
50      * @author Pawel Pieczul - initial contribution
51      *
52      */
53     private class LxSubResponse {
54         @SerializedName("control")
55         private String command;
56         @SerializedName(value = "Code", alternate = { "code" })
57         private Integer code;
58         private JsonElement value;
59
60         private boolean isSubResponseOk() {
61             return (getResponseCode() == LxErrorCode.OK) && (command != null) && (value != null);
62         }
63
64         private LxErrorCode getResponseCode() {
65             return LxErrorCode.getErrorCode(code);
66         }
67     }
68
69     @SerializedName("LL")
70     public LxSubResponse subResponse;
71     private final Logger logger = LoggerFactory.getLogger(LxResponse.class);
72
73     /**
74      * Return true when response has correct syntax and return code was successful
75      *
76      * @return true when response is ok
77      */
78     public boolean isResponseOk() {
79         return (subResponse != null && subResponse.isSubResponseOk());
80     }
81
82     /**
83      * Gets command to which this response relates
84      *
85      * @return command name
86      */
87     public String getCommand() {
88         return (subResponse != null ? subResponse.command : null);
89     }
90
91     /**
92      * Gets error code from the response in numerical form or null if absent
93      *
94      * @return error code value
95      */
96     public Integer getResponseCodeNumber() {
97         return (subResponse != null ? subResponse.code : null);
98     }
99
100     /**
101      * Gets error code from the response as an enumerated value
102      *
103      * @return error code
104      */
105     public LxErrorCode getResponseCode() {
106         return LxErrorCode.getErrorCode(getResponseCodeNumber());
107     }
108
109     /**
110      * Gets response value as a string
111      *
112      * @return response value as string
113      */
114     public String getValueAsString() {
115         return (subResponse != null && subResponse.value != null ? subResponse.value.getAsString() : null);
116     }
117
118     /**
119      * Deserializes response value as a given type
120      *
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
125      */
126     public <T> T getValueAs(Gson gson, Type type) {
127         if (subResponse != null && subResponse.value != null) {
128             try {
129                 return gson.fromJson(subResponse.value, type);
130             } catch (NumberFormatException | JsonParseException e) {
131                 logger.debug("Error parsing response value as {}: {}", type, e.getMessage());
132             }
133         }
134         return null;
135     }
136 }