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.daikin.internal.api;
15 import java.util.HashMap;
17 import java.util.Optional;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.daikin.internal.api.Enums.AdvancedMode;
21 import org.openhab.binding.daikin.internal.api.Enums.FanMovement;
22 import org.openhab.binding.daikin.internal.api.Enums.FanSpeed;
23 import org.openhab.binding.daikin.internal.api.Enums.Mode;
24 import org.openhab.binding.daikin.internal.api.Enums.SpecialMode;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 * Class for holding the set of parameters used by set and get control info.
31 * @author Tim Waterhouse - Initial Contribution
32 * @author Paul Smedley - mods for Daikin Airbase
36 public class ControlInfo {
37 private static final Logger LOGGER = LoggerFactory.getLogger(ControlInfo.class);
39 public String ret = "";
40 public boolean power = false;
41 // Store the accepted auto mode for later use.
42 public int autoModeValue = Mode.AUTO.getValue();
43 public Mode mode = Mode.AUTO;
44 // Degrees in Celsius.
45 public Optional<Double> temp = Optional.empty();
46 public FanSpeed fanSpeed = FanSpeed.AUTO;
47 public FanMovement fanMovement = FanMovement.STOPPED;
48 // Not supported by all units. Sets the target humidity for dehumidifying.
49 public Optional<Integer> targetHumidity = Optional.empty();
50 public AdvancedMode advancedMode = AdvancedMode.UNKNOWN;
51 public boolean separatedDirectionParams = false;
53 private ControlInfo() {
56 public static ControlInfo parse(String response) {
57 LOGGER.trace("Parsing string: \"{}\"", response);
59 Map<String, String> responseMap = InfoParser.parse(response);
61 ControlInfo info = new ControlInfo();
62 info.ret = Optional.ofNullable(responseMap.get("ret")).orElse("");
63 info.power = "1".equals(responseMap.get("pow"));
64 info.mode = Optional.ofNullable(responseMap.get("mode")).flatMap(value -> InfoParser.parseInt(value))
65 .map(value -> Mode.fromValue(value)).orElse(Mode.AUTO);
66 // Normalize AUTO1 and AUTO7 to AUTO
67 if (info.mode == Mode.AUTO1 || info.mode == Mode.AUTO7) {
68 info.autoModeValue = info.mode.getValue();
69 info.mode = Mode.AUTO;
71 info.temp = Optional.ofNullable(responseMap.get("stemp")).flatMap(value -> InfoParser.parseDouble(value));
72 info.fanSpeed = Optional.ofNullable(responseMap.get("f_rate")).map(value -> FanSpeed.fromValue(value))
73 .orElse(FanSpeed.AUTO);
74 // determine if device has combined direction (f_dir) or separated directions (f_dir_ud/f_dir_lr)
75 if (response.contains("f_dir=")) {
76 info.fanMovement = Optional.ofNullable(responseMap.get("f_dir"))
77 .flatMap(value -> InfoParser.parseInt(value)).map(value -> FanMovement.fromValue(value))
78 .orElse(FanMovement.STOPPED);
80 info.separatedDirectionParams = true;
81 String ud = responseMap.get("f_dir_ud");
82 String lr = responseMap.get("f_dir_lr");
83 if (ud != null && lr != null) {
84 info.fanMovement = parseFanMovement(ud, lr);
86 info.fanMovement = FanMovement.UNKNOWN;
90 info.targetHumidity = Optional.ofNullable(responseMap.get("shum")).flatMap(value -> InfoParser.parseInt(value));
92 info.advancedMode = Optional.ofNullable(responseMap.get("adv")).map(value -> AdvancedMode.fromValue(value))
93 .orElse(AdvancedMode.UNKNOWN);
97 public Map<String, String> getParamString() {
98 Map<String, String> params = new HashMap<>();
99 params.put("pow", power ? "1" : "0");
100 params.put("mode", Integer.toString(mode == Mode.AUTO ? autoModeValue : mode.getValue()));
101 params.put("f_rate", fanSpeed.getValue());
102 if (separatedDirectionParams) {
103 params.put("f_dir_lr",
104 fanMovement == FanMovement.VERTICAL || fanMovement == FanMovement.VERTICAL_AND_HORIZONTAL ? "S"
106 params.put("f_dir_ud",
107 fanMovement == FanMovement.HORIZONTAL || fanMovement == FanMovement.VERTICAL_AND_HORIZONTAL ? "S"
110 params.put("f_dir", Integer.toString(fanMovement.getValue()));
112 params.put("stemp", temp.orElse(20.0).toString());
113 params.put("shum", targetHumidity.map(value -> value.toString()).orElse(""));
118 public SpecialMode getSpecialMode() {
119 return SpecialMode.fromAdvancedMode(advancedMode);
123 * This method combines different direction parameters to one.
125 * @param ud up/ down value S or 0
127 * @param lr left/ right value S or 0
129 * @return combined value based on {@link FanMovement} enum
131 public static FanMovement parseFanMovement(String ud, String lr) {
132 if ("S".equals(ud)) {
133 return "S".equals(lr) ? FanMovement.VERTICAL_AND_HORIZONTAL : FanMovement.VERTICAL;
135 return "S".equals(lr) ? FanMovement.HORIZONTAL : FanMovement.STOPPED;