2 * Copyright (c) 2010-2023 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 org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.enocean.internal.config.EnOceanChannelRockerSwitchConfigBase.SwitchMode;
20 import org.openhab.binding.enocean.internal.eep.Base._RPSMessage;
21 import org.openhab.binding.enocean.internal.messages.ERP1Message;
22 import org.openhab.core.config.core.Configuration;
23 import org.openhab.core.library.types.OnOffType;
24 import org.openhab.core.library.types.UpDownType;
25 import org.openhab.core.thing.CommonTriggerEvents;
26 import org.openhab.core.types.State;
27 import org.openhab.core.types.UnDefType;
31 * @author Daniel Weber - Initial contribution
34 public abstract class F6_02 extends _RPSMessage {
36 protected static final byte AI = 0;
37 protected static final byte A0 = 1;
38 protected static final byte BI = 2;
39 protected static final byte B0 = 3;
40 protected static final byte PRESSED = 16;
41 protected static final byte PRESSED_SEC = 1;
43 protected static final String DIR1 = "DIR1";
44 protected static final String DIR2 = "DIR2";
45 protected static final String NODIR = "-";
48 int secondStatus = -1;
54 public F6_02(ERP1Message packet) {
58 private String getChannelADir() {
59 if ((bytes[0] >>> 5) == A0 && (bytes[0] & PRESSED) != 0) {
61 } else if ((bytes[0] >>> 5) == AI && (bytes[0] & PRESSED) != 0) {
68 private String getChannelBDir() {
69 if ((bytes[0] >>> 5) == B0 && (bytes[0] & PRESSED) != 0) {
71 } else if ((bytes[0] >>> 5) == BI && (bytes[0] & PRESSED) != 0) {
73 } else if (((bytes[0] & 0xf) >>> 1) == B0 && (bytes[0] & PRESSED_SEC) != 0) {
75 } else if (((bytes[0] & 0xf) >>> 1) == BI && (bytes[0] & PRESSED_SEC) != 0) {
82 protected String getRockerSwitchAction(Configuration config) {
83 String dirA = getChannelADir();
84 String dirB = getChannelBDir();
86 return dirA + "|" + dirB;
89 protected @Nullable String getChannelEvent(byte dir1, byte dir2) {
90 if ((bytes[0] & PRESSED_SEC) != 0) {
91 // Do not emit an event if channelA is pressed together with channelB as it is undetermined which one gets
94 } else if ((bytes[0] >>> 5) == dir1) {
95 return ((bytes[0] & PRESSED) != 0) ? CommonTriggerEvents.DIR1_PRESSED : CommonTriggerEvents.DIR1_RELEASED;
96 } else if ((bytes[0] >>> 5) == dir2) {
97 return ((bytes[0] & PRESSED) != 0) ? CommonTriggerEvents.DIR2_PRESSED : CommonTriggerEvents.DIR2_RELEASED;
103 protected State getState(byte dir1, byte dir2, boolean handleSecondAction, SwitchMode switchMode,
104 String channelTypeId, State currentState) {
105 // We are just listening on the pressed event here
106 switch (switchMode) {
108 if ((bytes[0] >>> 5) == dir1) {
109 if (((bytes[0] & PRESSED) != 0)) {
110 return channelTypeId.equals(CHANNEL_ROCKERSWITCHLISTENERSWITCH) ? OnOffType.ON : UpDownType.UP;
112 } else if ((bytes[0] >>> 5) == dir2) {
113 if (((bytes[0] & PRESSED) != 0)) {
114 return channelTypeId.equals(CHANNEL_ROCKERSWITCHLISTENERSWITCH) ? OnOffType.OFF
117 } else if (handleSecondAction && ((bytes[0] & 0xf) >>> 1) == dir1) {
118 if (((bytes[0] & PRESSED_SEC) != 0)) {
119 return channelTypeId.equals(CHANNEL_ROCKERSWITCHLISTENERSWITCH) ? OnOffType.ON : UpDownType.UP;
121 } else if (handleSecondAction && ((bytes[0] & 0xf) >>> 1) == dir2) {
122 if (((bytes[0] & PRESSED_SEC) != 0)) {
123 return channelTypeId.equals(CHANNEL_ROCKERSWITCHLISTENERSWITCH) ? OnOffType.OFF
129 if ((bytes[0] >>> 5) == dir1) {
130 if (((bytes[0] & PRESSED) != 0)) {
131 return channelTypeId.equals(CHANNEL_ROCKERSWITCHLISTENERSWITCH)
132 ? (currentState == UnDefType.UNDEF ? OnOffType.ON : inverse((OnOffType) currentState))
133 : (currentState == UnDefType.UNDEF ? UpDownType.UP
134 : inverse((UpDownType) currentState));
136 } else if (handleSecondAction && ((bytes[0] & 0xf) >>> 1) == dir1) {
137 if (((bytes[0] & PRESSED_SEC) != 0)) {
138 return channelTypeId.equals(CHANNEL_ROCKERSWITCHLISTENERSWITCH)
139 ? (currentState == UnDefType.UNDEF ? OnOffType.ON : inverse((OnOffType) currentState))
140 : (currentState == UnDefType.UNDEF ? UpDownType.UP
141 : inverse((UpDownType) currentState));
146 if ((bytes[0] >>> 5) == dir2) {
147 if (((bytes[0] & PRESSED) != 0)) {
148 return channelTypeId.equals(CHANNEL_ROCKERSWITCHLISTENERSWITCH)
149 ? (currentState == UnDefType.UNDEF ? OnOffType.ON : inverse((OnOffType) currentState))
150 : (currentState == UnDefType.UNDEF ? UpDownType.UP
151 : inverse((UpDownType) currentState));
153 } else if (handleSecondAction && ((bytes[0] & 0xf) >>> 1) == dir2) {
154 if (((bytes[0] & PRESSED_SEC) != 0)) {
155 return channelTypeId.equals(CHANNEL_ROCKERSWITCHLISTENERSWITCH)
156 ? (currentState == UnDefType.UNDEF ? OnOffType.ON : inverse((OnOffType) currentState))
157 : (currentState == UnDefType.UNDEF ? UpDownType.UP
158 : inverse((UpDownType) currentState));
166 return UnDefType.UNDEF;
169 protected State inverse(OnOffType currentState) {
170 return currentState == OnOffType.ON ? OnOffType.OFF : OnOffType.ON;
173 protected State inverse(UpDownType currentState) {
174 return currentState == UpDownType.UP ? UpDownType.DOWN : UpDownType.UP;
178 protected boolean validateData(byte[] bytes) {
179 return super.validateData(bytes) && !getBit(bytes[0], 7);