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