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