]> git.basschouten.com Git - openhab-addons.git/blob
66b23e65238efd8dc28241c175557f881afb05fc
[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 org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.binding.ecovacs.internal.api.impl.ProtocolVersion;
17 import org.openhab.binding.ecovacs.internal.api.impl.dto.response.deviceapi.json.CleanReport;
18 import org.openhab.binding.ecovacs.internal.api.impl.dto.response.deviceapi.json.CleanReportV2;
19 import org.openhab.binding.ecovacs.internal.api.impl.dto.response.deviceapi.xml.CleaningInfo;
20 import org.openhab.binding.ecovacs.internal.api.impl.dto.response.portal.AbstractPortalIotCommandResponse;
21 import org.openhab.binding.ecovacs.internal.api.impl.dto.response.portal.PortalIotCommandJsonResponse;
22 import org.openhab.binding.ecovacs.internal.api.impl.dto.response.portal.PortalIotCommandXmlResponse;
23 import org.openhab.binding.ecovacs.internal.api.model.CleanMode;
24 import org.openhab.binding.ecovacs.internal.api.util.DataParsingException;
25
26 import com.google.gson.Gson;
27
28 /**
29  * @author Danny Baumann - Initial contribution
30  */
31 @NonNullByDefault
32 public class GetCleanStateCommand extends IotDeviceCommand<CleanMode> {
33     public GetCleanStateCommand() {
34         super();
35     }
36
37     @Override
38     public String getName(ProtocolVersion version) {
39         switch (version) {
40             case XML:
41                 return "GetCleanState";
42             case JSON:
43                 return "getCleanInfo";
44             case JSON_V2:
45                 return "getCleanInfo_V2";
46         }
47         throw new AssertionError();
48     }
49
50     @Override
51     public CleanMode convertResponse(AbstractPortalIotCommandResponse response, ProtocolVersion version, Gson gson)
52             throws DataParsingException {
53         if (response instanceof PortalIotCommandJsonResponse) {
54             final PortalIotCommandJsonResponse jsonResponse = (PortalIotCommandJsonResponse) response;
55             final CleanMode mode;
56             if (version == ProtocolVersion.JSON) {
57                 CleanReport resp = jsonResponse.getResponsePayloadAs(gson, CleanReport.class);
58                 mode = resp.determineCleanMode(gson);
59             } else {
60                 CleanReportV2 resp = jsonResponse.getResponsePayloadAs(gson, CleanReportV2.class);
61                 mode = resp.determineCleanMode(gson);
62             }
63             if (mode == null) {
64                 throw new DataParsingException("Could not get clean mode from response " + jsonResponse.response);
65             }
66             return mode;
67         } else {
68             String payload = ((PortalIotCommandXmlResponse) response).getResponsePayloadXml();
69             return CleaningInfo.parseCleanStateInfo(payload, gson).mode;
70         }
71     }
72 }