]> git.basschouten.com Git - openhab-addons.git/blob
df1faddcddea41f705e91c87d2e95f4765da79ce
[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 byte getDB0() {
49         return bytes[3];
50     }
51
52     public int getDB0Value() {
53         return (getDB0() & 0xFF);
54     }
55
56     public byte getDB1() {
57         return bytes[2];
58     }
59
60     public int getDB1Value() {
61         return (getDB1() & 0xFF);
62     }
63
64     public byte getDB2() {
65         return bytes[1];
66     }
67
68     public int getDB2Value() {
69         return (getDB2() & 0xFF);
70     }
71
72     public byte getDB3() {
73         return bytes[0];
74     }
75
76     public int getDB3Value() {
77         return (getDB3() & 0xFF);
78     }
79
80     @Override
81     protected void teachInQueryImpl(@Nullable Configuration config) {
82         if (config == null) {
83             return;
84         }
85
86         EnOceanChannelTeachInConfig c = config.as(EnOceanChannelTeachInConfig.class);
87         if (c.teachInMSG.isEmpty()) {
88             EEPType type = getEEPType();
89
90             byte db3 = (byte) ((getEEPType().getFunc() << 2) | ((type.getType()) >>> 5));
91             byte db2 = (byte) ((type.getType() << 3) & 0xff);
92             byte db1 = 0;
93
94             try {
95                 int manufId = (Integer.parseInt(c.manufacturerId, 16) & 0x7ff); // => 11 bit
96                 db2 += (manufId >>> 8);
97                 db1 += (manufId & 0xff);
98             } catch (Exception e) {
99             }
100
101             setData(db3, db2, db1, LRN_TYPE_MASK);
102         } else {
103             try {
104                 byte[] msg = HexUtils.hexToBytes(c.teachInMSG);
105                 setData(msg);
106             } catch (IllegalArgumentException e) {
107                 logger.debug("Command TeachIn could not transformed");
108                 throw e;
109             }
110         }
111     }
112 }