]> git.basschouten.com Git - openhab-addons.git/blob
29627a85108fc905cbec7b5c0739286a4304cf69
[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.ecovacs.internal.api.impl.dto.response.portal;
14
15 import org.eclipse.jdt.annotation.Nullable;
16 import org.openhab.binding.ecovacs.internal.api.util.DataParsingException;
17
18 import com.google.gson.Gson;
19 import com.google.gson.JsonElement;
20 import com.google.gson.JsonSyntaxException;
21 import com.google.gson.annotations.SerializedName;
22
23 /**
24  * @author Danny Baumann - Initial contribution
25  */
26 public class PortalIotCommandJsonResponse extends AbstractPortalIotCommandResponse {
27     @SerializedName("resp")
28     public final JsonElement response;
29
30     public PortalIotCommandJsonResponse(String result, JsonElement response, int errorCode, String errorMessage) {
31         super(result, errorCode, errorMessage);
32         this.response = response;
33     }
34
35     public <T> T getResponsePayloadAs(Gson gson, Class<T> clazz) throws DataParsingException {
36         try {
37             JsonElement payloadRaw = getResponsePayload(gson);
38             @Nullable
39             T payload = gson.fromJson(payloadRaw, clazz);
40             if (payload == null) {
41                 throw new DataParsingException("Empty JSON payload");
42             }
43             return payload;
44         } catch (JsonSyntaxException e) {
45             throw new DataParsingException(e);
46         }
47     }
48
49     public JsonElement getResponsePayload(Gson gson) throws DataParsingException {
50         try {
51             @Nullable
52             JsonResponsePayloadWrapper wrapper = gson.fromJson(response, JsonResponsePayloadWrapper.class);
53             if (wrapper == null) {
54                 throw new DataParsingException("Empty JSON payload");
55             }
56             return wrapper.body.payload;
57         } catch (JsonSyntaxException e) {
58             throw new DataParsingException(e);
59         }
60     }
61
62     public static class JsonPayloadHeader {
63         @SerializedName("pri")
64         public int pri;
65         @SerializedName("ts")
66         public long timestamp;
67         @SerializedName("tzm")
68         public int tzm;
69         @SerializedName("fwVer")
70         public String firmwareVersion;
71         @SerializedName("hwVer")
72         public String hardwareVersion;
73     }
74
75     public static class JsonResponsePayloadWrapper {
76         @SerializedName("header")
77         public JsonPayloadHeader header;
78         @SerializedName("body")
79         public JsonResponsePayloadBody body;
80     }
81
82     public static class JsonResponsePayloadBody {
83         @SerializedName("code")
84         public int code;
85         @SerializedName("msg")
86         public String message;
87         @SerializedName("data")
88         public JsonElement payload;
89     }
90 }