]> git.basschouten.com Git - openhab-addons.git/blob
972b9e02b9a2c4d80d3576b1601788cd7c9f50c8
[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.D2_05;
14
15 import static org.openhab.binding.enocean.internal.EnOceanBindingConstants.*;
16
17 import java.util.function.Function;
18
19 import org.openhab.binding.enocean.internal.eep.Base._VLDMessage;
20 import org.openhab.binding.enocean.internal.messages.ERP1Message;
21 import org.openhab.core.config.core.Configuration;
22 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
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.RefreshType;
28 import org.openhab.core.types.State;
29 import org.openhab.core.types.UnDefType;
30
31 /**
32  *
33  * @author Daniel Weber - Initial contribution
34  */
35 public class D2_05_00 extends _VLDMessage {
36
37     protected final byte cmdMask = 0x0f;
38     protected final byte outputValueMask = 0x7f;
39     protected final byte outputChannelMask = 0x1f;
40
41     protected final byte CMD_ACTUATOR_SET_POSITION = 0x01;
42     protected final byte CMD_ACTUATOR_STOP = 0x02;
43     protected final byte CMD_ACTUATOR_POSITION_QUERY = 0x03;
44     protected final byte CMD_ACTUATOR_POSITION_RESPONE = 0x04;
45
46     protected final byte AllChannels_Mask = 0x1e;
47     protected final byte ChannelA_Mask = 0x00;
48
49     protected final byte DOWN = 0x64; // 100%
50     protected final byte UP = 0x00; // 0%
51
52     public D2_05_00() {
53         super();
54     }
55
56     public D2_05_00(ERP1Message packet) {
57         super(packet);
58     }
59
60     protected byte getCMD() {
61         return (byte) (bytes[bytes.length - 1] & cmdMask);
62     }
63
64     protected void setPositionData(Command command, byte outputChannel) {
65         if (command instanceof UpDownType) {
66             if (command == UpDownType.DOWN) {
67                 setData(DOWN, (byte) 0x00, (byte) 0x00, (byte) (outputChannel + CMD_ACTUATOR_SET_POSITION));
68             } else {
69                 setData(UP, (byte) 0x00, (byte) 0x00, (byte) (outputChannel + CMD_ACTUATOR_SET_POSITION));
70             }
71         } else if (command instanceof StopMoveType) {
72             if (command == StopMoveType.STOP) {
73                 setData((byte) (outputChannel + CMD_ACTUATOR_STOP));
74             }
75         } else if (command instanceof PercentType) {
76             setData((byte) (((PercentType) command).intValue()), (byte) 0x00, (byte) 0x00,
77                     (byte) (outputChannel + CMD_ACTUATOR_SET_POSITION));
78         }
79     }
80
81     protected void setPositionQueryData(byte outputChannel) {
82         setData((byte) (outputChannel + CMD_ACTUATOR_POSITION_QUERY));
83     }
84
85     protected State getPositionData() {
86         if (getCMD() == CMD_ACTUATOR_POSITION_RESPONE) {
87             int position = bytes[0] & 0x7f;
88             if (position != 127) {
89                 return new PercentType(position);
90             }
91         }
92
93         return UnDefType.UNDEF;
94     }
95
96     protected byte getChannel() {
97         return (byte) (bytes[1] & outputChannelMask);
98     }
99
100     @Override
101     public void addConfigPropertiesTo(DiscoveryResultBuilder discoveredThingResultBuilder) {
102         discoveredThingResultBuilder.withProperty(PARAMETER_SENDINGEEPID, getEEPType().getId())
103                 .withProperty(PARAMETER_RECEIVINGEEPID, getEEPType().getId());
104     }
105
106     @Override
107     protected void convertFromCommandImpl(String channelId, String channelTypeId, Command command,
108             Function<String, State> getCurrentStateFunc, Configuration config) {
109         if (channelId.equals(CHANNEL_ROLLERSHUTTER)) {
110             if (command == RefreshType.REFRESH) {
111                 setPositionQueryData(ChannelA_Mask);
112             } else {
113                 setPositionData(command, ChannelA_Mask);
114             }
115         }
116     }
117
118     @Override
119     protected State convertToStateImpl(String channelId, String channelTypeId,
120             Function<String, State> getCurrentStateFunc, Configuration config) {
121         switch (channelId) {
122             case CHANNEL_ROLLERSHUTTER:
123                 return getPositionData();
124         }
125
126         return UnDefType.UNDEF;
127     }
128 }