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.A5_3F;
15 import static org.openhab.binding.enocean.internal.EnOceanBindingConstants.ZERO;
17 import java.util.function.Function;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.enocean.internal.config.EnOceanChannelRollershutterConfig;
22 import org.openhab.binding.enocean.internal.eep.Base._4BSMessage;
23 import org.openhab.binding.enocean.internal.messages.ERP1Message;
24 import org.openhab.core.config.core.Configuration;
25 import org.openhab.core.library.types.PercentType;
26 import org.openhab.core.library.types.StopMoveType;
27 import org.openhab.core.library.types.UpDownType;
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
37 public class A5_3F_7F_EltakoFSB extends _4BSMessage {
39 static final byte STOP = 0x00;
40 static final byte MOVE_UP = 0x01;
41 static final byte MOVE_DOWN = 0x02;
43 static final byte UP = 0x70;
44 static final byte DOWN = 0x50;
46 public A5_3F_7F_EltakoFSB() {
50 public A5_3F_7F_EltakoFSB(ERP1Message packet) {
55 protected void convertFromCommandImpl(String channelId, String channelTypeId, Command command,
56 Function<String, State> getCurrentStateFunc, @Nullable Configuration config) {
59 logger.debug("No configuration, shutTime fallback to {}", shutTime);
61 shutTime = Math.min(255, config.as(EnOceanChannelRollershutterConfig.class).shutTime);
64 if (command instanceof PercentType) {
65 State channelState = getCurrentStateFunc.apply(channelId);
67 PercentType target = (PercentType) command;
68 if (target.intValue() == PercentType.ZERO.intValue()) {
69 setData(ZERO, (byte) shutTime, MOVE_UP, TEACHIN_BIT); // => move completely up
70 } else if (target.intValue() == PercentType.HUNDRED.intValue()) {
71 setData(ZERO, (byte) shutTime, MOVE_DOWN, TEACHIN_BIT); // => move completely down
72 } else if (channelState != null) {
73 PercentType current = channelState.as(PercentType.class);
74 if (current != null) {
75 if (current.intValue() != target.intValue()) {
76 byte direction = current.intValue() > target.intValue() ? MOVE_UP : MOVE_DOWN;
77 byte duration = (byte) Math.min(255,
78 (Math.abs(current.intValue() - target.intValue()) * shutTime)
79 / PercentType.HUNDRED.intValue());
81 setData(ZERO, duration, direction, TEACHIN_BIT);
86 } else if (command instanceof UpDownType) {
87 if ((UpDownType) command == UpDownType.UP) {
88 setData(ZERO, (byte) shutTime, MOVE_UP, TEACHIN_BIT); // => 0 percent
89 } else if ((UpDownType) command == UpDownType.DOWN) {
90 setData(ZERO, (byte) shutTime, MOVE_DOWN, TEACHIN_BIT); // => 100 percent
92 } else if (command instanceof StopMoveType) {
93 if ((StopMoveType) command == StopMoveType.STOP) {
94 setData(ZERO, (byte) 0xFF, STOP, TEACHIN_BIT);
100 protected State convertToStateImpl(String channelId, String channelTypeId,
101 Function<String, @Nullable State> getCurrentStateFunc, Configuration config) {
102 State currentState = getCurrentStateFunc.apply(channelId);
104 if (currentState != null) {
105 int duration = ((getDB3Value() << 8) + getDB2Value()) / 10; // => Time in DB3 and DB2 is given
107 EnOceanChannelRollershutterConfig c = config.as(EnOceanChannelRollershutterConfig.class);
108 if (duration == c.shutTime) {
109 return getDB1() == MOVE_UP ? PercentType.ZERO : PercentType.HUNDRED;
111 PercentType current = PercentType.ZERO;
112 if (currentState instanceof PercentType) {
113 current = currentState.as(PercentType.class);
116 int direction = getDB1() == MOVE_UP ? -1 : 1;
117 if (current != null && c.shutTime != -1 && c.shutTime != 0) {
118 return new PercentType(Math.min(100, (Math.max(0, current.intValue()
119 + direction * ((duration * PercentType.HUNDRED.intValue()) / c.shutTime)))));
124 return UnDefType.UNDEF;