]> git.basschouten.com Git - openhab-addons.git/blob
76e169785171af219d194de8dd6b7d12601c8a8b
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.EnOceanChannelVirtualRockerSwitchConfig;
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.OnOffType;
25 import org.openhab.core.library.types.StringType;
26 import org.openhab.core.library.types.UpDownType;
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 public class F6_02_02 extends _RPSMessage {
37
38     final byte AI = 0;
39     final byte A0 = 1;
40     final byte BI = 2;
41     final byte B0 = 3;
42     final byte PRESSED = 16;
43
44     public F6_02_02() {
45         super();
46     }
47
48     public F6_02_02(ERP1Message packet) {
49         super(packet);
50     }
51
52     @Override
53     protected String convertToEventImpl(String channelId, String channelTypeId, String lastEvent,
54             Configuration config) {
55
56         if (t21 && nu) {
57             byte dir1 = channelId.equals(CHANNEL_ROCKERSWITCH_CHANNELA) ? AI : BI;
58             byte dir2 = channelId.equals(CHANNEL_ROCKERSWITCH_CHANNELA) ? A0 : B0;
59
60             if ((bytes[0] >>> 5) == dir1) {
61                 return ((bytes[0] & PRESSED) != 0) ? CommonTriggerEvents.DIR1_PRESSED
62                         : CommonTriggerEvents.DIR1_RELEASED;
63             } else if ((bytes[0] >>> 5) == dir2) {
64                 return ((bytes[0] & PRESSED) != 0) ? CommonTriggerEvents.DIR2_PRESSED
65                         : CommonTriggerEvents.DIR2_RELEASED;
66             }
67         } else if (t21 && !nu) {
68             if (lastEvent != null && lastEvent.equals(CommonTriggerEvents.DIR1_PRESSED)) {
69                 return CommonTriggerEvents.DIR1_RELEASED;
70             } else if (lastEvent != null && lastEvent.equals(CommonTriggerEvents.DIR2_PRESSED)) {
71                 return CommonTriggerEvents.DIR2_RELEASED;
72             }
73         }
74
75         return null;
76     }
77
78     @Override
79     protected void convertFromCommandImpl(String channelId, String channelTypeId, Command command,
80             Function<String, State> getCurrentStateFunc, Configuration config) {
81         if (command instanceof StringType) {
82             StringType s = (StringType) command;
83
84             if (s.equals(CommonTriggerEvents.DIR1_RELEASED) || s.equals(CommonTriggerEvents.DIR2_RELEASED)) {
85                 setStatus(_RPSMessage.T21Flag);
86                 setData((byte) 0x00);
87                 return;
88             }
89
90             byte dir1 = channelTypeId.equalsIgnoreCase(CHANNEL_VIRTUALROCKERSWITCHB) ? BI : AI;
91             byte dir2 = channelTypeId.equalsIgnoreCase(CHANNEL_VIRTUALROCKERSWITCHB) ? B0 : A0;
92
93             if (s.equals(CommonTriggerEvents.DIR1_PRESSED)) {
94                 setStatus((byte) (_RPSMessage.T21Flag | _RPSMessage.NUFlag));
95                 setData((byte) ((dir1 << 5) | PRESSED));
96             } else if (s.equals(CommonTriggerEvents.DIR2_PRESSED)) {
97                 setStatus((byte) (_RPSMessage.T21Flag | _RPSMessage.NUFlag));
98                 setData((byte) ((dir2 << 5) | PRESSED));
99             }
100         }
101     }
102
103     @Override
104     protected State convertToStateImpl(String channelId, String channelTypeId,
105             Function<String, State> getCurrentStateFunc, Configuration config) {
106         // this method is used by the classic device listener channels to convert an rocker switch message into an
107         // appropriate item update
108         State currentState = getCurrentStateFunc.apply(channelId);
109         if (t21 && nu) {
110             EnOceanChannelVirtualRockerSwitchConfig c = config.as(EnOceanChannelVirtualRockerSwitchConfig.class);
111             byte dir1 = c.getChannel() == Channel.ChannelA ? AI : BI;
112             byte dir2 = c.getChannel() == Channel.ChannelA ? A0 : B0;
113
114             // We are just listening on the pressed event here
115             switch (c.getSwitchMode()) {
116                 case RockerSwitch:
117                     if ((bytes[0] >>> 5) == dir1) {
118                         if (((bytes[0] & PRESSED) != 0)) {
119                             return channelTypeId.equals(CHANNEL_ROCKERSWITCHLISTENERSWITCH) ? OnOffType.ON
120                                     : UpDownType.UP;
121                         }
122                     } else if ((bytes[0] >>> 5) == dir2) {
123                         if (((bytes[0] & PRESSED) != 0)) {
124                             return channelTypeId.equals(CHANNEL_ROCKERSWITCHLISTENERSWITCH) ? OnOffType.OFF
125                                     : UpDownType.DOWN;
126                         }
127                     }
128                     break;
129                 case ToggleDir1:
130                     if ((bytes[0] >>> 5) == dir1) {
131                         if (((bytes[0] & PRESSED) != 0)) {
132                             return channelTypeId.equals(CHANNEL_ROCKERSWITCHLISTENERSWITCH)
133                                     ? (currentState == UnDefType.UNDEF ? OnOffType.ON
134                                             : inverse((OnOffType) currentState))
135                                     : (currentState == UnDefType.UNDEF ? UpDownType.UP
136                                             : inverse((UpDownType) currentState));
137                         }
138                     }
139                     break;
140                 case ToggleDir2:
141                     if ((bytes[0] >>> 5) == dir2) {
142                         if (((bytes[0] & PRESSED) != 0)) {
143                             return channelTypeId.equals(CHANNEL_ROCKERSWITCHLISTENERSWITCH)
144                                     ? (currentState == UnDefType.UNDEF ? OnOffType.ON
145                                             : inverse((OnOffType) currentState))
146                                     : (currentState == UnDefType.UNDEF ? UpDownType.UP
147                                             : inverse((UpDownType) currentState));
148                         }
149                     }
150                     break;
151                 default:
152                     break;
153             }
154         }
155
156         return UnDefType.UNDEF;
157     }
158
159     private State inverse(OnOffType currentState) {
160         return currentState == OnOffType.ON ? OnOffType.OFF : OnOffType.ON;
161     }
162
163     private State inverse(UpDownType currentState) {
164         return currentState == UpDownType.UP ? UpDownType.DOWN : UpDownType.UP;
165     }
166
167     @Override
168     public boolean isValidForTeachIn() {
169         return false; // Never treat a message as F6-02-02, let user decide which orientation of rocker switch is used
170     }
171 }