]> git.basschouten.com Git - openhab-addons.git/blob
d4ae212a5ec261ddd9e5c9ae7fe8c6d7f5c96f2c
[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.messages.ByteEnumUtil.fromByte;
16
17 import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
18 import org.openhab.core.types.Type;
19
20 /**
21  * RFXCOM data class for transmitter message.
22  *
23  * @author Pauli Anttila - Initial contribution
24  */
25 public class RFXComTransmitterMessage extends RFXComBaseMessage {
26
27     public enum SubType implements ByteEnumWrapper {
28         ERROR_RECEIVER_DID_NOT_LOCK(0),
29         RESPONSE(1);
30
31         private final int subType;
32
33         SubType(int subType) {
34             this.subType = subType;
35         }
36
37         @Override
38         public byte toByte() {
39             return (byte) subType;
40         }
41     }
42
43     public enum Response implements ByteEnumWrapper {
44         ACK(0), // ACK, transmit OK
45         ACK_DELAYED(1), // ACK, but transmit started after 3 seconds delay
46                         // anyway with RF receive data
47         NAK(2), // NAK, transmitter did not lock on the requested transmit
48                 // frequency
49         NAK_INVALID_AC_ADDRESS(3); // NAK, AC address zero in id1-id4 not
50                                    // allowed
51
52         private final int response;
53
54         Response(int response) {
55             this.response = response;
56         }
57
58         @Override
59         public byte toByte() {
60             return (byte) response;
61         }
62     }
63
64     public SubType subType;
65     public Response response;
66
67     public RFXComTransmitterMessage() {
68         super(PacketType.TRANSMITTER_MESSAGE);
69     }
70
71     public RFXComTransmitterMessage(byte[] data) throws RFXComException {
72         encodeMessage(data);
73     }
74
75     @Override
76     public String toString() {
77         String str = "";
78
79         str += super.toString();
80
81         if (subType == SubType.RESPONSE) {
82             str += ", Sub type = " + subType;
83             str += ", Response = " + response;
84         } else {
85             str += ", Sub type = " + subType;
86             // Response not used
87         }
88
89         return str;
90     }
91
92     @Override
93     public void encodeMessage(byte[] data) throws RFXComException {
94         super.encodeMessage(data);
95
96         subType = fromByte(SubType.class, super.subType);
97         response = fromByte(Response.class, data[4]);
98     }
99
100     @Override
101     public byte[] decodeMessage() {
102         byte[] data = new byte[5];
103
104         data[0] = 0x04;
105         data[1] = RFXComBaseMessage.PacketType.TRANSMITTER_MESSAGE.toByte();
106         data[2] = subType.toByte();
107         data[3] = seqNbr;
108         data[4] = response.toByte();
109
110         return data;
111     }
112
113     @Override
114     public void convertFromState(String channelId, Type type) {
115         throw new UnsupportedOperationException();
116     }
117 }