]> git.basschouten.com Git - openhab-addons.git/blob
aa796bec53071faf95807c166e73d23eff8f3482
[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.util.Optional;
16
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;
23
24 import com.google.gson.JsonElement;
25 import com.google.gson.JsonObject;
26
27 /**
28  * @author Danny Baumann - Initial contribution
29  */
30 @NonNullByDefault
31 abstract class AbstractCleaningCommand extends AbstractNoResponseCommand {
32     private final String xmlAction;
33     private final String jsonAction;
34     private final Optional<CleanMode> mode;
35
36     protected AbstractCleaningCommand(String xmlAction, String jsonAction, @Nullable CleanMode mode) {
37         super();
38         this.xmlAction = xmlAction;
39         this.jsonAction = jsonAction;
40         this.mode = Optional.ofNullable(mode);
41     }
42
43     @Override
44     public String getName(ProtocolVersion version) {
45         switch (version) {
46             case XML:
47                 return "Clean";
48             case JSON:
49                 return "clean";
50             case JSON_V2:
51                 return "clean_V2";
52         }
53         throw new AssertionError();
54     }
55
56     @Override
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);
63     }
64
65     @Override
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);
74                 payload = content;
75             }
76             payload.addProperty("type", m);
77         });
78         return args;
79     }
80
81     private Optional<String> getCleanModeProperty(ProtocolVersion version) {
82         return mode.flatMap(m -> {
83             switch (m) {
84                 case AUTO:
85                     return Optional.of("auto");
86                 case CUSTOM_AREA:
87                     return Optional.of(version == ProtocolVersion.XML ? "CustomArea" : "customArea");
88                 case EDGE:
89                     return Optional.of("border");
90                 case SPOT:
91                     return Optional.of("spot");
92                 case SPOT_AREA:
93                     return Optional.of(version == ProtocolVersion.XML ? "SpotArea" : "spotArea");
94                 case SINGLE_ROOM:
95                     return Optional.of("singleRoom");
96                 case STOP:
97                     return Optional.of("stop");
98                 default:
99                     return Optional.empty();
100             }
101         });
102     }
103 }