]> git.basschouten.com Git - openhab-addons.git/blob
4823f91e33065bf4a4611b58bad51dfe1e59ef3d
[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.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;
28
29 /**
30  *
31  * @author Andreas Hofinger
32  */
33 public class A5_3F_7F_EltakoFRM extends _4BSMessage {
34
35     static final byte Stop = 0x00;
36     static final byte Move = 0x03;
37
38     static final int Top = 0xC8;
39     static final int Bottom = 0x00;
40
41     public A5_3F_7F_EltakoFRM() {
42         super();
43     }
44
45     public A5_3F_7F_EltakoFRM(ERP1Message packet) {
46         super(packet);
47     }
48
49     @Override
50     protected void convertFromCommandImpl(String channelId, String channelTypeId, Command command,
51             Function<String, State> getCurrentStateFunc, Configuration config) {
52
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
64             }
65         } else if (command instanceof StopMoveType) {
66             if ((StopMoveType) command == StopMoveType.STOP) {
67                 setData(ZERO, ZERO, Stop, TeachInBit);
68             }
69         }
70     }
71
72     @Override
73     protected State convertToStateImpl(String channelId, String channelTypeId,
74             Function<String, State> getCurrentStateFunc, Configuration config) {
75
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)))));
82         }
83         return UnDefType.UNDEF;
84     }
85 }