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;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.binding.ecovacs.internal.api.model.ChargeMode;
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 DeviceInfo {
31 private static final Set<String> ERROR_ATTR_NAMES = Set.of("code", "error", "errno", "errs");
33 public static int parseBatteryInfo(String xml) throws DataParsingException {
34 Node batteryAttr = XPathUtils.getFirstXPathMatch(xml, "//battery/@power");
35 return Integer.valueOf(batteryAttr.getNodeValue());
38 public static ChargeMode parseChargeInfo(String xml, Gson gson) throws DataParsingException {
39 String modeString = XPathUtils.getFirstXPathMatch(xml, "//charge/@type").getNodeValue();
40 ChargeMode mode = gson.fromJson(modeString, ChargeMode.class);
42 throw new IllegalArgumentException("Could not parse charge mode " + modeString);
47 public static Optional<Integer> parseErrorInfo(String xml) throws DataParsingException {
48 for (String attr : ERROR_ATTR_NAMES) {
49 Optional<Node> node = XPathUtils.getFirstXPathMatchOpt(xml, "//@" + attr);
50 if (node.isPresent()) {
52 String value = node.get().getNodeValue();
53 return value.isEmpty() ? Optional.empty() : Optional.of(Integer.valueOf(value));
54 } catch (NumberFormatException e) {
55 throw new DataParsingException(e);
59 return Optional.empty();
62 public static int parseComponentLifespanInfo(String xml) throws DataParsingException {
63 Optional<Integer> value = nodeValueToInt(xml, "value");
64 Optional<Integer> total = nodeValueToInt(xml, "total");
65 Optional<Integer> left = nodeValueToInt(xml, "left");
66 if (value.isPresent() && total.isPresent()) {
67 return (int) Math.round(100.0 * value.get() / total.get());
68 } else if (value.isPresent()) {
69 return (int) Math.round(0.01 * value.get());
70 } else if (left.isPresent() && total.isPresent()) {
71 return (int) Math.round(100.0 * left.get() / total.get());
72 } else if (left.isPresent()) {
73 return (int) Math.round((double) left.get() / 60.0);
78 public static boolean parseEnabledStateInfo(String xml) throws DataParsingException {
79 String value = XPathUtils.getFirstXPathMatch(xml, "//@on").getNodeValue();
81 return Integer.valueOf(value) != 0;
82 } catch (NumberFormatException e) {
83 throw new DataParsingException(e);
87 private static Optional<Integer> nodeValueToInt(String xml, String attrName) throws DataParsingException {
89 return XPathUtils.getFirstXPathMatchOpt(xml, "//ctl/@" + attrName)
90 .map(n -> Integer.valueOf(n.getNodeValue()));
91 } catch (NumberFormatException e) {
92 throw new DataParsingException(e);