2 * Copyright (c) 2010-2020 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.exceptions.RFXComException;
19 import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedChannelException;
20 import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedValueException;
21 import org.openhab.binding.rfxcom.internal.handler.DeviceState;
22 import org.openhab.core.library.types.DecimalType;
23 import org.openhab.core.library.types.OpenClosedType;
24 import org.openhab.core.types.State;
25 import org.openhab.core.types.Type;
26 import org.openhab.core.types.UnDefType;
29 * RFXCOM data class for thermostat1 message.
30 * Digimax 210 Thermostat RF sensor operational
32 * @author Les Ashworth - Initial contribution
33 * @author Pauli Anttila - Migrated for OH2
35 public class RFXComThermostat1Message extends RFXComDeviceMessageImpl<RFXComThermostat1Message.SubType> {
37 public enum SubType implements ByteEnumWrapper {
41 private final int subType;
43 SubType(int subType) {
44 this.subType = subType;
48 public byte toByte() {
49 return (byte) subType;
53 /* Added item for ContactTypes */
54 public enum Status implements ByteEnumWrapper {
60 private final int status;
67 public byte toByte() {
73 public enum Mode implements ByteEnumWrapper {
77 private final int mode;
84 public byte toByte() {
89 public SubType subType;
91 public byte temperature;
96 public RFXComThermostat1Message() {
97 super(PacketType.THERMOSTAT1);
100 public RFXComThermostat1Message(byte[] data) throws RFXComException {
105 public String toString() {
108 str += super.toString();
109 str += ", Sub type = " + subType;
110 str += ", Device Id = " + getDeviceId();
111 str += ", Temperature = " + temperature;
112 str += ", Set = " + set;
113 str += ", Mode = " + mode;
114 str += ", Status = " + status;
115 str += ", Signal level = " + signalLevel;
121 public void encodeMessage(byte[] data) throws RFXComException {
122 super.encodeMessage(data);
124 subType = fromByte(SubType.class, super.subType);
125 sensorId = (data[4] & 0xFF) << 8 | (data[5] & 0xFF);
126 temperature = data[6];
128 mode = fromByte(Mode.class, (data[8] & 0xF0) >> 7);
130 status = fromByte(Status.class, data[8] & 0x03);
131 signalLevel = (byte) ((data[9] & 0xF0) >> 4);
135 public byte[] decodeMessage() {
136 byte[] data = new byte[10];
139 data[1] = RFXComBaseMessage.PacketType.THERMOSTAT1.toByte();
140 data[2] = subType.toByte();
142 data[4] = (byte) ((sensorId & 0xFF00) >> 8);
143 data[5] = (byte) (sensorId & 0x00FF);
144 data[6] = (temperature);
146 data[8] = (byte) ((mode.toByte() << 7) | (status.toByte() & 0xFF));
147 data[9] = (byte) (signalLevel << 4);
153 public String getDeviceId() {
154 return String.valueOf(sensorId);
158 public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
160 case CHANNEL_TEMPERATURE:
161 return new DecimalType(temperature);
163 case CHANNEL_SET_POINT:
164 return new DecimalType(set);
166 case CHANNEL_CONTACT:
169 return OpenClosedType.CLOSED;
171 return OpenClosedType.OPEN;
173 return UnDefType.UNDEF;
177 return super.convertToState(channelId, deviceState);
182 public void setSubType(SubType subType) {
183 throw new UnsupportedOperationException();
187 public void setDeviceId(String deviceId) {
188 throw new UnsupportedOperationException();
192 public void convertFromState(String channelId, Type type) {
193 throw new UnsupportedOperationException();
197 public SubType convertSubType(String subType) throws RFXComUnsupportedValueException {
198 return ByteEnumUtil.convertSubType(SubType.class, subType);