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.openhab.binding.enocean.internal.config.EnOceanChannelRollershutterConfig;
20 import org.openhab.binding.enocean.internal.eep.Base._4BSMessage;
21 import org.openhab.binding.enocean.internal.messages.ERP1Message;
22 import org.openhab.core.config.core.Configuration;
23 import org.openhab.core.library.types.PercentType;
24 import org.openhab.core.library.types.StopMoveType;
25 import org.openhab.core.library.types.UpDownType;
26 import org.openhab.core.types.Command;
27 import org.openhab.core.types.State;
28 import org.openhab.core.types.UnDefType;
32 * @author Daniel Weber - Initial contribution
34 public class A5_3F_7F_EltakoFSB extends _4BSMessage {
36 static final byte Stop = 0x00;
37 static final byte MoveUp = 0x01;
38 static final byte MoveDown = 0x02;
40 static final byte Up = 0x70;
41 static final byte Down = 0x50;
43 public A5_3F_7F_EltakoFSB() {
47 public A5_3F_7F_EltakoFSB(ERP1Message packet) {
52 protected void convertFromCommandImpl(String channelId, String channelTypeId, Command command,
53 Function<String, State> getCurrentStateFunc, Configuration config) {
56 shutTime = Math.min(255, config.as(EnOceanChannelRollershutterConfig.class).shutTime);
59 if (command instanceof PercentType) {
60 State channelState = getCurrentStateFunc.apply(channelId);
62 PercentType target = (PercentType) command;
63 if (target.intValue() == PercentType.ZERO.intValue()) {
64 setData(ZERO, (byte) shutTime, MoveUp, TeachInBit); // => move completely up
65 } else if (target.intValue() == PercentType.HUNDRED.intValue()) {
66 setData(ZERO, (byte) shutTime, MoveDown, TeachInBit); // => move completely down
67 } else if (channelState != null) {
68 PercentType current = channelState.as(PercentType.class);
69 if (config != null && current != null) {
70 if (current.intValue() != target.intValue()) {
71 byte direction = current.intValue() > target.intValue() ? MoveUp : MoveDown;
72 byte duration = (byte) Math.min(255,
73 (Math.abs(current.intValue() - target.intValue()) * shutTime)
74 / PercentType.HUNDRED.intValue());
76 setData(ZERO, duration, direction, TeachInBit);
81 } else if (command instanceof UpDownType) {
82 if ((UpDownType) command == UpDownType.UP) {
83 setData(ZERO, (byte) shutTime, MoveUp, TeachInBit); // => 0 percent
84 } else if ((UpDownType) command == UpDownType.DOWN) {
85 setData(ZERO, (byte) shutTime, MoveDown, TeachInBit); // => 100 percent
87 } else if (command instanceof StopMoveType) {
88 if ((StopMoveType) command == StopMoveType.STOP) {
89 setData(ZERO, (byte) 0xFF, Stop, TeachInBit);
95 protected State convertToStateImpl(String channelId, String channelTypeId,
96 Function<String, State> getCurrentStateFunc, Configuration config) {
97 State currentState = getCurrentStateFunc.apply(channelId);
99 if (currentState != null) {
100 int duration = ((getDB_3Value() << 8) + getDB_2Value()) / 10; // => Time in DB3 and DB2 is given
103 if (config != null) {
104 EnOceanChannelRollershutterConfig c = config.as(EnOceanChannelRollershutterConfig.class);
105 if (duration == c.shutTime) {
106 return getDB_1() == MoveUp ? PercentType.ZERO : PercentType.HUNDRED;
108 PercentType current = PercentType.ZERO;
109 if (currentState instanceof PercentType) {
110 current = currentState.as(PercentType.class);
113 int direction = getDB_1() == MoveUp ? -1 : 1;
114 if (c.shutTime != -1 && c.shutTime != 0) {
115 return new PercentType(Math.min(100, (Math.max(0, current.intValue()
116 + direction * ((duration * PercentType.HUNDRED.intValue()) / c.shutTime)))));
122 return UnDefType.UNDEF;