]> git.basschouten.com Git - openhab-addons.git/blob
dcb73aae10308318826285346e1ffd975e6d692a
[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.max.internal.command;
14
15 import java.util.Base64;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.max.internal.Utils;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 /**
23  * The {@link SConfigCommand} for setting MAX! thermostat configuration.
24  *
25  * @author Marcel Verpaalen - Initial contribution
26  */
27 @NonNullByDefault
28 public class SConfigCommand extends CubeCommand {
29
30     private String baseString = "";
31     private final String rfAddress;
32     private final int roomId;
33
34     private byte[] commandBytes = new byte[0];
35
36     private final Logger logger = LoggerFactory.getLogger(SConfigCommand.class);
37
38     public enum ConfigCommandType {
39         Temperature,
40         Valve,
41         SetRoom,
42         RemoveRoom,
43         ProgramData
44     }
45
46     private final ConfigCommandType configCommandType;
47
48     /**
49      * Creates a base command with rfAddress and roomID.
50      *
51      * @param rfAddress
52      *            the RF address the command is for
53      * @param roomId
54      *            the room ID the RF address is mapped to
55      * @param configCommandType
56      *            the Type of config command to be send
57      */
58     public SConfigCommand(String rfAddress, int roomId, ConfigCommandType configCommandType) {
59         this.rfAddress = rfAddress;
60         this.roomId = roomId;
61         this.configCommandType = configCommandType;
62         if (configCommandType == ConfigCommandType.Temperature) {
63             setTempConfigDefault();
64         } else if (configCommandType == ConfigCommandType.SetRoom) {
65             baseString = "000022000000";
66             commandBytes = new byte[] { 0 };
67         } else if (configCommandType == ConfigCommandType.RemoveRoom) {
68             baseString = "000023000000";
69             commandBytes = new byte[] { 0 };
70         } else {
71             logger.debug("Config Command {} not implemented", configCommandType);
72         }
73     }
74
75     /**
76      * Set the Thermostat temperature configuration
77      */
78     public SConfigCommand(String rfAddress, int roomId, double tempComfort, double tempEco, double tempSetpointMax,
79             double tempSetpointMin, double tempOffset, double tempOpenWindow, int durationOpenWindow) {
80         this.rfAddress = rfAddress;
81         this.roomId = roomId;
82         this.configCommandType = ConfigCommandType.Temperature;
83         setTempConfig(tempComfort, tempEco, tempSetpointMax, tempSetpointMin, tempOffset, tempOpenWindow,
84                 durationOpenWindow);
85     }
86
87     /**
88      * Set the thermostat default temperature config.
89      */
90     public void setTempConfigDefault() {
91         double tempComfort = 21.0; // 0x2a
92         double tempEco = 17.0; // 0x28
93         double tempSetpointMax = 30.0; // 0x3d
94         double tempSetpointMin = 4.5; // 0x09
95         double tempOffset = 3.5; // 0x07
96         double tempOpenWindow = 12.0; // 0x18;
97         int durationOpenWindow = 3; // 0x03;
98         setTempConfig(tempComfort, tempEco, tempSetpointMax, tempSetpointMin, tempOffset, tempOpenWindow,
99                 durationOpenWindow);
100     }
101
102     /**
103      * Set the thermostat temperature config.
104      *
105      * @param tempComfort
106      *            the Comfort temperature
107      * @param tempEco
108      *            the ECO temperature
109      * @param tempSetpointMax
110      *            the Max temperature
111      * @param tempSetpointMin
112      *            the min temperature
113      * @param tempOffset
114      *            the offset temperature
115      * @param tempOpenWindow
116      *            the window open temperature
117      * @param durationOpenWindow
118      *            the window open duration in minutes
119      */
120     public void setTempConfig(double tempComfort, double tempEco, double tempSetpointMax, double tempSetpointMin,
121             double tempOffset, double tempOpenWindow, int durationOpenWindow) {
122         baseString = "000011000000";
123
124         byte tempComfortByte = (byte) (tempComfort * 2);
125         byte tempEcoByte = (byte) (tempEco * 2);
126         byte tempSetpointMaxByte = (byte) (tempSetpointMax * 2);
127         byte tempSetpointMinByte = (byte) (tempSetpointMin * 2);
128         byte tempOffsetByte = (byte) ((tempOffset + 3.5) * 2);
129         byte tempOpenWindowByte = (byte) (tempOpenWindow * 2);
130         byte durationOpenWindowByte = (byte) (durationOpenWindow / 5);
131         commandBytes = new byte[] { tempComfortByte, tempEcoByte, tempSetpointMaxByte, tempSetpointMinByte,
132                 tempOffsetByte, tempOpenWindowByte, durationOpenWindowByte };
133         logger.debug(
134                 "Thermostat Config Command:  confTemp: {}, ecoTemp: {}, setMax: {}, setMin: {}, offset: {}, windowTemp: {}, windowDur:{}",
135                 tempComfort, tempEco, tempSetpointMax, tempSetpointMin, tempOffset, tempOpenWindow, durationOpenWindow);
136     }
137
138     /**
139      * Returns the Base64 encoded command string to be sent via the MAX! Cube.
140      *
141      * @return the string representing the command
142      */
143     @Override
144     public String getCommandString() {
145         final StringBuilder commandConfigString = new StringBuilder();
146         for (byte b : commandBytes) {
147             commandConfigString.append(String.format("%02X", b));
148         }
149
150         String commandString = baseString + rfAddress;
151         if (configCommandType == ConfigCommandType.SetRoom || configCommandType == ConfigCommandType.RemoveRoom) {
152             commandString = commandString + commandConfigString + Utils.toHex(roomId);
153         } else {
154             commandString = commandString + Utils.toHex(roomId) + commandConfigString;
155         }
156
157         String encodedString = Base64.getEncoder().encodeToString(Utils.hexStringToByteArray(commandString));
158         return "s:" + encodedString + "\r\n";
159     }
160
161     @Override
162     public String getReturnStrings() {
163         return "S:";
164     }
165 }