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;
15 import java.util.Optional;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.ecovacs.internal.api.EcovacsDevice;
19 import org.openhab.binding.ecovacs.internal.api.EcovacsDevice.EventListener;
20 import org.openhab.binding.ecovacs.internal.api.impl.dto.response.deviceapi.xml.CleaningInfo;
21 import org.openhab.binding.ecovacs.internal.api.impl.dto.response.deviceapi.xml.DeviceInfo;
22 import org.openhab.binding.ecovacs.internal.api.impl.dto.response.deviceapi.xml.WaterSystemInfo;
23 import org.openhab.binding.ecovacs.internal.api.model.ChargeMode;
24 import org.openhab.binding.ecovacs.internal.api.model.CleanMode;
25 import org.openhab.binding.ecovacs.internal.api.util.DataParsingException;
26 import org.openhab.binding.ecovacs.internal.api.util.XPathUtils;
27 import org.slf4j.Logger;
28 import org.w3c.dom.Node;
30 import com.google.gson.Gson;
33 * @author Danny Baumann - Initial contribution
36 class XmlReportParser implements ReportParser {
37 private final EcovacsDevice device;
38 private final EventListener listener;
39 private final Gson gson;
40 private final Logger logger;
42 XmlReportParser(EcovacsDevice device, EventListener listener, Gson gson, Logger logger) {
44 this.listener = listener;
50 public void handleMessage(String eventName, String payload) throws DataParsingException {
51 switch (eventName.toLowerCase()) {
53 listener.onBatteryLevelUpdated(device, DeviceInfo.parseBatteryInfo(payload));
56 ChargeMode mode = DeviceInfo.parseChargeInfo(payload, gson);
57 if (mode == ChargeMode.RETURNING) {
58 listener.onCleaningModeUpdated(device, CleanMode.RETURNING, Optional.empty());
60 listener.onChargingStateUpdated(device, mode == ChargeMode.CHARGING);
64 CleaningInfo.CleanStateInfo info = CleaningInfo.parseCleanStateInfo(payload, gson);
65 if (info.mode == CleanMode.CUSTOM_AREA) {
66 logger.debug("{}: Custom area cleaning stated with area definition {}", device.getSerialNumber(),
69 listener.onCleaningModeUpdated(device, info.mode, info.areaDefinition);
71 // <ctl td='CleanReport'><clean type='auto' speed='standard' st='s' rsn='a'/></ctl>
74 case "cleanrptbgdata": {
75 Node fromChargerNode = XPathUtils.getFirstXPathMatch(payload, "//@IsFrmCharger");
76 if ("1".equals(fromChargerNode.getNodeValue())) {
77 // Device just started cleaning, but likely won't send us a ChargeState report,
78 // so update charging state from here
79 listener.onChargingStateUpdated(device, false);
82 // <ctl td='CleanRptBgdata' ts='1643044172' Battery='102' CleanID='1333688018' iCleanID='0497265223'
83 // MapID='1430814334' rsn='a' IsFrmCharger='1' CleanType='auto' Speed='standard' OnOffRag='0'
84 // WorkMode='s' Spray='2' WorkArea='002'/>
88 String area = XPathUtils.getFirstXPathMatch(payload, "//@a").getNodeValue();
89 String duration = XPathUtils.getFirstXPathMatch(payload, "//@l").getNodeValue();
90 listener.onCleaningStatsUpdated(device, Integer.valueOf(area), Integer.valueOf(duration));
94 DeviceInfo.parseErrorInfo(payload).ifPresent(errorCode -> {
95 listener.onErrorReported(device, errorCode);
99 listener.onWaterSystemPresentUpdated(device, WaterSystemInfo.parseWaterBoxInfo(payload));