]> git.basschouten.com Git - openhab-addons.git/blob
df9dc4450577461b19ca71580132f25b8a680aef
[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.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;
29
30 /**
31  *
32  * @author Daniel Weber - Initial contribution
33  */
34 public class A5_3F_7F_EltakoFSB extends _4BSMessage {
35
36     static final byte Stop = 0x00;
37     static final byte MoveUp = 0x01;
38     static final byte MoveDown = 0x02;
39
40     static final byte Up = 0x70;
41     static final byte Down = 0x50;
42
43     public A5_3F_7F_EltakoFSB() {
44         super();
45     }
46
47     public A5_3F_7F_EltakoFSB(ERP1Message packet) {
48         super(packet);
49     }
50
51     @Override
52     protected void convertFromCommandImpl(String channelId, String channelTypeId, Command command,
53             Function<String, State> getCurrentStateFunc, Configuration config) {
54         int shutTime = 0xFF;
55         if (config != null) {
56             shutTime = Math.min(255, config.as(EnOceanChannelRollershutterConfig.class).shutTime);
57         }
58
59         if (command instanceof PercentType) {
60             State channelState = getCurrentStateFunc.apply(channelId);
61
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());
75
76                         setData(ZERO, duration, direction, TeachInBit);
77                     }
78                 }
79             }
80
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
86             }
87         } else if (command instanceof StopMoveType) {
88             if ((StopMoveType) command == StopMoveType.STOP) {
89                 setData(ZERO, (byte) 0xFF, Stop, TeachInBit);
90             }
91         }
92     }
93
94     @Override
95     protected State convertToStateImpl(String channelId, String channelTypeId,
96             Function<String, State> getCurrentStateFunc, Configuration config) {
97         State currentState = getCurrentStateFunc.apply(channelId);
98
99         if (currentState != null) {
100             int duration = ((getDB_3Value() << 8) + getDB_2Value()) / 10; // => Time in DB3 and DB2 is given
101                                                                           // in ms
102
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;
107                 } else {
108                     PercentType current = PercentType.ZERO;
109                     if (currentState instanceof PercentType) {
110                         current = currentState.as(PercentType.class);
111                     }
112
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)))));
117                     }
118                 }
119             }
120         }
121
122         return UnDefType.UNDEF;
123     }
124 }