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 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),
88 private final int packetType;
89 private final ByteEnumWrapper[] subTypes;
91 PacketType(int packetType, ByteEnumWrapper... subTypes) {
92 this.packetType = packetType;
93 this.subTypes = subTypes;
97 public byte toByte() {
98 return (byte) packetType;
102 public byte[] rawMessage;
103 private PacketType packetType;
104 public byte packetId;
110 public RFXComBaseMessage() {
113 public RFXComBaseMessage(PacketType packetType) {
114 this.packetType = packetType;
118 public void encodeMessage(byte[] data) throws RFXComException {
122 packetType = fromByte(data[1], data[2]);
127 if (data.length > 5) {
132 private PacketType fromByte(byte packetId, byte subType) throws RFXComUnsupportedValueException {
133 for (PacketType enumValue : PacketType.values()) {
134 if (enumValue.toByte() == packetId) {
135 // if there are no subtypes?
136 if (enumValue.subTypes.length == 0) {
139 // otherwise check for the matching subType
140 for (ByteEnumWrapper e : enumValue.subTypes) {
141 if (e.toByte() == subType) {
148 throw new RFXComUnsupportedValueException(PacketType.class, packetId);
151 public PacketType getPacketType() {
156 public String toString() {
159 if (rawMessage == null) {
160 str = "Raw data = unknown";
162 str = "Raw data = " + HexUtils.bytesToHex(rawMessage);
165 str += ", Packet type = " + packetType;
166 str += ", Seq number = " + (short) (seqNbr & 0xFF);
172 public void setConfig(RFXComDeviceConfiguration deviceConfiguration) throws RFXComException {