]> git.basschouten.com Git - openhab-addons.git/blob
f3efce5145ed091ea1aee7e5fd31e49b54daf297
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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_14;
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.config.EnOceanChannelContactConfig;
20 import org.openhab.binding.enocean.internal.messages.ERP1Message;
21 import org.openhab.core.config.core.Configuration;
22 import org.openhab.core.library.types.OpenClosedType;
23 import org.openhab.core.library.types.StringType;
24 import org.openhab.core.types.State;
25 import org.openhab.core.types.UnDefType;
26
27 /**
28  * Window/Door-Sensor with States Open/Closed/Tilt, Supply voltage monitor
29  *
30  * @author Dominik Krickl-Vorreiter - Initial contribution
31  */
32 public class A5_14_09 extends A5_14 {
33     public final byte CLOSED = (byte) 0x00;
34     public final byte TILTED = (byte) 0x01;
35     public final byte OPEN = (byte) 0x03;
36
37     public A5_14_09(ERP1Message packet) {
38         super(packet);
39     }
40
41     private State getWindowhandleState() {
42         byte ct = (byte) ((getDB_0() & 0x06) >> 1);
43
44         switch (ct) {
45             case CLOSED:
46                 return new StringType("CLOSED");
47             case OPEN:
48                 return new StringType("OPEN");
49             case TILTED:
50                 return new StringType("TILTED");
51         }
52
53         return UnDefType.UNDEF;
54     }
55
56     private State getContact(boolean inverted) {
57         byte ct = (byte) ((getDB_0() & 0x06) >> 1);
58
59         switch (ct) {
60             case CLOSED:
61                 return inverted ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
62             case OPEN:
63             case TILTED:
64                 return inverted ? OpenClosedType.CLOSED : OpenClosedType.OPEN;
65         }
66
67         return UnDefType.UNDEF;
68     }
69
70     @Override
71     protected State convertToStateImpl(String channelId, String channelTypeId,
72             Function<String, State> getCurrentStateFunc, Configuration config) {
73         switch (channelId) {
74             case CHANNEL_WINDOWHANDLESTATE:
75                 return getWindowhandleState();
76             case CHANNEL_CONTACT:
77                 EnOceanChannelContactConfig c = config.as(EnOceanChannelContactConfig.class);
78                 return getContact(c.inverted);
79         }
80
81         return super.convertToStateImpl(channelId, channelTypeId, getCurrentStateFunc, config);
82     }
83 }