2 * Copyright (c) 2010-2021 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 org.openhab.binding.rfxcom.internal.config.RFXComDeviceConfiguration;
16 import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
17 import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedValueException;
18 import org.openhab.core.util.HexUtils;
21 * Base class for RFXCOM data classes. All other data classes should extend this class.
23 * @author Pauli Anttila - Initial contribution
25 public abstract class RFXComBaseMessage implements RFXComMessage {
27 public static final String ID_DELIMITER = ".";
29 public enum PacketType implements ByteEnumWrapper {
32 TRANSMITTER_MESSAGE(2),
33 UNDECODED_RF_MESSAGE(3),
41 FAN(23, RFXComFanMessage.SubType.LUCCI_AIR_FAN, RFXComFanMessage.SubType.WESTINGHOUSE_7226640,
42 RFXComFanMessage.SubType.CASAFAN),
43 FAN_SF01(23, RFXComFanMessage.SubType.SF01),
44 FAN_ITHO(23, RFXComFanMessage.SubType.CVE_RFT),
45 FAN_LUCCI_DC(23, RFXComFanMessage.SubType.LUCCI_AIR_DC),
46 FAN_LUCCI_DC_II(23, RFXComFanMessage.SubType.LUCCI_AIR_DC_II),
47 FAN_SEAV(23, RFXComFanMessage.SubType.SEAV_TXS4),
48 FAN_FT1211R(23, RFXComFanMessage.SubType.FT1211R),
49 FAN_FALMEC(23, RFXComFanMessage.SubType.FALMEC),
68 TEMPERATURE_HUMIDITY(82),
70 TEMPERATURE_HUMIDITY_BAROMETRIC(84),
89 private final int packetType;
90 private final ByteEnumWrapper[] subTypes;
92 PacketType(int packetType, ByteEnumWrapper... subTypes) {
93 this.packetType = packetType;
94 this.subTypes = subTypes;
98 public byte toByte() {
99 return (byte) packetType;
103 public byte[] rawMessage;
104 private PacketType packetType;
105 public byte packetId;
111 public RFXComBaseMessage() {
114 public RFXComBaseMessage(PacketType packetType) {
115 this.packetType = packetType;
119 public void encodeMessage(byte[] data) throws RFXComException {
123 packetType = fromByte(data[1], data[2]);
128 if (data.length > 5) {
133 private PacketType fromByte(byte packetId, byte subType) throws RFXComUnsupportedValueException {
134 for (PacketType enumValue : PacketType.values()) {
135 if (enumValue.toByte() == packetId) {
136 // if there are no subtypes?
137 if (enumValue.subTypes.length == 0) {
140 // otherwise check for the matching subType
141 for (ByteEnumWrapper e : enumValue.subTypes) {
142 if (e.toByte() == subType) {
149 throw new RFXComUnsupportedValueException(PacketType.class, packetId);
152 public PacketType getPacketType() {
157 public String toString() {
160 if (rawMessage == null) {
161 str = "Raw data = unknown";
163 str = "Raw data = " + HexUtils.bytesToHex(rawMessage);
166 str += ", Packet type = " + packetType;
167 str += ", Seq number = " + Byte.toUnsignedInt(seqNbr);
173 public void setConfig(RFXComDeviceConfiguration deviceConfiguration) throws RFXComException {