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