]> git.basschouten.com Git - openhab-addons.git/blob
49b8b31dfc92d7a9eb39582b67bf68068a15fd4c
[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         if (state != 0) {
44             // TODO: display error state on thing
45             return true;
46         } else {
47             return false;
48         }
49     }
50
51     protected State getPositionData() {
52         byte db1 = getDB_1();
53         boolean pvf = getBit(db1, 7);
54
55         if (pvf) {
56             byte db0 = getDB_0();
57
58             boolean motp = getBit(db0, 6);
59             int bsp = getDB_3Value();
60
61             if ((bsp >= 0) && (bsp <= 100)) {
62                 return new PercentType(motp ? 100 - bsp : bsp);
63             }
64         }
65
66         return UnDefType.UNDEF;
67     }
68
69     protected State getAngleData() {
70         byte db1 = getDB_1();
71         boolean avf = getBit(db1, 6);
72
73         if (avf) {
74             byte db2 = getDB_2();
75
76             boolean as = getBit(db2, 7);
77             int an = (db2 & 0x7F) * 2;
78
79             if ((an >= 0) && (an <= 180)) {
80                 return new QuantityType<>(as ? an * -1 : an, Units.DEGREE_ANGLE);
81             }
82         }
83
84         return UnDefType.UNDEF;
85     }
86
87     @Override
88     protected State convertToStateImpl(String channelId, String channelTypeId,
89             Function<String, State> getCurrentStateFunc, Configuration config) {
90         if (isErrorState()) {
91             return UnDefType.UNDEF;
92         }
93
94         switch (channelId) {
95             case CHANNEL_ROLLERSHUTTER:
96                 return getPositionData();
97             case CHANNEL_ANGLE:
98                 return getAngleData();
99         }
100
101         return UnDefType.UNDEF;
102     }
103 }