2 * Copyright (c) 2010-2021 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.enocean.internal.eep.F6_02;
15 import static org.openhab.binding.enocean.internal.EnOceanBindingConstants.*;
17 import java.util.function.Function;
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;
34 * @author Daniel Weber - Initial contribution
36 public class F6_02_01 extends _RPSMessage {
42 final byte PRESSED = 16;
45 int secondStatus = -1;
51 public F6_02_01(ERP1Message packet) {
56 protected String convertToEventImpl(String channelId, String channelTypeId, String lastEvent,
57 Configuration config) {
60 byte dir1 = channelId.equals(CHANNEL_ROCKERSWITCH_CHANNELA) ? A0 : B0;
61 byte dir2 = channelId.equals(CHANNEL_ROCKERSWITCH_CHANNELA) ? AI : BI;
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;
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;
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;
87 if (s.equals(CommonTriggerEvents.DIR1_RELEASED) || s.equals(CommonTriggerEvents.DIR2_RELEASED)) {
88 setStatus(_RPSMessage.T21Flag);
93 byte dir1 = channelTypeId.equalsIgnoreCase(CHANNEL_VIRTUALROCKERSWITCHB) ? B0 : A0;
94 byte dir2 = channelTypeId.equalsIgnoreCase(CHANNEL_VIRTUALROCKERSWITCHB) ? BI : AI;
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));
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);
113 EnOceanChannelVirtualRockerSwitchConfig c = config.as(EnOceanChannelVirtualRockerSwitchConfig.class);
114 byte dir1 = c.getChannel() == Channel.ChannelA ? A0 : B0;
115 byte dir2 = c.getChannel() == Channel.ChannelA ? AI : BI;
117 // We are just listening on the pressed event here
118 switch (c.getSwitchMode()) {
120 if ((bytes[0] >>> 5) == dir1) {
121 if (((bytes[0] & PRESSED) != 0)) {
122 return channelTypeId.equals(CHANNEL_ROCKERSWITCHLISTENERSWITCH) ? OnOffType.ON
125 } else if ((bytes[0] >>> 5) == dir2) {
126 if (((bytes[0] & PRESSED) != 0)) {
127 return channelTypeId.equals(CHANNEL_ROCKERSWITCHLISTENERSWITCH) ? OnOffType.OFF
133 if ((bytes[0] >>> 5) == dir1) {
134 if (((bytes[0] & PRESSED) != 0)) {
135 return channelTypeId.equals(CHANNEL_ROCKERSWITCHLISTENERSWITCH)
136 ? (currentState == UnDefType.UNDEF ? OnOffType.ON
137 : inverse((OnOffType) currentState))
138 : (currentState == UnDefType.UNDEF ? UpDownType.UP
139 : inverse((UpDownType) currentState));
144 if ((bytes[0] >>> 5) == dir2) {
145 if (((bytes[0] & PRESSED) != 0)) {
146 return channelTypeId.equals(CHANNEL_ROCKERSWITCHLISTENERSWITCH)
147 ? (currentState == UnDefType.UNDEF ? OnOffType.ON
148 : inverse((OnOffType) currentState))
149 : (currentState == UnDefType.UNDEF ? UpDownType.UP
150 : inverse((UpDownType) currentState));
159 return UnDefType.UNDEF;
162 private State inverse(OnOffType currentState) {
163 return currentState == OnOffType.ON ? OnOffType.OFF : OnOffType.ON;
166 private State inverse(UpDownType currentState) {
167 return currentState == UpDownType.UP ? UpDownType.DOWN : UpDownType.UP;
171 protected boolean validateData(byte[] bytes) {
172 return super.validateData(bytes) && !getBit(bytes[0], 7);
176 public boolean isValidForTeachIn() {
178 // just treat press as teach in => DB0.4 has to be set
179 if (!getBit(bytes[0], 4)) {
182 // DB0.7 is never set for rocker switch message
183 if (getBit(bytes[0], 7)) {