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