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