]> git.basschouten.com Git - openhab-addons.git/blob
595e7b15d47fbbb372e291f6e17db34b490eceab
[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.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     }
43
44     /**
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.
47      *
48      * @author Pawel Pieczul - initial contribution
49      *
50      */
51     private class LxSubResponse {
52         @SerializedName("control")
53         private String command;
54         @SerializedName(value = "Code", alternate = { "code" })
55         private Integer code;
56         private JsonElement value;
57
58         private boolean isSubResponseOk() {
59             return (getResponseCode() == LxErrorCode.OK) && (command != null) && (value != null);
60         }
61
62         private LxErrorCode getResponseCode() {
63             return LxErrorCode.getErrorCode(code);
64         }
65     }
66
67     @SerializedName("LL")
68     public LxSubResponse subResponse;
69     private final Logger logger = LoggerFactory.getLogger(LxResponse.class);
70
71     /**
72      * Return true when response has correct syntax and return code was successful
73      *
74      * @return true when response is ok
75      */
76     public boolean isResponseOk() {
77         return (subResponse != null && subResponse.isSubResponseOk());
78     }
79
80     /**
81      * Gets command to which this response relates
82      *
83      * @return command name
84      */
85     public String getCommand() {
86         return (subResponse != null ? subResponse.command : null);
87     }
88
89     /**
90      * Gets error code from the response in numerical form or null if absent
91      *
92      * @return error code value
93      */
94     public Integer getResponseCodeNumber() {
95         return (subResponse != null ? subResponse.code : null);
96     }
97
98     /**
99      * Gets error code from the response as an enumerated value
100      *
101      * @return error code
102      */
103     public LxErrorCode getResponseCode() {
104         return LxErrorCode.getErrorCode(getResponseCodeNumber());
105     }
106
107     /**
108      * Gets response value as a string
109      *
110      * @return response value as string
111      */
112     public String getValueAsString() {
113         return (subResponse != null && subResponse.value != null ? subResponse.value.getAsString() : null);
114     }
115
116     /**
117      * Deserializes response value as a given type
118      *
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
123      */
124     public <T> T getValueAs(Gson gson, Type type) {
125         if (subResponse != null && subResponse.value != null) {
126             try {
127                 return gson.fromJson(subResponse.value, type);
128             } catch (NumberFormatException | JsonParseException e) {
129                 logger.debug("Error parsing response value as {}: {}", type, e.getMessage());
130             }
131         }
132         return null;
133     }
134 }