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.commands;
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;
21 import javax.xml.parsers.DocumentBuilder;
22 import javax.xml.parsers.DocumentBuilderFactory;
23 import javax.xml.parsers.ParserConfigurationException;
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;
38 import com.google.gson.Gson;
41 * @author Danny Baumann - Initial contribution
44 public class GetCleanLogsCommand extends IotDeviceCommand<List<CleanLogRecord>> {
45 private static final int LOG_SIZE = 20;
48 public String getName(ProtocolVersion version) {
49 if (version != ProtocolVersion.XML) {
50 throw new IllegalStateException("Command is only supported for XML");
52 return "GetCleanLogs";
56 protected void applyXmlPayload(Document doc, Element ctl) {
57 ctl.setAttribute("count", String.valueOf(LOG_SIZE));
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();
66 DocumentBuilder db = dbf.newDocumentBuilder();
67 NodeList entryNodes = db.parse(new ByteArrayInputStream(payload.getBytes("UTF-8"))).getFirstChild()
69 List<CleanLogRecord> result = new ArrayList<>();
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();
77 result.add(new CleanLogRecord(Long.parseLong(startTime), Integer.parseInt(duration),
78 Integer.parseInt(area), Optional.empty(), CleanMode.IDLE));
81 } catch (ParserConfigurationException | SAXException | IOException e) {
82 throw new DataParsingException(e);