]> git.basschouten.com Git - openhab-addons.git/blob
8f51859022509a301a8d5882c973e725ff17b0b6
[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.openhab.binding.max.internal.device.ThermostatModeType;
20
21 /**
22  * {@link SCommand} for setting MAX! thermostat temperature and mode.
23  *
24  * @author Andreas Heil - Initial contribution
25  * @author Marcel Verpaalen - OH2 update + simplification
26  */
27 @NonNullByDefault
28 public class SCommand extends CubeCommand {
29
30     private static final String BASE_STRING_S = "000040000000"; // for single devices
31     private static final String BASE_STRING_G = "000440000000"; // for group/room devices
32
33     private final boolean[] bits;
34     private final String rfAddress;
35     private final int roomId;
36
37     /**
38      * Creates a new instance of the MAX! protocol S command.
39      *
40      * @param rfAddress
41      *            the RF address the command is for
42      * @param roomId
43      *            the room ID the RF address is mapped to
44      * @param setpointTemperature
45      *            the desired setpoint temperature for the device.
46      */
47     public SCommand(String rfAddress, int roomId, ThermostatModeType mode, double setpointTemperature) {
48         this.rfAddress = rfAddress;
49         this.roomId = roomId;
50
51         // Temperature setpoint, Temp uses 6 bits (bit 0:5),
52         // 20 deg C = bits 101000 = dec 40/2 = 20 deg C,
53         // you need 8 bits to send so add the 2 bits below (sample 10101000 = hex A8)
54         // bit 0,1 = 00 = Auto weekprog (no temp is needed)
55
56         int setpointValue = (int) (setpointTemperature * 2);
57         bits = Utils.getBits(setpointValue);
58
59         // default to perm setting
60         // AB => bit mapping
61         // 01 = Permanent
62         // 10 = Temporarily
63         // 11 = Boost
64
65         switch (mode) {
66             case MANUAL:
67                 bits[7] = false; // A (MSB)
68                 bits[6] = true; // B
69                 break;
70             case AUTOMATIC:
71                 bits[7] = false; // A (MSB)
72                 bits[6] = false; // B
73                 break;
74             case BOOST:
75                 bits[7] = true; // A (MSB)
76                 bits[6] = true; // B
77                 break;
78             case VACATION:
79                 // not implemented needs time
80             default:
81                 // no further modes supported
82         }
83     }
84
85     /**
86      * Returns the Base64 encoded command string to be sent via the MAX!
87      * protocol.
88      *
89      * @return the string representing the command
90      */
91     @Override
92     public String getCommandString() {
93         final String baseString;
94         if (roomId == 0) {
95             baseString = BASE_STRING_S;
96         } else {
97             baseString = BASE_STRING_G;
98         }
99
100         final String commandString = baseString + rfAddress + Utils.toHex(roomId) + Utils.toHex(bits);
101
102         final String encodedString = Base64.getEncoder().encodeToString(Utils.hexStringToByteArray(commandString));
103
104         return "s:" + encodedString + "\r\n";
105     }
106
107     @Override
108     public String getReturnStrings() {
109         return "S:";
110     }
111 }