]> git.basschouten.com Git - openhab-addons.git/blob
3ed1bc1b806809392db26372d469444ee39ee9fa
[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     public Mode mode = Mode.AUTO;
42     /** Degrees in Celsius. */
43     public Optional<Double> temp = Optional.empty();
44     public FanSpeed fanSpeed = FanSpeed.AUTO;
45     public FanMovement fanMovement = FanMovement.STOPPED;
46     /* Not supported by all units. Sets the target humidity for dehumidifying. */
47     public Optional<Integer> targetHumidity = Optional.empty();
48     public AdvancedMode advancedMode = AdvancedMode.UNKNOWN;
49     public boolean separatedDirectionParams = false;
50
51     private ControlInfo() {
52     }
53
54     public static ControlInfo parse(String response) {
55         LOGGER.trace("Parsing string: \"{}\"", response);
56
57         Map<String, String> responseMap = InfoParser.parse(response);
58
59         ControlInfo info = new ControlInfo();
60         info.ret = Optional.ofNullable(responseMap.get("ret")).orElse("");
61         info.power = "1".equals(responseMap.get("pow"));
62         info.mode = Optional.ofNullable(responseMap.get("mode")).flatMap(value -> InfoParser.parseInt(value))
63                 .map(value -> Mode.fromValue(value)).orElse(Mode.AUTO);
64         info.temp = Optional.ofNullable(responseMap.get("stemp")).flatMap(value -> InfoParser.parseDouble(value));
65         info.fanSpeed = Optional.ofNullable(responseMap.get("f_rate")).map(value -> FanSpeed.fromValue(value))
66                 .orElse(FanSpeed.AUTO);
67         // determine if device has combined direction (f_dir) or separated directions (f_dir_ud/f_dir_lr)
68         if (response.contains("f_dir=")) {
69             info.fanMovement = Optional.ofNullable(responseMap.get("f_dir"))
70                     .flatMap(value -> InfoParser.parseInt(value)).map(value -> FanMovement.fromValue(value))
71                     .orElse(FanMovement.STOPPED);
72         } else {
73             info.separatedDirectionParams = true;
74             String ud = responseMap.get("f_dir_ud");
75             String lr = responseMap.get("f_dir_lr");
76             if (ud != null && lr != null) {
77                 info.fanMovement = parseFanMovement(ud, lr);
78             } else {
79                 info.fanMovement = FanMovement.UNKNOWN;
80             }
81         }
82
83         info.targetHumidity = Optional.ofNullable(responseMap.get("shum")).flatMap(value -> InfoParser.parseInt(value));
84
85         info.advancedMode = Optional.ofNullable(responseMap.get("adv")).map(value -> AdvancedMode.fromValue(value))
86                 .orElse(AdvancedMode.UNKNOWN);
87         return info;
88     }
89
90     public Map<String, String> getParamString() {
91         Map<String, String> params = new HashMap<>();
92         params.put("pow", power ? "1" : "0");
93         params.put("mode", Integer.toString(mode.getValue()));
94         params.put("f_rate", fanSpeed.getValue());
95         if (separatedDirectionParams) {
96             params.put("f_dir_lr",
97                     fanMovement == FanMovement.VERTICAL || fanMovement == FanMovement.VERTICAL_AND_HORIZONTAL ? "S"
98                             : "0");
99             params.put("f_dir_ud",
100                     fanMovement == FanMovement.HORIZONTAL || fanMovement == FanMovement.VERTICAL_AND_HORIZONTAL ? "S"
101                             : "0");
102         } else {
103             params.put("f_dir", Integer.toString(fanMovement.getValue()));
104         }
105         params.put("stemp", temp.orElse(20.0).toString());
106         params.put("shum", targetHumidity.map(value -> value.toString()).orElse(""));
107
108         return params;
109     }
110
111     public SpecialMode getSpecialMode() {
112         return SpecialMode.fromAdvancedMode(advancedMode);
113     }
114
115     /*
116      * This method combines different direction parameters to one.
117      * 
118      * @param ud up/ down value S or 0
119      * 
120      * @param lr left/ right value S or 0
121      * 
122      * @return combined value based on {@link FanMovement} enum
123      */
124     public static FanMovement parseFanMovement(String ud, String lr) {
125         if ("S".equals(ud)) {
126             return "S".equals(lr) ? FanMovement.VERTICAL_AND_HORIZONTAL : FanMovement.VERTICAL;
127         } else {
128             return "S".equals(lr) ? FanMovement.HORIZONTAL : FanMovement.STOPPED;
129         }
130     }
131 }