]> git.basschouten.com Git - openhab-addons.git/blob
91ea0aa399684430f2ad7aba35cd8634527d721c
[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.D2_03;
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._VLDMessage;
22 import org.openhab.binding.enocean.internal.messages.ERP1Message;
23 import org.openhab.core.config.core.Configuration;
24 import org.openhab.core.library.types.QuantityType;
25 import org.openhab.core.library.unit.Units;
26 import org.openhab.core.thing.CommonTriggerEvents;
27 import org.openhab.core.types.State;
28 import org.openhab.core.types.UnDefType;
29
30 /**
31  *
32  * @author Daniel Weber - Initial contribution
33  */
34 @NonNullByDefault
35 public class D2_03_0A extends _VLDMessage {
36
37     protected static final byte SHORT_PRESS = 0x01;
38     protected static final byte DOUBLE_PRESS = 0x02;
39     protected static final byte LONG_PRESS = 0x03;
40     protected static final byte LONG_RELEASE = 0x04;
41
42     public D2_03_0A() {
43         super();
44     }
45
46     public D2_03_0A(ERP1Message packet) {
47         super(packet);
48     }
49
50     @Override
51     protected @Nullable String convertToEventImpl(String channelId, String channelTypeId, @Nullable String lastEvent,
52             Configuration config) {
53         switch (channelId) {
54             case CHANNEL_PUSHBUTTON:
55                 return (bytes[1] == SHORT_PRESS) ? CommonTriggerEvents.PRESSED : null;
56             case CHANNEL_DOUBLEPRESS:
57                 return (bytes[1] == DOUBLE_PRESS) ? CommonTriggerEvents.PRESSED : null;
58             case CHANNEL_LONGPRESS:
59                 return (bytes[1] == LONG_PRESS) ? CommonTriggerEvents.PRESSED
60                         : ((bytes[1] == LONG_RELEASE) ? CommonTriggerEvents.RELEASED : null);
61             default:
62                 return null;
63         }
64     }
65
66     @Override
67     public State convertToStateImpl(String channelId, String channelTypeId,
68             Function<String, @Nullable State> getCurrentStateFunc, Configuration config) {
69         if (CHANNEL_BATTERY_LEVEL.equals(channelId)) {
70             return new QuantityType<>(bytes[0] & 0xFF, Units.PERCENT);
71         }
72
73         return UnDefType.UNDEF;
74     }
75 }