]> git.basschouten.com Git - openhab-addons.git/blob
a4b9edb6695b230e1c38b2b6915353dfaf6510ea
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.enocean.internal.eep.A5_3F;
14
15 import static org.openhab.binding.enocean.internal.EnOceanBindingConstants.ZERO;
16
17 import java.util.function.Function;
18
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;
30
31 /**
32  *
33  * @author Andreas Hofinger - Initial contribution
34  */
35 @NonNullByDefault
36 public class A5_3F_7F_EltakoFRM extends _4BSMessage {
37
38     static final byte STOP = 0x00;
39     static final byte MOVE = 0x03;
40
41     static final int TOP = 0xC8;
42     static final int BOTTOM = 0x00;
43
44     public A5_3F_7F_EltakoFRM() {
45         super();
46     }
47
48     public A5_3F_7F_EltakoFRM(ERP1Message packet) {
49         super(packet);
50     }
51
52     @Override
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
65             }
66         } else if (command instanceof StopMoveType stopMoveCommand) {
67             if (stopMoveCommand == StopMoveType.STOP) {
68                 setData(ZERO, ZERO, STOP, TEACHIN_BIT);
69             }
70         }
71     }
72
73     @Override
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)))));
82         }
83         return UnDefType.UNDEF;
84     }
85 }