]> git.basschouten.com Git - openhab-addons.git/blob
be9f7a0b7ec8f133eb0f5a1778a726e552704b91
[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;
14
15 import java.util.Optional;
16
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;
29
30 import com.google.gson.Gson;
31
32 /**
33  * @author Danny Baumann - Initial contribution
34  */
35 @NonNullByDefault
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;
41
42     XmlReportParser(EcovacsDevice device, EventListener listener, Gson gson, Logger logger) {
43         this.device = device;
44         this.listener = listener;
45         this.gson = gson;
46         this.logger = logger;
47     }
48
49     @Override
50     public void handleMessage(String eventName, String payload) throws DataParsingException {
51         switch (eventName.toLowerCase()) {
52             case "batteryinfo":
53                 listener.onBatteryLevelUpdated(device, DeviceInfo.parseBatteryInfo(payload));
54                 break;
55             case "chargestate": {
56                 ChargeMode mode = DeviceInfo.parseChargeInfo(payload, gson);
57                 if (mode == ChargeMode.RETURNING) {
58                     listener.onCleaningModeUpdated(device, CleanMode.RETURNING, Optional.empty());
59                 }
60                 listener.onChargingStateUpdated(device, mode == ChargeMode.CHARGING);
61                 break;
62             }
63             case "cleanreport": {
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(),
67                             info.areaDefinition);
68                 }
69                 listener.onCleaningModeUpdated(device, info.mode, info.areaDefinition);
70                 // Full report:
71                 // <ctl td='CleanReport'><clean type='auto' speed='standard' st='s' rsn='a'/></ctl>
72                 break;
73             }
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);
80                 }
81                 // Full report:
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'/>
85                 break;
86             }
87             case "cleanst": {
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));
91                 break;
92             }
93             case "error":
94                 DeviceInfo.parseErrorInfo(payload).ifPresent(errorCode -> {
95                     listener.onErrorReported(device, errorCode);
96                 });
97                 break;
98             case "waterboxinfo":
99                 listener.onWaterSystemPresentUpdated(device, WaterSystemInfo.parseWaterBoxInfo(payload));
100                 break;
101         }
102     }
103 }