]> git.basschouten.com Git - openhab-addons.git/blob
9ac668558bbe780cf124ebc3700eab314bc1aa98
[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 import java.util.Set;
17
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;
23
24 import com.google.gson.Gson;
25
26 /**
27  * @author Danny Baumann - Initial contribution
28  */
29 @NonNullByDefault
30 public class DeviceInfo {
31     private static final Set<String> ERROR_ATTR_NAMES = Set.of("code", "error", "errno", "errs");
32
33     public static int parseBatteryInfo(String xml) throws DataParsingException {
34         Node batteryAttr = XPathUtils.getFirstXPathMatch(xml, "//battery/@power");
35         return Integer.valueOf(batteryAttr.getNodeValue());
36     }
37
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);
41         if (mode == null) {
42             throw new IllegalArgumentException("Could not parse charge mode " + modeString);
43         }
44         return mode;
45     }
46
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()) {
51                 try {
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);
56                 }
57             }
58         }
59         return Optional.empty();
60     }
61
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);
74         }
75         return 0;
76     }
77
78     public static boolean parseEnabledStateInfo(String xml) throws DataParsingException {
79         String value = XPathUtils.getFirstXPathMatch(xml, "//@on").getNodeValue();
80         try {
81             return Integer.valueOf(value) != 0;
82         } catch (NumberFormatException e) {
83             throw new DataParsingException(e);
84         }
85     }
86
87     private static Optional<Integer> nodeValueToInt(String xml, String attrName) throws DataParsingException {
88         try {
89             return XPathUtils.getFirstXPathMatchOpt(xml, "//ctl/@" + attrName)
90                     .map(n -> Integer.valueOf(n.getNodeValue()));
91         } catch (NumberFormatException e) {
92             throw new DataParsingException(e);
93         }
94     }
95 }