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.eep.Base._4BSMessage;
20 import org.openhab.binding.enocean.internal.messages.ERP1Message;
21 import org.openhab.core.config.core.Configuration;
22 import org.openhab.core.library.types.PercentType;
23 import org.openhab.core.library.types.StopMoveType;
24 import org.openhab.core.library.types.UpDownType;
25 import org.openhab.core.types.Command;
26 import org.openhab.core.types.State;
27 import org.openhab.core.types.UnDefType;
31 * @author Andreas Hofinger
33 public class A5_3F_7F_EltakoFRM extends _4BSMessage {
35 static final byte Stop = 0x00;
36 static final byte Move = 0x03;
38 static final int Top = 0xC8;
39 static final int Bottom = 0x00;
41 public A5_3F_7F_EltakoFRM() {
45 public A5_3F_7F_EltakoFRM(ERP1Message packet) {
50 protected void convertFromCommandImpl(String channelId, String channelTypeId, Command command,
51 Function<String, State> getCurrentStateFunc, Configuration config) {
53 if (command instanceof PercentType) {
54 PercentType target = (PercentType) command;
55 int rawPosition = Math.round(
56 (PercentType.HUNDRED.floatValue() - target.floatValue()) * Top / PercentType.HUNDRED.floatValue());
57 int position = Math.min(Top, Math.max(Bottom, rawPosition));
58 setData((byte) position, ZERO, Move, TeachInBit);
59 } else if (command instanceof UpDownType) {
60 if ((UpDownType) command == UpDownType.UP) {
61 setData((byte) Top, ZERO, Move, TeachInBit); // => 0 percent
62 } else if ((UpDownType) command == UpDownType.DOWN) {
63 setData((byte) Bottom, ZERO, Move, TeachInBit); // => 100 percent
65 } else if (command instanceof StopMoveType) {
66 if ((StopMoveType) command == StopMoveType.STOP) {
67 setData(ZERO, ZERO, Stop, TeachInBit);
73 protected State convertToStateImpl(String channelId, String channelTypeId,
74 Function<String, State> getCurrentStateFunc, Configuration config) {
76 // 0x0A.. Move was locked for switch
77 // 0x0E.. Move was not locked
78 if (getDB_2() == ZERO && getDB_1() == Move && (getDB_0() == 0x0A || getDB_0() == 0x0E)) {
79 int position = getDB_3Value();
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;