]> git.basschouten.com Git - openhab-addons.git/blob
5a7cbd0db6d54afe6a833312a764d7aa29e8f2a0
[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.eep.Base;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.enocean.internal.config.EnOceanChannelTeachInConfig;
18 import org.openhab.binding.enocean.internal.eep.EEP;
19 import org.openhab.binding.enocean.internal.eep.EEPType;
20 import org.openhab.binding.enocean.internal.messages.ERP1Message;
21 import org.openhab.core.config.core.Configuration;
22 import org.openhab.core.util.HexUtils;
23
24 /**
25  *
26  * @author Daniel Weber - Initial contribution
27  */
28 @NonNullByDefault
29 public abstract class _4BSMessage extends EEP {
30
31     protected boolean supportsTeachInVariation3 = false;
32
33     public boolean isTeachInVariation3Supported() {
34         return supportsTeachInVariation3;
35     }
36
37     public _4BSMessage(ERP1Message packet) {
38         super(packet);
39     }
40
41     public _4BSMessage() {
42         super();
43     }
44
45     public static final byte TEACHIN_BIT = 0x08;
46     public static final byte LRN_TYPE_MASK = (byte) 0x80;
47
48     public long getDBByOffsetSizeValue(int offset, int size) {
49         if ((offset < 0 || offset > 31) || (size < 1 || size > 32 - offset)) {
50             logger.warn("4BSMessage get DB value by offset: {} and size: {}", offset, size);
51             return 0;
52         }
53
54         long msg = (((long) bytes[0] & 0xFF) << 24) | (((long) bytes[1] & 0xFF) << 16) | (((long) bytes[2] & 0xFF) << 8)
55                 | bytes[3];
56         msg = (msg >> (32 - offset - size)) & ((1 << size) - 1);
57
58         logger.debug("_4BSMessage get DB value message bytes {} {} {} {} resulted in {} with offset: {} and size: {}",
59                 bytes[0], bytes[1], bytes[2], bytes[3], msg, offset, size);
60
61         return msg;
62     }
63
64     public byte getDB0() {
65         return bytes[3];
66     }
67
68     public int getDB0Value() {
69         return (getDB0() & 0xFF);
70     }
71
72     public byte getDB1() {
73         return bytes[2];
74     }
75
76     public int getDB1Value() {
77         return (getDB1() & 0xFF);
78     }
79
80     public byte getDB2() {
81         return bytes[1];
82     }
83
84     public int getDB2Value() {
85         return (getDB2() & 0xFF);
86     }
87
88     public byte getDB3() {
89         return bytes[0];
90     }
91
92     public int getDB3Value() {
93         return (getDB3() & 0xFF);
94     }
95
96     @Override
97     protected void teachInQueryImpl(@Nullable Configuration config) {
98         if (config == null) {
99             return;
100         }
101
102         EnOceanChannelTeachInConfig c = config.as(EnOceanChannelTeachInConfig.class);
103         if (c.teachInMSG.isEmpty()) {
104             EEPType type = getEEPType();
105
106             byte db3 = (byte) ((getEEPType().getFunc() << 2) | ((type.getType()) >>> 5));
107             byte db2 = (byte) ((type.getType() << 3) & 0xff);
108             byte db1 = 0;
109
110             try {
111                 int manufId = (Integer.parseInt(c.manufacturerId, 16) & 0x7ff); // => 11 bit
112                 db2 += (manufId >>> 8);
113                 db1 += (manufId & 0xff);
114             } catch (Exception e) {
115             }
116
117             setData(db3, db2, db1, LRN_TYPE_MASK);
118         } else {
119             try {
120                 byte[] msg = HexUtils.hexToBytes(c.teachInMSG);
121                 setData(msg);
122             } catch (IllegalArgumentException e) {
123                 logger.debug("Command TeachIn could not transformed");
124                 throw e;
125             }
126         }
127     }
128 }