]> git.basschouten.com Git - openhab-addons.git/blob
a4dce40e367dc32bc7417deaf734a02ae5e05232
[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.deviceapi.xml;
14
15 import java.util.Optional;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.ecovacs.internal.api.model.CleanMode;
19 import org.openhab.binding.ecovacs.internal.api.model.SuctionPower;
20 import org.openhab.binding.ecovacs.internal.api.util.DataParsingException;
21 import org.openhab.binding.ecovacs.internal.api.util.XPathUtils;
22 import org.w3c.dom.Node;
23
24 import com.google.gson.Gson;
25
26 /**
27  * @author Danny Baumann - Initial contribution
28  */
29 @NonNullByDefault
30 public class CleaningInfo {
31     public static class CleanStateInfo {
32         public final CleanMode mode;
33         public final Optional<String> areaDefinition;
34
35         CleanStateInfo(CleanMode mode) {
36             this(mode, Optional.empty());
37         }
38
39         CleanStateInfo(CleanMode mode, Optional<String> areaDefinition) {
40             this.mode = mode;
41             this.areaDefinition = areaDefinition;
42         }
43     }
44
45     public static CleanStateInfo parseCleanStateInfo(String xml, Gson gson) throws DataParsingException {
46         String stateString = XPathUtils.getFirstXPathMatchOpt(xml, "//clean/@st").map(n -> n.getNodeValue()).orElse("");
47
48         if ("h".equals(stateString)) {
49             return new CleanStateInfo(CleanMode.STOP);
50         } else if ("p".equals(stateString)) {
51             return new CleanStateInfo(CleanMode.PAUSE);
52         } else {
53             String modeString = XPathUtils.getFirstXPathMatch(xml, "//clean/@type").getNodeValue();
54             CleanMode parsedMode = gson.fromJson(modeString, CleanMode.class);
55             if (parsedMode == CleanMode.SPOT_AREA) {
56                 Optional<Node> pointOpt = XPathUtils.getFirstXPathMatchOpt(xml, "//clean/@p");
57                 if (pointOpt.isPresent()) {
58                     return new CleanStateInfo(CleanMode.CUSTOM_AREA, pointOpt.map(n -> n.getNodeValue()));
59                 }
60                 Optional<Node> midOpt = XPathUtils.getFirstXPathMatchOpt(xml, "//clean/@mid");
61                 return new CleanStateInfo(CleanMode.SPOT_AREA, midOpt.map(n -> n.getNodeValue()));
62             }
63             if (parsedMode != null) {
64                 return new CleanStateInfo(parsedMode);
65             }
66         }
67         throw new DataParsingException("Unexpected clean state report: " + xml);
68     }
69
70     public static SuctionPower parseCleanSpeedInfo(String xml, Gson gson) throws DataParsingException {
71         String levelString = XPathUtils.getFirstXPathMatch(xml, "//@speed").getNodeValue();
72         SuctionPower level = gson.fromJson(levelString, SuctionPower.class);
73         if (level == null) {
74             throw new DataParsingException("Could not parse power level " + levelString);
75         }
76         return level;
77     }
78 }