]> git.basschouten.com Git - openhab-addons.git/blob
b190d8662054ca208e05dfec21b66fecff2d36d0
[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.commands;
14
15 import java.lang.reflect.Type;
16 import java.util.List;
17
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;
30
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;
36
37 /**
38  * @author Danny Baumann - Initial contribution
39  */
40 @NonNullByDefault
41 public class GetComponentLifeSpanCommand extends IotDeviceCommand<Integer> {
42     private final Component type;
43
44     public GetComponentLifeSpanCommand(Component type) {
45         this.type = type;
46     }
47
48     @Override
49     public String getName(ProtocolVersion version) {
50         return version == ProtocolVersion.XML ? "GetLifeSpan" : "getLifeSpan";
51     }
52
53     @Override
54     protected void applyXmlPayload(Document doc, Element ctl) {
55         ctl.setAttribute("type", type.xmlValue);
56     }
57
58     @Override
59     protected @Nullable JsonElement getJsonPayloadArgs(ProtocolVersion version) {
60         JsonArray args = new JsonArray(1);
61         args.add(type.jsonValue);
62         return args;
63     }
64
65     @Override
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>>() {
71             }.getType();
72             try {
73                 List<ComponentLifeSpanReport> resp = gson.fromJson(respPayloadRaw, type);
74                 if (resp == null || resp.isEmpty()) {
75                     throw new DataParsingException("Invalid lifespan response " + respPayloadRaw);
76                 }
77                 return (int) Math.round(100.0 * resp.get(0).left / resp.get(0).total);
78             } catch (JsonSyntaxException e) {
79                 throw new DataParsingException(e);
80             }
81         } else {
82             String payload = ((PortalIotCommandXmlResponse) response).getResponsePayloadXml();
83             return DeviceInfo.parseComponentLifespanInfo(payload);
84         }
85     }
86 }