]> git.basschouten.com Git - openhab-addons.git/blob
514c07eab7e7c560b58e2c52cbd0433f118e6bce
[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.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.enocean.internal.config.EnOceanChannelRockerSwitchConfigBase.Channel;
22 import org.openhab.binding.enocean.internal.config.EnOceanChannelRockerSwitchListenerConfig;
23 import org.openhab.binding.enocean.internal.eep.Base._RPSMessage;
24 import org.openhab.binding.enocean.internal.messages.ERP1Message;
25 import org.openhab.core.config.core.Configuration;
26 import org.openhab.core.library.types.StringType;
27 import org.openhab.core.thing.CommonTriggerEvents;
28 import org.openhab.core.types.Command;
29 import org.openhab.core.types.State;
30 import org.openhab.core.types.UnDefType;
31
32 /**
33  *
34  * @author Daniel Weber - Initial contribution
35  */
36 @NonNullByDefault
37 public class F6_02_02 extends F6_02 {
38
39     public F6_02_02() {
40         super();
41     }
42
43     public F6_02_02(ERP1Message packet) {
44         super(packet);
45     }
46
47     @Override
48     protected @Nullable String convertToEventImpl(String channelId, String channelTypeId, String lastEvent,
49             Configuration config) {
50         if (t21 && nu) {
51             if (CHANNEL_ROCKERSWITCH_ACTION.equals(channelTypeId)) {
52                 return getRockerSwitchAction(config);
53             } else {
54                 byte dir1 = channelId.equals(CHANNEL_ROCKERSWITCH_CHANNELA) ? AI : BI;
55                 byte dir2 = channelId.equals(CHANNEL_ROCKERSWITCH_CHANNELA) ? A0 : B0;
56                 return getChannelEvent(dir1, dir2);
57             }
58         } else if (t21 && !nu) {
59             if (CHANNEL_ROCKERSWITCH_ACTION.equals(channelTypeId)) {
60                 return CommonTriggerEvents.RELEASED;
61             } else {
62                 if (lastEvent.equals(CommonTriggerEvents.DIR1_PRESSED)) {
63                     return CommonTriggerEvents.DIR1_RELEASED;
64                 } else if (lastEvent.equals(CommonTriggerEvents.DIR2_PRESSED)) {
65                     return CommonTriggerEvents.DIR2_RELEASED;
66                 }
67             }
68         }
69
70         return null;
71     }
72
73     @Override
74     protected void convertFromCommandImpl(String channelId, String channelTypeId, Command command,
75             Function<String, State> getCurrentStateFunc, @Nullable Configuration config) {
76         if (command instanceof StringType) {
77             String s = ((StringType) command).toString();
78
79             if (s.equals(CommonTriggerEvents.DIR1_RELEASED) || s.equals(CommonTriggerEvents.DIR2_RELEASED)) {
80                 setStatus(_RPSMessage.T21_FLAG);
81                 setData((byte) 0x00);
82                 return;
83             }
84
85             byte dir1 = channelTypeId.equalsIgnoreCase(CHANNEL_VIRTUALROCKERSWITCHB) ? BI : AI;
86             byte dir2 = channelTypeId.equalsIgnoreCase(CHANNEL_VIRTUALROCKERSWITCHB) ? B0 : A0;
87
88             if (s.equals(CommonTriggerEvents.DIR1_PRESSED)) {
89                 setStatus((byte) (_RPSMessage.T21_FLAG | _RPSMessage.NU_FLAG));
90                 setData((byte) ((dir1 << 5) | PRESSED));
91             } else if (s.equals(CommonTriggerEvents.DIR2_PRESSED)) {
92                 setStatus((byte) (_RPSMessage.T21_FLAG | _RPSMessage.NU_FLAG));
93                 setData((byte) ((dir2 << 5) | PRESSED));
94             }
95         }
96     }
97
98     @Override
99     protected State convertToStateImpl(String channelId, String channelTypeId,
100             Function<String, @Nullable State> getCurrentStateFunc, Configuration config) {
101         // this method is used by the classic device listener channels to convert a rocker switch message into an
102         // appropriate item update
103         State currentState = getCurrentStateFunc.apply(channelId);
104         if (t21 && nu && currentState != null) {
105             EnOceanChannelRockerSwitchListenerConfig c = config.as(EnOceanChannelRockerSwitchListenerConfig.class);
106             byte dir1 = c.getChannel() == Channel.ChannelA ? AI : BI;
107             byte dir2 = c.getChannel() == Channel.ChannelA ? A0 : B0;
108
109             return getState(dir1, dir2, c.handleSecondAction, c.getSwitchMode(), channelTypeId, currentState);
110         }
111
112         return UnDefType.UNDEF;
113     }
114
115     @Override
116     public boolean isValidForTeachIn() {
117         return false; // Never treat a message as F6-02-02, let user decide which orientation of rocker switch is used
118     }
119 }