]> git.basschouten.com Git - openhab-addons.git/blob
456510707913ec8bee72898c587f08bbe3e9ad64
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.rfxcom.internal.messages;
14
15 import static org.openhab.binding.rfxcom.internal.RFXComBindingConstants.*;
16 import static org.openhab.binding.rfxcom.internal.messages.RFXComBaseMessage.PacketType.UNDECODED_RF_MESSAGE;
17
18 import java.util.Arrays;
19
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;
29
30 /**
31  * RFXCOM data class for undecoded messages.
32  *
33  * @author Ivan Martinez - Initial contribution
34  * @author James Hewitt-Thomas - Migrated for OH2
35  */
36 public class RFXComUndecodedRFMessage extends RFXComDeviceMessageImpl<RFXComUndecodedRFMessage.SubType> {
37
38     public enum SubType implements ByteEnumWrapper {
39         AC(0x00),
40         ARC(0x01),
41         ATI(0x02),
42         HIDEKI_UPM(0x03),
43         LACROSSE_VIKING(0x04),
44         AD(0x05),
45         MERTIK(0x06),
46         OREGON1(0x07),
47         OREGON2(0x08),
48         OREGON3(0x09),
49         PROGUARD(0x0A),
50         VISONIC(0x0B),
51         NEC(0x0C),
52         FS20(0x0D),
53         RESERVED(0x0E),
54         BLINDS(0x0F),
55         RUBICSON(0x10),
56         AE(0x11),
57         FINE_OFFSET(0x12),
58         RGB(0x13),
59         RTS(0x14),
60         SELECT_PLUS(0x15),
61         HOME_CONFORT(0x16),
62         EDISIO(0x17),
63         HONEYWELL(0x18),
64         FUNKBUS(0x19),
65         BYRONSX(0x1A),
66
67         UNKNOWN(0xFF);
68
69         private final int subType;
70
71         SubType(int subType) {
72             this.subType = subType;
73         }
74
75         @Override
76         public byte toByte() {
77             return (byte) subType;
78         }
79
80         public static SubType fromByte(int input) {
81             for (SubType c : SubType.values()) {
82                 if (c.subType == input) {
83                     return c;
84                 }
85             }
86
87             return SubType.UNKNOWN;
88         }
89     }
90
91     public SubType subType;
92     public byte[] rawPayload;
93
94     public RFXComUndecodedRFMessage() {
95         super(UNDECODED_RF_MESSAGE);
96     }
97
98     public RFXComUndecodedRFMessage(byte[] message) throws RFXComException {
99         encodeMessage(message);
100     }
101
102     @Override
103     public String toString() {
104         String str = super.toString();
105
106         str += ", Sub type = " + subType;
107
108         return str;
109     }
110
111     @Override
112     public void encodeMessage(byte[] message) throws RFXComException {
113         super.encodeMessage(message);
114
115         subType = SubType.fromByte(super.subType);
116         rawPayload = Arrays.copyOfRange(rawMessage, 4, rawMessage.length);
117     }
118
119     @Override
120     public byte[] decodeMessage() throws RFXComException {
121         if (rawPayload.length > 33) {
122             throw new RFXComMessageTooLongException("Longest payload according to RFXCOM spec is 33 bytes.");
123         }
124
125         final int rawPayloadLen = rawPayload.length;
126         byte[] data = new byte[4 + rawPayloadLen];
127
128         data[0] = (byte) (data.length - 1);
129         data[1] = UNDECODED_RF_MESSAGE.toByte();
130         data[2] = subType.toByte();
131         data[3] = seqNbr;
132
133         System.arraycopy(rawPayload, 0, data, 4, rawPayloadLen);
134         return data;
135     }
136
137     @Override
138     public String getDeviceId() {
139         return "UNDECODED";
140     }
141
142     @Override
143     public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
144         switch (channelId) {
145             case CHANNEL_RAW_MESSAGE:
146                 return new StringType(HexUtils.bytesToHex(rawMessage));
147
148             case CHANNEL_RAW_PAYLOAD:
149                 return new StringType(HexUtils.bytesToHex(rawPayload));
150
151             default:
152                 throw new RFXComUnsupportedChannelException("Nothing relevant for " + channelId);
153         }
154     }
155
156     @Override
157     public void setSubType(SubType subType) {
158         throw new UnsupportedOperationException();
159     }
160
161     @Override
162     public void setDeviceId(String deviceId) {
163         throw new UnsupportedOperationException();
164     }
165
166     @Override
167     public void convertFromState(String channelId, Type type) {
168         throw new UnsupportedOperationException();
169     }
170
171     @Override
172     public SubType convertSubType(String subType) throws RFXComUnsupportedValueException {
173         return ByteEnumUtil.convertSubType(SubType.class, subType);
174     }
175 }