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