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 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.exceptions.RFXComException;
21 import org.openhab.binding.rfxcom.internal.exceptions.RFXComMessageTooLongException;
22 import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedChannelException;
23 import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedValueException;
24 import org.openhab.binding.rfxcom.internal.handler.DeviceState;
25 import org.openhab.core.library.types.StringType;
26 import org.openhab.core.types.State;
27 import org.openhab.core.types.Type;
28 import org.openhab.core.util.HexUtils;
31 * RFXCOM data class for undecoded messages.
33 * @author Ivan Martinez - Initial contribution
34 * @author James Hewitt-Thomas - Migrated for OH2
36 public class RFXComUndecodedRFMessage extends RFXComDeviceMessageImpl<RFXComUndecodedRFMessage.SubType> {
38 public enum SubType implements ByteEnumWrapper {
43 LACROSSE_VIKING(0x04),
69 private final int subType;
71 SubType(int subType) {
72 this.subType = subType;
76 public byte toByte() {
77 return (byte) subType;
80 public static SubType fromByte(int input) {
81 for (SubType c : SubType.values()) {
82 if (c.subType == input) {
87 return SubType.UNKNOWN;
91 public SubType subType;
92 public byte[] rawPayload;
94 public RFXComUndecodedRFMessage() {
95 super(UNDECODED_RF_MESSAGE);
98 public RFXComUndecodedRFMessage(byte[] message) throws RFXComException {
99 encodeMessage(message);
103 public String toString() {
104 String str = super.toString();
106 str += ", Sub type = " + subType;
112 public void encodeMessage(byte[] message) throws RFXComException {
113 super.encodeMessage(message);
115 subType = SubType.fromByte(super.subType);
116 rawPayload = Arrays.copyOfRange(rawMessage, 4, rawMessage.length);
120 public byte[] decodeMessage() throws RFXComException {
121 if (rawPayload.length > 33) {
122 throw new RFXComMessageTooLongException("Longest payload according to RFXCOM spec is 33 bytes.");
125 final int rawPayloadLen = rawPayload.length;
126 byte[] data = new byte[4 + rawPayloadLen];
128 data[0] = (byte) (data.length - 1);
129 data[1] = UNDECODED_RF_MESSAGE.toByte();
130 data[2] = subType.toByte();
133 System.arraycopy(rawPayload, 0, data, 4, rawPayloadLen);
138 public String getDeviceId() {
143 public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
145 case CHANNEL_RAW_MESSAGE:
146 return new StringType(HexUtils.bytesToHex(rawMessage));
148 case CHANNEL_RAW_PAYLOAD:
149 return new StringType(HexUtils.bytesToHex(rawPayload));
152 throw new RFXComUnsupportedChannelException("Nothing relevant for " + channelId);
157 public void setSubType(SubType subType) {
158 throw new UnsupportedOperationException();
162 public void setDeviceId(String deviceId) {
163 throw new UnsupportedOperationException();
167 public void convertFromState(String channelId, Type type) {
168 throw new UnsupportedOperationException();
172 public SubType convertSubType(String subType) throws RFXComUnsupportedValueException {
173 return ByteEnumUtil.convertSubType(SubType.class, subType);