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