]> git.basschouten.com Git - openhab-addons.git/blob
3d6e55d0c7378a24b675f81d3183ea95087829fe
[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) {
65             State channelState = getCurrentStateFunc.apply(channelId);
66
67             PercentType target = (PercentType) command;
68             if (target.intValue() == PercentType.ZERO.intValue()) {
69                 setData(ZERO, (byte) shutTime, MOVE_UP, TEACHIN_BIT); // => move completely up
70             } else if (target.intValue() == PercentType.HUNDRED.intValue()) {
71                 setData(ZERO, (byte) shutTime, MOVE_DOWN, TEACHIN_BIT); // => move completely down
72             } else if (channelState != null) {
73                 PercentType current = channelState.as(PercentType.class);
74                 if (current != null) {
75                     if (current.intValue() != target.intValue()) {
76                         byte direction = current.intValue() > target.intValue() ? MOVE_UP : MOVE_DOWN;
77                         byte duration = (byte) Math.min(255,
78                                 (Math.abs(current.intValue() - target.intValue()) * shutTime)
79                                         / PercentType.HUNDRED.intValue());
80
81                         setData(ZERO, duration, direction, TEACHIN_BIT);
82                     }
83                 }
84             }
85
86         } else if (command instanceof UpDownType) {
87             if ((UpDownType) command == UpDownType.UP) {
88                 setData(ZERO, (byte) shutTime, MOVE_UP, TEACHIN_BIT); // => 0 percent
89             } else if ((UpDownType) command == UpDownType.DOWN) {
90                 setData(ZERO, (byte) shutTime, MOVE_DOWN, TEACHIN_BIT); // => 100 percent
91             }
92         } else if (command instanceof StopMoveType) {
93             if ((StopMoveType) command == StopMoveType.STOP) {
94                 setData(ZERO, (byte) 0xFF, STOP, TEACHIN_BIT);
95             }
96         }
97     }
98
99     @Override
100     protected State convertToStateImpl(String channelId, String channelTypeId,
101             Function<String, @Nullable State> getCurrentStateFunc, Configuration config) {
102         State currentState = getCurrentStateFunc.apply(channelId);
103
104         if (currentState != null) {
105             int duration = ((getDB3Value() << 8) + getDB2Value()) / 10; // => Time in DB3 and DB2 is given
106                                                                         // in ms
107             EnOceanChannelRollershutterConfig c = config.as(EnOceanChannelRollershutterConfig.class);
108             if (duration == c.shutTime) {
109                 return getDB1() == MOVE_UP ? PercentType.ZERO : PercentType.HUNDRED;
110             } else {
111                 PercentType current = PercentType.ZERO;
112                 if (currentState instanceof PercentType) {
113                     current = currentState.as(PercentType.class);
114                 }
115
116                 int direction = getDB1() == MOVE_UP ? -1 : 1;
117                 if (current != null && c.shutTime != -1 && c.shutTime != 0) {
118                     return new PercentType(Math.min(100, (Math.max(0, current.intValue()
119                             + direction * ((duration * PercentType.HUNDRED.intValue()) / c.shutTime)))));
120                 }
121             }
122         }
123
124         return UnDefType.UNDEF;
125     }
126 }