]> git.basschouten.com Git - openhab-addons.git/blob
4c398ee6eae74755bad9a9ffea422b455edb662e
[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.enocean.internal.messages;
14
15 import java.security.InvalidParameterException;
16 import java.util.Arrays;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.binding.enocean.internal.eep.Base.UTEResponse;
20 import org.openhab.binding.enocean.internal.eep.Base._1BSMessage;
21 import org.openhab.binding.enocean.internal.eep.Base._4BSMessage;
22
23 /**
24  *
25  * @author Daniel Weber - Initial contribution
26  */
27 @NonNullByDefault
28 public class ERP1Message extends BasePacket {
29
30     // these are just ESP3 RORGs, ESP2 ORGs are converted by ESP2PacketConverter
31     public enum RORG {
32         Unknown((byte) 0x00, 0),
33         RPS((byte) 0xF6, 1),
34         _1BS((byte) 0xD5, 1),
35         _4BS((byte) 0xA5, 4),
36         VLD((byte) 0xD2, -1),
37         // ADT(0xA6, -1),
38         UTE((byte) 0xD4, -1),
39         SIG((byte) 0xD0, -1),
40         MSC((byte) 0xD1, -1);
41
42         private byte value;
43         private int dataLength;
44
45         RORG(byte value, int dataLength) {
46             this.value = value;
47             this.dataLength = dataLength;
48         }
49
50         public byte getValue() {
51             return this.value;
52         }
53
54         public int getDataLength() {
55             return dataLength;
56         }
57
58         public static RORG getRORG(byte value) {
59             for (RORG t : RORG.values()) {
60                 if (t.value == value) {
61                     return t;
62                 }
63             }
64
65             throw new InvalidParameterException("Unknown choice");
66         }
67     }
68
69     RORG rorg;
70
71     byte[] senderId;
72     boolean teachIn;
73
74     public ERP1Message(int dataLength, int optionalDataLength, byte[] payload) {
75         super(dataLength, optionalDataLength, ESPPacketType.RADIO_ERP1, payload);
76
77         teachIn = false;
78         senderId = new byte[0];
79         try {
80             rorg = RORG.getRORG(payload[0]);
81
82             switch (rorg) {
83                 case RPS:
84                     if (dataLength >= 6) {
85                         senderId = Arrays.copyOfRange(payload, 2, 6);
86                         teachIn = false;
87                     }
88                     break;
89                 case _1BS:
90                     if (dataLength >= 6) {
91                         senderId = Arrays.copyOfRange(payload, 2, 6);
92                         teachIn = ((_1BSMessage.TEACHIN_BIT & payload[1]) == 0);
93                     }
94                     break;
95                 case _4BS:
96                     if (dataLength >= 9) {
97                         senderId = Arrays.copyOfRange(payload, 5, 9);
98                         teachIn = (_4BSMessage.TEACHIN_BIT & payload[4]) == 0;
99                     }
100                     break;
101                 case VLD:
102                     teachIn = false;
103                     senderId = Arrays.copyOfRange(payload, dataLength - 5, dataLength - 1);
104                     break;
105                 case UTE:
106                     if (dataLength >= 6) {
107                         teachIn = (payload[1] & UTEResponse.TEACHIN_MASK) == 0
108                                 || (payload[1] & UTEResponse.TEACHIN_MASK) == UTEResponse.TEACHIN_NPTSPECIFIED;
109                         senderId = Arrays.copyOfRange(payload, dataLength - 5, dataLength - 1);
110                     }
111                     break;
112                 case SIG:
113                 case MSC:
114                     teachIn = false;
115                     senderId = Arrays.copyOfRange(payload, dataLength - 5, dataLength - 1);
116                     break;
117                 default:
118                     rorg = RORG.Unknown;
119             }
120
121         } catch (Exception e) {
122             rorg = RORG.Unknown;
123             senderId = new byte[0];
124         }
125     }
126
127     public RORG getRORG() {
128         return rorg;
129     }
130
131     public byte[] getSenderId() {
132         return senderId;
133     }
134
135     public boolean getIsTeachIn() {
136         return teachIn;
137     }
138 }