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.commands;
15 import java.lang.reflect.Type;
16 import java.util.List;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.ecovacs.internal.api.impl.ProtocolVersion;
21 import org.openhab.binding.ecovacs.internal.api.impl.dto.response.deviceapi.json.ComponentLifeSpanReport;
22 import org.openhab.binding.ecovacs.internal.api.impl.dto.response.deviceapi.xml.DeviceInfo;
23 import org.openhab.binding.ecovacs.internal.api.impl.dto.response.portal.AbstractPortalIotCommandResponse;
24 import org.openhab.binding.ecovacs.internal.api.impl.dto.response.portal.PortalIotCommandJsonResponse;
25 import org.openhab.binding.ecovacs.internal.api.impl.dto.response.portal.PortalIotCommandXmlResponse;
26 import org.openhab.binding.ecovacs.internal.api.model.Component;
27 import org.openhab.binding.ecovacs.internal.api.util.DataParsingException;
28 import org.w3c.dom.Document;
29 import org.w3c.dom.Element;
31 import com.google.gson.Gson;
32 import com.google.gson.JsonArray;
33 import com.google.gson.JsonElement;
34 import com.google.gson.JsonSyntaxException;
35 import com.google.gson.reflect.TypeToken;
38 * @author Danny Baumann - Initial contribution
41 public class GetComponentLifeSpanCommand extends IotDeviceCommand<Integer> {
42 private final Component type;
44 public GetComponentLifeSpanCommand(Component type) {
49 public String getName(ProtocolVersion version) {
50 return version == ProtocolVersion.XML ? "GetLifeSpan" : "getLifeSpan";
54 protected void applyXmlPayload(Document doc, Element ctl) {
55 ctl.setAttribute("type", type.xmlValue);
59 protected @Nullable JsonElement getJsonPayloadArgs(ProtocolVersion version) {
60 JsonArray args = new JsonArray(1);
61 args.add(type.jsonValue);
66 public Integer convertResponse(AbstractPortalIotCommandResponse response, ProtocolVersion version, Gson gson)
67 throws DataParsingException {
68 if (response instanceof PortalIotCommandJsonResponse jsonResponse) {
69 JsonElement respPayloadRaw = jsonResponse.getResponsePayload(gson);
70 Type type = new TypeToken<List<ComponentLifeSpanReport>>() {
73 List<ComponentLifeSpanReport> resp = gson.fromJson(respPayloadRaw, type);
74 if (resp == null || resp.isEmpty()) {
75 throw new DataParsingException("Invalid lifespan response " + respPayloadRaw);
77 return (int) Math.round(100.0 * resp.get(0).left / resp.get(0).total);
78 } catch (JsonSyntaxException e) {
79 throw new DataParsingException(e);
82 String payload = ((PortalIotCommandXmlResponse) response).getResponsePayloadXml();
83 return DeviceInfo.parseComponentLifespanInfo(payload);