]> git.basschouten.com Git - openhab-addons.git/blob
67f016f171cd9e2d086bcc5df7c06ea0bc1deeff
[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.config.EnOceanChannelRollershutterConfig;
22 import org.openhab.binding.enocean.internal.eep.Base._4BSMessage;
23 import org.openhab.binding.enocean.internal.messages.ERP1Message;
24 import org.openhab.core.config.core.Configuration;
25 import org.openhab.core.library.types.PercentType;
26 import org.openhab.core.library.types.StopMoveType;
27 import org.openhab.core.library.types.UpDownType;
28 import org.openhab.core.types.Command;
29 import org.openhab.core.types.State;
30 import org.openhab.core.types.UnDefType;
31
32 /**
33  *
34  * @author Daniel Weber - Initial contribution
35  */
36 @NonNullByDefault
37 public class A5_3F_7F_EltakoFSB extends _4BSMessage {
38
39     static final byte STOP = 0x00;
40     static final byte MOVE_UP = 0x01;
41     static final byte MOVE_DOWN = 0x02;
42
43     static final byte UP = 0x70;
44     static final byte DOWN = 0x50;
45
46     public A5_3F_7F_EltakoFSB() {
47         super();
48     }
49
50     public A5_3F_7F_EltakoFSB(ERP1Message packet) {
51         super(packet);
52     }
53
54     @Override
55     protected void convertFromCommandImpl(String channelId, String channelTypeId, Command command,
56             Function<String, State> getCurrentStateFunc, @Nullable Configuration config) {
57         int shutTime = 0xFF;
58         if (config == null) {
59             logger.debug("No configuration, shutTime fallback to {}", shutTime);
60         } else {
61             shutTime = Math.min(255, config.as(EnOceanChannelRollershutterConfig.class).shutTime);
62         }
63
64         if (command instanceof PercentType percentCommand) {
65             State channelState = getCurrentStateFunc.apply(channelId);
66             if (percentCommand.intValue() == PercentType.ZERO.intValue()) {
67                 setData(ZERO, (byte) shutTime, MOVE_UP, TEACHIN_BIT); // => move completely up
68             } else if (percentCommand.intValue() == PercentType.HUNDRED.intValue()) {
69                 setData(ZERO, (byte) shutTime, MOVE_DOWN, TEACHIN_BIT); // => move completely down
70             } else if (channelState != null) {
71                 PercentType current = channelState.as(PercentType.class);
72                 if (current != null) {
73                     if (current.intValue() != percentCommand.intValue()) {
74                         byte direction = current.intValue() > percentCommand.intValue() ? MOVE_UP : MOVE_DOWN;
75                         byte duration = (byte) Math.min(255,
76                                 (Math.abs(current.intValue() - percentCommand.intValue()) * shutTime)
77                                         / PercentType.HUNDRED.intValue());
78
79                         setData(ZERO, duration, direction, TEACHIN_BIT);
80                     }
81                 }
82             }
83
84         } else if (command instanceof UpDownType upDownCommand) {
85             if (upDownCommand == UpDownType.UP) {
86                 setData(ZERO, (byte) shutTime, MOVE_UP, TEACHIN_BIT); // => 0 percent
87             } else if (upDownCommand == UpDownType.DOWN) {
88                 setData(ZERO, (byte) shutTime, MOVE_DOWN, TEACHIN_BIT); // => 100 percent
89             }
90         } else if (command instanceof StopMoveType stopMoveCommand) {
91             if (stopMoveCommand == StopMoveType.STOP) {
92                 setData(ZERO, (byte) 0xFF, STOP, TEACHIN_BIT);
93             }
94         }
95     }
96
97     @Override
98     protected State convertToStateImpl(String channelId, String channelTypeId,
99             Function<String, @Nullable State> getCurrentStateFunc, Configuration config) {
100         State currentState = getCurrentStateFunc.apply(channelId);
101
102         if (currentState != null) {
103             int duration = ((getDB3Value() << 8) + getDB2Value()) / 10; // => Time in DB3 and DB2 is given
104                                                                         // in ms
105             EnOceanChannelRollershutterConfig c = config.as(EnOceanChannelRollershutterConfig.class);
106             if (duration == c.shutTime) {
107                 return getDB1() == MOVE_UP ? PercentType.ZERO : PercentType.HUNDRED;
108             } else {
109                 PercentType current = PercentType.ZERO;
110                 if (currentState instanceof PercentType) {
111                     current = currentState.as(PercentType.class);
112                 }
113
114                 int direction = getDB1() == MOVE_UP ? -1 : 1;
115                 if (current != null && c.shutTime != -1 && c.shutTime != 0) {
116                     return new PercentType(Math.min(100, (Math.max(0, current.intValue()
117                             + direction * ((duration * PercentType.HUNDRED.intValue()) / c.shutTime)))));
118                 }
119             }
120         }
121
122         return UnDefType.UNDEF;
123     }
124 }