]> git.basschouten.com Git - openhab-addons.git/blob
d4d5fe1de7690d81b7508d4d8853deb6ddcbb7b1
[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.F6_02;
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.EnOceanChannelRockerSwitchConfigBase.Channel;
20 import org.openhab.binding.enocean.internal.config.EnOceanChannelRockerSwitchListenerConfig;
21 import org.openhab.binding.enocean.internal.eep.Base._RPSMessage;
22 import org.openhab.binding.enocean.internal.messages.ERP1Message;
23 import org.openhab.core.config.core.Configuration;
24 import org.openhab.core.library.types.StringType;
25 import org.openhab.core.thing.CommonTriggerEvents;
26 import org.openhab.core.types.Command;
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 public class F6_02_01 extends F6_02 {
35
36     public F6_02_01() {
37         super();
38     }
39
40     public F6_02_01(ERP1Message packet) {
41         super(packet);
42     }
43
44     @Override
45     protected String convertToEventImpl(String channelId, String channelTypeId, String lastEvent,
46             Configuration config) {
47
48         if (t21 && nu) {
49             if (CHANNEL_ROCKERSWITCH_ACTION.equals(channelTypeId)) {
50                 return getRockerSwitchAction(config);
51             } else {
52                 byte dir1 = channelId.equals(CHANNEL_ROCKERSWITCH_CHANNELA) ? A0 : B0;
53                 byte dir2 = channelId.equals(CHANNEL_ROCKERSWITCH_CHANNELA) ? AI : BI;
54
55                 return getChannelEvent(dir1, dir2);
56             }
57         } else if (t21 && !nu) {
58             if (CHANNEL_ROCKERSWITCH_ACTION.equals(channelTypeId)) {
59                 return CommonTriggerEvents.RELEASED;
60             } else {
61                 if (lastEvent != null && lastEvent.equals(CommonTriggerEvents.DIR1_PRESSED)) {
62                     return CommonTriggerEvents.DIR1_RELEASED;
63                 } else if (lastEvent != null && lastEvent.equals(CommonTriggerEvents.DIR2_PRESSED)) {
64                     return CommonTriggerEvents.DIR2_RELEASED;
65                 }
66             }
67         }
68
69         return null;
70     }
71
72     @Override
73     protected void convertFromCommandImpl(String channelId, String channelTypeId, Command command,
74             Function<String, State> getCurrentStateFunc, Configuration config) {
75         if (command instanceof StringType) {
76             String s = ((StringType) command).toString();
77
78             if (s.equals(CommonTriggerEvents.DIR1_RELEASED) || s.equals(CommonTriggerEvents.DIR2_RELEASED)) {
79                 setStatus(_RPSMessage.T21Flag);
80                 setData((byte) 0x00);
81                 return;
82             }
83
84             byte dir1 = channelTypeId.equalsIgnoreCase(CHANNEL_VIRTUALROCKERSWITCHB) ? B0 : A0;
85             byte dir2 = channelTypeId.equalsIgnoreCase(CHANNEL_VIRTUALROCKERSWITCHB) ? BI : AI;
86
87             if (s.equals(CommonTriggerEvents.DIR1_PRESSED)) {
88                 setStatus((byte) (_RPSMessage.T21Flag | _RPSMessage.NUFlag));
89                 setData((byte) ((dir1 << 5) | PRESSED));
90             } else if (s.equals(CommonTriggerEvents.DIR2_PRESSED)) {
91                 setStatus((byte) (_RPSMessage.T21Flag | _RPSMessage.NUFlag));
92                 setData((byte) ((dir2 << 5) | PRESSED));
93             }
94         }
95     }
96
97     @Override
98     protected State convertToStateImpl(String channelId, String channelTypeId,
99             Function<String, State> getCurrentStateFunc, Configuration config) {
100         // this method is used by the classic device listener channels to convert a rocker switch message into an
101         // appropriate item update
102         State currentState = getCurrentStateFunc.apply(channelId);
103         if (t21 && nu) {
104             EnOceanChannelRockerSwitchListenerConfig c = config.as(EnOceanChannelRockerSwitchListenerConfig.class);
105             byte dir1 = c.getChannel() == Channel.ChannelA ? A0 : B0;
106             byte dir2 = c.getChannel() == Channel.ChannelA ? AI : BI;
107
108             return getState(dir1, dir2, c.handleSecondAction, c.getSwitchMode(), channelTypeId, currentState);
109         }
110
111         return UnDefType.UNDEF;
112     }
113
114     @Override
115     public boolean isValidForTeachIn() {
116         if (t21) {
117             // just treat press as teach in => DB0.4 has to be set
118             if (!getBit(bytes[0], 4)) {
119                 return false;
120             }
121             // DB0.7 is never set for rocker switch message
122             if (getBit(bytes[0], 7)) {
123                 return false;
124             }
125         } else {
126             return false;
127         }
128
129         return true;
130     }
131 }