]> git.basschouten.com Git - openhab-addons.git/blob
5d2f419d472e61466c3cf1c522c2644875c68ea7
[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 java.io.ByteArrayInputStream;
16 import java.io.IOException;
17 import java.util.ArrayList;
18 import java.util.List;
19 import java.util.Optional;
20
21 import javax.xml.parsers.DocumentBuilder;
22 import javax.xml.parsers.DocumentBuilderFactory;
23 import javax.xml.parsers.ParserConfigurationException;
24
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.openhab.binding.ecovacs.internal.api.impl.ProtocolVersion;
27 import org.openhab.binding.ecovacs.internal.api.impl.dto.response.portal.AbstractPortalIotCommandResponse;
28 import org.openhab.binding.ecovacs.internal.api.impl.dto.response.portal.PortalIotCommandXmlResponse;
29 import org.openhab.binding.ecovacs.internal.api.model.CleanLogRecord;
30 import org.openhab.binding.ecovacs.internal.api.model.CleanMode;
31 import org.openhab.binding.ecovacs.internal.api.util.DataParsingException;
32 import org.w3c.dom.Document;
33 import org.w3c.dom.Element;
34 import org.w3c.dom.NamedNodeMap;
35 import org.w3c.dom.NodeList;
36 import org.xml.sax.SAXException;
37
38 import com.google.gson.Gson;
39
40 /**
41  * @author Danny Baumann - Initial contribution
42  */
43 @NonNullByDefault
44 public class GetCleanLogsCommand extends IotDeviceCommand<List<CleanLogRecord>> {
45     private static final int LOG_SIZE = 20;
46
47     @Override
48     public String getName(ProtocolVersion version) {
49         if (version != ProtocolVersion.XML) {
50             throw new IllegalStateException("Command is only supported for XML");
51         }
52         return "GetCleanLogs";
53     }
54
55     @Override
56     protected void applyXmlPayload(Document doc, Element ctl) {
57         ctl.setAttribute("count", String.valueOf(LOG_SIZE));
58     }
59
60     @Override
61     public List<CleanLogRecord> convertResponse(AbstractPortalIotCommandResponse response, ProtocolVersion version,
62             Gson gson) throws DataParsingException {
63         String payload = ((PortalIotCommandXmlResponse) response).getResponsePayloadXml();
64         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
65         try {
66             DocumentBuilder db = dbf.newDocumentBuilder();
67             NodeList entryNodes = db.parse(new ByteArrayInputStream(payload.getBytes("UTF-8"))).getFirstChild()
68                     .getChildNodes();
69             List<CleanLogRecord> result = new ArrayList<>();
70
71             for (int i = 0; i < entryNodes.getLength(); i++) {
72                 NamedNodeMap attrs = entryNodes.item(i).getAttributes();
73                 String area = attrs.getNamedItem("a").getNodeValue();
74                 String startTime = attrs.getNamedItem("s").getNodeValue();
75                 String duration = attrs.getNamedItem("l").getNodeValue();
76
77                 result.add(new CleanLogRecord(Long.parseLong(startTime), Integer.parseInt(duration),
78                         Integer.parseInt(area), Optional.empty(), CleanMode.IDLE));
79             }
80             return result;
81         } catch (ParserConfigurationException | SAXException | IOException e) {
82             throw new DataParsingException(e);
83         }
84     }
85 }