]> git.basschouten.com Git - openhab-addons.git/blob
b12c1706c2d00867c8e7c16661f67907e0b109b2
[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.daikin.internal.api;
14
15 import java.util.HashMap;
16 import java.util.Map;
17 import java.util.Optional;
18
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;
27
28 /**
29  * Class for holding the set of parameters used by set and get control info.
30  *
31  * @author Tim Waterhouse - Initial Contribution
32  * @author Paul Smedley <paul@smedley.id.au> - mods for Daikin Airbase
33  *
34  */
35 @NonNullByDefault
36 public class ControlInfo {
37     private static final Logger LOGGER = LoggerFactory.getLogger(ControlInfo.class);
38
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;
52
53     private ControlInfo() {
54     }
55
56     public static ControlInfo parse(String response) {
57         LOGGER.trace("Parsing string: \"{}\"", response);
58
59         Map<String, String> responseMap = InfoParser.parse(response);
60
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;
70         }
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);
79         } else {
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);
85             } else {
86                 info.fanMovement = FanMovement.UNKNOWN;
87             }
88         }
89
90         info.targetHumidity = Optional.ofNullable(responseMap.get("shum")).flatMap(value -> InfoParser.parseInt(value));
91
92         info.advancedMode = Optional.ofNullable(responseMap.get("adv")).map(value -> AdvancedMode.fromValue(value))
93                 .orElse(AdvancedMode.UNKNOWN);
94         return info;
95     }
96
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"
105                             : "0");
106             params.put("f_dir_ud",
107                     fanMovement == FanMovement.HORIZONTAL || fanMovement == FanMovement.VERTICAL_AND_HORIZONTAL ? "S"
108                             : "0");
109         } else {
110             params.put("f_dir", Integer.toString(fanMovement.getValue()));
111         }
112         params.put("stemp", temp.orElse(20.0).toString());
113         params.put("shum", targetHumidity.map(value -> value.toString()).orElse(""));
114
115         return params;
116     }
117
118     public SpecialMode getSpecialMode() {
119         return SpecialMode.fromAdvancedMode(advancedMode);
120     }
121
122     /*
123      * This method combines different direction parameters to one.
124      * 
125      * @param ud up/ down value S or 0
126      * 
127      * @param lr left/ right value S or 0
128      * 
129      * @return combined value based on {@link FanMovement} enum
130      */
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;
134         } else {
135             return "S".equals(lr) ? FanMovement.HORIZONTAL : FanMovement.STOPPED;
136         }
137     }
138 }