]> git.basschouten.com Git - openhab-addons.git/blob
4cb89535842b60abe45426b495b928d7dec8c526
[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_02 extends F6_02 {
35
36     public F6_02_02() {
37         super();
38     }
39
40     public F6_02_02(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) ? AI : BI;
53                 byte dir2 = channelId.equals(CHANNEL_ROCKERSWITCH_CHANNELA) ? A0 : B0;
54                 return getChannelEvent(dir1, dir2);
55             }
56         } else if (t21 && !nu) {
57             if (CHANNEL_ROCKERSWITCH_ACTION.equals(channelTypeId)) {
58                 return CommonTriggerEvents.RELEASED;
59             } else {
60                 if (lastEvent != null && lastEvent.equals(CommonTriggerEvents.DIR1_PRESSED)) {
61                     return CommonTriggerEvents.DIR1_RELEASED;
62                 } else if (lastEvent != null && lastEvent.equals(CommonTriggerEvents.DIR2_PRESSED)) {
63                     return CommonTriggerEvents.DIR2_RELEASED;
64                 }
65             }
66         }
67
68         return null;
69     }
70
71     @Override
72     protected void convertFromCommandImpl(String channelId, String channelTypeId, Command command,
73             Function<String, State> getCurrentStateFunc, Configuration config) {
74         if (command instanceof StringType) {
75             String s = ((StringType) command).toString();
76
77             if (s.equals(CommonTriggerEvents.DIR1_RELEASED) || s.equals(CommonTriggerEvents.DIR2_RELEASED)) {
78                 setStatus(_RPSMessage.T21Flag);
79                 setData((byte) 0x00);
80                 return;
81             }
82
83             byte dir1 = channelTypeId.equalsIgnoreCase(CHANNEL_VIRTUALROCKERSWITCHB) ? BI : AI;
84             byte dir2 = channelTypeId.equalsIgnoreCase(CHANNEL_VIRTUALROCKERSWITCHB) ? B0 : A0;
85
86             if (s.equals(CommonTriggerEvents.DIR1_PRESSED)) {
87                 setStatus((byte) (_RPSMessage.T21Flag | _RPSMessage.NUFlag));
88                 setData((byte) ((dir1 << 5) | PRESSED));
89             } else if (s.equals(CommonTriggerEvents.DIR2_PRESSED)) {
90                 setStatus((byte) (_RPSMessage.T21Flag | _RPSMessage.NUFlag));
91                 setData((byte) ((dir2 << 5) | PRESSED));
92             }
93         }
94     }
95
96     @Override
97     protected State convertToStateImpl(String channelId, String channelTypeId,
98             Function<String, State> getCurrentStateFunc, Configuration config) {
99         // this method is used by the classic device listener channels to convert a rocker switch message into an
100         // appropriate item update
101         State currentState = getCurrentStateFunc.apply(channelId);
102         if (t21 && nu) {
103             EnOceanChannelRockerSwitchListenerConfig c = config.as(EnOceanChannelRockerSwitchListenerConfig.class);
104             byte dir1 = c.getChannel() == Channel.ChannelA ? AI : BI;
105             byte dir2 = c.getChannel() == Channel.ChannelA ? A0 : B0;
106
107             return getState(dir1, dir2, c.handleSecondAction, c.getSwitchMode(), channelTypeId, currentState);
108         }
109
110         return UnDefType.UNDEF;
111     }
112
113     @Override
114     public boolean isValidForTeachIn() {
115         return false; // Never treat a message as F6-02-02, let user decide which orientation of rocker switch is used
116     }
117 }