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.RFXComBaseMessage.PacketType.UNDECODED_RF_MESSAGE;
18 import java.util.Arrays;
20 import org.openhab.binding.rfxcom.internal.config.RFXComDeviceConfiguration;
21 import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
22 import org.openhab.binding.rfxcom.internal.exceptions.RFXComMessageTooLongException;
23 import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedChannelException;
24 import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedValueException;
25 import org.openhab.binding.rfxcom.internal.handler.DeviceState;
26 import org.openhab.core.library.types.StringType;
27 import org.openhab.core.types.State;
28 import org.openhab.core.types.Type;
29 import org.openhab.core.util.HexUtils;
32 * RFXCOM data class for undecoded messages.
34 * @author Ivan Martinez - Initial contribution
35 * @author James Hewitt-Thomas - Migrated for OH2
37 public class RFXComUndecodedRFMessage extends RFXComDeviceMessageImpl<RFXComUndecodedRFMessage.SubType> {
39 public enum SubType implements ByteEnumWrapper {
44 LACROSSE_VIKING(0x04),
70 private final int subType;
72 SubType(int subType) {
73 this.subType = subType;
77 public byte toByte() {
78 return (byte) subType;
81 public static SubType fromByte(int input) {
82 for (SubType c : SubType.values()) {
83 if (c.subType == input) {
88 return SubType.UNKNOWN;
92 public SubType subType;
93 public byte[] rawPayload;
95 public RFXComUndecodedRFMessage() {
96 super(UNDECODED_RF_MESSAGE);
99 public RFXComUndecodedRFMessage(byte[] message) throws RFXComException {
100 encodeMessage(message);
104 public String toString() {
105 String str = super.toString();
107 str += ", Sub type = " + subType;
113 public void encodeMessage(byte[] message) throws RFXComException {
114 super.encodeMessage(message);
116 subType = SubType.fromByte(super.subType);
117 rawPayload = Arrays.copyOfRange(rawMessage, 4, rawMessage.length);
121 public byte[] decodeMessage() throws RFXComException {
122 if (rawPayload.length > 33) {
123 throw new RFXComMessageTooLongException("Longest payload according to RFXCOM spec is 33 bytes.");
126 final int rawPayloadLen = rawPayload.length;
127 byte[] data = new byte[4 + rawPayloadLen];
129 data[0] = (byte) (data.length - 1);
130 data[1] = UNDECODED_RF_MESSAGE.toByte();
131 data[2] = subType.toByte();
134 System.arraycopy(rawPayload, 0, data, 4, rawPayloadLen);
139 public String getDeviceId() {
144 public State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
145 throws RFXComUnsupportedChannelException {
147 case CHANNEL_RAW_MESSAGE:
148 return new StringType(HexUtils.bytesToHex(rawMessage));
150 case CHANNEL_RAW_PAYLOAD:
151 return new StringType(HexUtils.bytesToHex(rawPayload));
154 throw new RFXComUnsupportedChannelException("Nothing relevant for " + channelId);
159 public void setSubType(SubType subType) {
160 throw new UnsupportedOperationException();
164 public void setDeviceId(String deviceId) {
165 throw new UnsupportedOperationException();
169 public void convertFromState(String channelId, Type type) {
170 throw new UnsupportedOperationException();
174 public SubType convertSubType(String subType) throws RFXComUnsupportedValueException {
175 return ByteEnumUtil.convertSubType(SubType.class, subType);