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.deviceapi.xml;
15 import java.util.Optional;
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;
24 import com.google.gson.Gson;
27 * @author Danny Baumann - Initial contribution
30 public class CleaningInfo {
31 public static class CleanStateInfo {
32 public final CleanMode mode;
33 public final Optional<String> areaDefinition;
35 CleanStateInfo(CleanMode mode) {
36 this(mode, Optional.empty());
39 CleanStateInfo(CleanMode mode, Optional<String> areaDefinition) {
41 this.areaDefinition = areaDefinition;
45 public static CleanStateInfo parseCleanStateInfo(String xml, Gson gson) throws DataParsingException {
46 String stateString = XPathUtils.getFirstXPathMatchOpt(xml, "//clean/@st").map(n -> n.getNodeValue()).orElse("");
48 if ("h".equals(stateString)) {
49 return new CleanStateInfo(CleanMode.STOP);
50 } else if ("p".equals(stateString)) {
51 return new CleanStateInfo(CleanMode.PAUSE);
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()));
60 Optional<Node> midOpt = XPathUtils.getFirstXPathMatchOpt(xml, "//clean/@mid");
61 return new CleanStateInfo(CleanMode.SPOT_AREA, midOpt.map(n -> n.getNodeValue()));
63 if (parsedMode != null) {
64 return new CleanStateInfo(parsedMode);
67 throw new DataParsingException("Unexpected clean state report: " + xml);
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);
74 throw new DataParsingException("Could not parse power level " + levelString);