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.util.Optional;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.ecovacs.internal.api.impl.ProtocolVersion;
20 import org.openhab.binding.ecovacs.internal.api.model.CleanMode;
21 import org.w3c.dom.Document;
22 import org.w3c.dom.Element;
24 import com.google.gson.JsonElement;
25 import com.google.gson.JsonObject;
28 * @author Danny Baumann - Initial contribution
31 abstract class AbstractCleaningCommand extends AbstractNoResponseCommand {
32 private final String xmlAction;
33 private final String jsonAction;
34 private final Optional<CleanMode> mode;
36 protected AbstractCleaningCommand(String xmlAction, String jsonAction, @Nullable CleanMode mode) {
38 this.xmlAction = xmlAction;
39 this.jsonAction = jsonAction;
40 this.mode = Optional.ofNullable(mode);
44 public String getName(ProtocolVersion version) {
53 throw new AssertionError();
57 protected void applyXmlPayload(Document doc, Element ctl) {
58 Element clean = doc.createElement("clean");
59 getCleanModeProperty(ProtocolVersion.XML).ifPresent(m -> clean.setAttribute("type", m));
60 clean.setAttribute("speed", "standard");
61 clean.setAttribute("act", xmlAction);
62 ctl.appendChild(clean);
66 protected @Nullable JsonElement getJsonPayloadArgs(ProtocolVersion version) {
67 JsonObject args = new JsonObject();
68 args.addProperty("act", jsonAction);
69 getCleanModeProperty(version).ifPresent(m -> {
70 JsonObject payload = args;
71 if (version == ProtocolVersion.JSON_V2) {
72 JsonObject content = new JsonObject();
73 args.add("content", content);
76 payload.addProperty("type", m);
81 private Optional<String> getCleanModeProperty(ProtocolVersion version) {
82 return mode.flatMap(m -> {
85 return Optional.of("auto");
87 return Optional.of(version == ProtocolVersion.XML ? "CustomArea" : "customArea");
89 return Optional.of("border");
91 return Optional.of("spot");
93 return Optional.of(version == ProtocolVersion.XML ? "SpotArea" : "spotArea");
95 return Optional.of("singleRoom");
97 return Optional.of("stop");
99 return Optional.empty();