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.rfxcom.internal.messages;
15 import static org.openhab.binding.rfxcom.internal.RFXComBindingConstants.*;
16 import static org.openhab.binding.rfxcom.internal.messages.ByteEnumUtil.fromByte;
18 import org.openhab.binding.rfxcom.internal.config.RFXComDeviceConfiguration;
19 import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
20 import org.openhab.binding.rfxcom.internal.exceptions.RFXComInvalidStateException;
21 import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedChannelException;
22 import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedValueException;
23 import org.openhab.binding.rfxcom.internal.handler.DeviceState;
24 import org.openhab.core.library.types.DecimalType;
25 import org.openhab.core.library.types.OpenClosedType;
26 import org.openhab.core.types.State;
27 import org.openhab.core.types.Type;
28 import org.openhab.core.types.UnDefType;
31 * RFXCOM data class for thermostat1 message.
32 * Digimax 210 Thermostat RF sensor operational
34 * @author Les Ashworth - Initial contribution
35 * @author Pauli Anttila - Migrated for OH2
37 public class RFXComThermostat1Message extends RFXComDeviceMessageImpl<RFXComThermostat1Message.SubType> {
39 public enum SubType implements ByteEnumWrapper {
43 private final int subType;
45 SubType(int subType) {
46 this.subType = subType;
50 public byte toByte() {
51 return (byte) subType;
55 /* Added item for ContactTypes */
56 public enum Status implements ByteEnumWrapper {
62 private final int status;
69 public byte toByte() {
75 public enum Mode implements ByteEnumWrapper {
79 private final int mode;
86 public byte toByte() {
91 public SubType subType;
93 public byte temperature;
98 public RFXComThermostat1Message() {
99 super(PacketType.THERMOSTAT1);
102 public RFXComThermostat1Message(byte[] data) throws RFXComException {
107 public String toString() {
110 str += super.toString();
111 str += ", Sub type = " + subType;
112 str += ", Device Id = " + getDeviceId();
113 str += ", Temperature = " + temperature;
114 str += ", Set = " + set;
115 str += ", Mode = " + mode;
116 str += ", Status = " + status;
117 str += ", Signal level = " + signalLevel;
123 public void encodeMessage(byte[] data) throws RFXComException {
124 super.encodeMessage(data);
126 subType = fromByte(SubType.class, super.subType);
127 sensorId = (data[4] & 0xFF) << 8 | (data[5] & 0xFF);
128 temperature = data[6];
130 mode = fromByte(Mode.class, (data[8] & 0xF0) >> 7);
132 status = fromByte(Status.class, data[8] & 0x03);
133 signalLevel = (byte) ((data[9] & 0xF0) >> 4);
137 public byte[] decodeMessage() {
138 byte[] data = new byte[10];
141 data[1] = RFXComBaseMessage.PacketType.THERMOSTAT1.toByte();
142 data[2] = subType.toByte();
144 data[4] = (byte) ((sensorId & 0xFF00) >> 8);
145 data[5] = (byte) (sensorId & 0x00FF);
146 data[6] = (temperature);
148 data[8] = (byte) ((mode.toByte() << 7) | (status.toByte() & 0xFF));
149 data[9] = (byte) (signalLevel << 4);
155 public String getDeviceId() {
156 return String.valueOf(sensorId);
160 public State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
161 throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
163 case CHANNEL_TEMPERATURE:
164 return new DecimalType(temperature);
166 case CHANNEL_SET_POINT:
167 return new DecimalType(set);
169 case CHANNEL_CONTACT:
172 return OpenClosedType.CLOSED;
174 return OpenClosedType.OPEN;
176 return UnDefType.UNDEF;
180 return super.convertToState(channelId, config, deviceState);
185 public void setSubType(SubType subType) {
186 throw new UnsupportedOperationException();
190 public void setDeviceId(String deviceId) {
191 throw new UnsupportedOperationException();
195 public void convertFromState(String channelId, Type type) {
196 throw new UnsupportedOperationException();
200 public SubType convertSubType(String subType) throws RFXComUnsupportedValueException {
201 return ByteEnumUtil.convertSubType(SubType.class, subType);