]> git.basschouten.com Git - openhab-addons.git/blob
e00d9dac70cd165d88414cc27aa832d6b31577a9
[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.A5_11;
14
15 import static org.openhab.binding.enocean.internal.EnOceanBindingConstants.*;
16
17 import java.util.function.Function;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.enocean.internal.eep.Base._4BSMessage;
22 import org.openhab.binding.enocean.internal.messages.ERP1Message;
23 import org.openhab.core.config.core.Configuration;
24 import org.openhab.core.library.types.PercentType;
25 import org.openhab.core.library.types.QuantityType;
26 import org.openhab.core.library.unit.Units;
27 import org.openhab.core.types.State;
28 import org.openhab.core.types.UnDefType;
29
30 /**
31  *
32  * @author Dominik Krickl-Vorreiter - Initial contribution
33  */
34 @NonNullByDefault
35 public class A5_11_03 extends _4BSMessage {
36
37     public A5_11_03(ERP1Message packet) {
38         super(packet);
39     }
40
41     protected boolean isErrorState() {
42         byte db1 = getDB1();
43
44         int state = (db1 >> 4) & 0x03;
45
46         // TODO: display error state on thing
47         return state != 0;
48     }
49
50     protected State getPositionData() {
51         byte db1 = getDB1();
52         boolean pvf = getBit(db1, 7);
53
54         if (pvf) {
55             byte db0 = getDB0();
56
57             boolean motp = getBit(db0, 6);
58             int bsp = getDB3Value();
59
60             if ((bsp >= 0) && (bsp <= 100)) {
61                 return new PercentType(motp ? 100 - bsp : bsp);
62             }
63         }
64
65         return UnDefType.UNDEF;
66     }
67
68     protected State getAngleData() {
69         byte db1 = getDB1();
70         boolean avf = getBit(db1, 6);
71
72         if (avf) {
73             byte db2 = getDB2();
74
75             boolean as = getBit(db2, 7);
76             int an = (db2 & 0x7F) * 2;
77
78             if ((an >= 0) && (an <= 180)) {
79                 return new QuantityType<>(as ? an * -1 : an, Units.DEGREE_ANGLE);
80             }
81         }
82
83         return UnDefType.UNDEF;
84     }
85
86     @Override
87     protected State convertToStateImpl(String channelId, String channelTypeId,
88             Function<String, @Nullable State> getCurrentStateFunc, Configuration config) {
89         if (isErrorState()) {
90             return UnDefType.UNDEF;
91         }
92
93         switch (channelId) {
94             case CHANNEL_ROLLERSHUTTER:
95                 return getPositionData();
96             case CHANNEL_ANGLE:
97                 return getAngleData();
98         }
99
100         return UnDefType.UNDEF;
101     }
102 }