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