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.eep.Base._4BSMessage;
22 import org.openhab.binding.enocean.internal.messages.ERP1Message;
23 import org.openhab.core.config.core.Configuration;
24 import org.openhab.core.library.types.PercentType;
25 import org.openhab.core.library.types.StopMoveType;
26 import org.openhab.core.library.types.UpDownType;
27 import org.openhab.core.types.Command;
28 import org.openhab.core.types.State;
29 import org.openhab.core.types.UnDefType;
33 * @author Andreas Hofinger - Initial contribution
36 public class A5_3F_7F_EltakoFRM extends _4BSMessage {
38 static final byte STOP = 0x00;
39 static final byte MOVE = 0x03;
41 static final int TOP = 0xC8;
42 static final int BOTTOM = 0x00;
44 public A5_3F_7F_EltakoFRM() {
48 public A5_3F_7F_EltakoFRM(ERP1Message packet) {
53 protected void convertFromCommandImpl(String channelId, String channelTypeId, Command command,
54 Function<String, State> getCurrentStateFunc, @Nullable Configuration config) {
55 if (command instanceof PercentType percentCommand) {
56 int rawPosition = Math.round((PercentType.HUNDRED.floatValue() - percentCommand.floatValue()) * TOP
57 / PercentType.HUNDRED.floatValue());
58 int position = Math.min(TOP, Math.max(BOTTOM, rawPosition));
59 setData((byte) position, ZERO, MOVE, TEACHIN_BIT);
60 } else if (command instanceof UpDownType upDownCommand) {
61 if (upDownCommand == UpDownType.UP) {
62 setData((byte) TOP, ZERO, MOVE, TEACHIN_BIT); // => 0 percent
63 } else if (upDownCommand == UpDownType.DOWN) {
64 setData((byte) BOTTOM, ZERO, MOVE, TEACHIN_BIT); // => 100 percent
66 } else if (command instanceof StopMoveType stopMoveCommand) {
67 if (stopMoveCommand == StopMoveType.STOP) {
68 setData(ZERO, ZERO, STOP, TEACHIN_BIT);
74 protected State convertToStateImpl(String channelId, String channelTypeId,
75 Function<String, @Nullable State> getCurrentStateFunc, Configuration config) {
76 // 0x0A.. Move was locked for switch
77 // 0x0E.. Move was not locked
78 if (getDB2() == ZERO && getDB1() == MOVE && (getDB0() == 0x0A || getDB0() == 0x0E)) {
79 int position = getDB3Value();
80 float percentage = 100.0f * (TOP - position) / (float) (TOP - BOTTOM);
81 return new PercentType(Math.round(Math.min(100, (Math.max(0, percentage)))));
83 return UnDefType.UNDEF;