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.ecovacs.internal.api.impl.dto.response.portal;
15 import org.eclipse.jdt.annotation.Nullable;
16 import org.openhab.binding.ecovacs.internal.api.util.DataParsingException;
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;
24 * @author Danny Baumann - Initial contribution
26 public class PortalIotCommandJsonResponse extends AbstractPortalIotCommandResponse {
27 @SerializedName("resp")
28 public final JsonElement response;
30 public PortalIotCommandJsonResponse(String result, JsonElement response, int errorCode, String errorMessage) {
31 super(result, errorCode, errorMessage);
32 this.response = response;
35 public <T> T getResponsePayloadAs(Gson gson, Class<T> clazz) throws DataParsingException {
37 JsonElement payloadRaw = getResponsePayload(gson);
39 T payload = gson.fromJson(payloadRaw, clazz);
40 if (payload == null) {
41 throw new DataParsingException("Empty JSON payload");
44 } catch (JsonSyntaxException e) {
45 throw new DataParsingException(e);
49 public JsonElement getResponsePayload(Gson gson) throws DataParsingException {
52 JsonResponsePayloadWrapper wrapper = gson.fromJson(response, JsonResponsePayloadWrapper.class);
53 if (wrapper == null) {
54 throw new DataParsingException("Empty JSON payload");
56 return wrapper.body.payload;
57 } catch (JsonSyntaxException e) {
58 throw new DataParsingException(e);
62 public static class JsonPayloadHeader {
63 @SerializedName("pri")
66 public long timestamp;
67 @SerializedName("tzm")
69 @SerializedName("fwVer")
70 public String firmwareVersion;
71 @SerializedName("hwVer")
72 public String hardwareVersion;
75 public static class JsonResponsePayloadWrapper {
76 @SerializedName("header")
77 public JsonPayloadHeader header;
78 @SerializedName("body")
79 public JsonResponsePayloadBody body;
82 public static class JsonResponsePayloadBody {
83 @SerializedName("code")
85 @SerializedName("msg")
86 public String message;
87 @SerializedName("data")
88 public JsonElement payload;