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.max.internal.command;
15 import java.util.Base64;
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;
23 * The {@link SConfigCommand} for setting MAX! thermostat configuration.
25 * @author Marcel Verpaalen - Initial contribution
28 public class SConfigCommand extends CubeCommand {
30 private String baseString = "";
31 private final String rfAddress;
32 private final int roomId;
34 private byte[] commandBytes = new byte[0];
36 private final Logger logger = LoggerFactory.getLogger(SConfigCommand.class);
38 public enum ConfigCommandType {
46 private final ConfigCommandType configCommandType;
49 * Creates a base command with rfAddress and roomID.
52 * the RF address the command is for
54 * the room ID the RF address is mapped to
55 * @param configCommandType
56 * the Type of config command to be send
58 public SConfigCommand(String rfAddress, int roomId, ConfigCommandType configCommandType) {
59 this.rfAddress = rfAddress;
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 };
71 logger.debug("Config Command {} not implemented", configCommandType);
76 * Set the Thermostat temperature configuration
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;
82 this.configCommandType = ConfigCommandType.Temperature;
83 setTempConfig(tempComfort, tempEco, tempSetpointMax, tempSetpointMin, tempOffset, tempOpenWindow,
88 * Set the thermostat default temperature config.
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,
103 * Set the thermostat temperature config.
106 * the Comfort temperature
108 * the ECO temperature
109 * @param tempSetpointMax
110 * the Max temperature
111 * @param tempSetpointMin
112 * the min temperature
114 * the offset temperature
115 * @param tempOpenWindow
116 * the window open temperature
117 * @param durationOpenWindow
118 * the window open duration in minutes
120 public void setTempConfig(double tempComfort, double tempEco, double tempSetpointMax, double tempSetpointMin,
121 double tempOffset, double tempOpenWindow, int durationOpenWindow) {
122 baseString = "000011000000";
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 };
134 "Thermostat Config Command: confTemp: {}, ecoTemp: {}, setMax: {}, setMin: {}, offset: {}, windowTemp: {}, windowDur:{}",
135 tempComfort, tempEco, tempSetpointMax, tempSetpointMin, tempOffset, tempOpenWindow, durationOpenWindow);
139 * Returns the Base64 encoded command string to be sent via the MAX! Cube.
141 * @return the string representing the command
144 public String getCommandString() {
145 final StringBuilder commandConfigString = new StringBuilder();
146 for (byte b : commandBytes) {
147 commandConfigString.append(String.format("%02X", b));
150 String commandString = baseString + rfAddress;
151 if (configCommandType == ConfigCommandType.SetRoom || configCommandType == ConfigCommandType.RemoveRoom) {
152 commandString = commandString + commandConfigString + Utils.toHex(roomId);
154 commandString = commandString + Utils.toHex(roomId) + commandConfigString;
157 String encodedString = Base64.getEncoder().encodeToString(Utils.hexStringToByteArray(commandString));
158 return "s:" + encodedString + "\r\n";
162 public String getReturnStrings() {