]> git.basschouten.com Git - openhab-addons.git/blob
faaf1bd956d5027548df3da2ae0a14984a159994
[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             if (bytes[0] != 127) {
88                 return new PercentType(bytes[0] & 0x7f);
89             }
90         }
91
92         return UnDefType.UNDEF;
93     }
94
95     protected byte getChannel() {
96         return (byte) (bytes[1] & outputChannelMask);
97     }
98
99     @Override
100     public void addConfigPropertiesTo(DiscoveryResultBuilder discoveredThingResultBuilder) {
101         discoveredThingResultBuilder.withProperty(PARAMETER_SENDINGEEPID, getEEPType().getId())
102                 .withProperty(PARAMETER_RECEIVINGEEPID, getEEPType().getId());
103     }
104
105     @Override
106     protected void convertFromCommandImpl(String channelId, String channelTypeId, Command command,
107             Function<String, State> getCurrentStateFunc, Configuration config) {
108         if (channelId.equals(CHANNEL_ROLLERSHUTTER)) {
109             if (command == RefreshType.REFRESH) {
110                 setPositionQueryData(ChannelA_Mask);
111             } else {
112                 setPositionData(command, ChannelA_Mask);
113             }
114         }
115     }
116
117     @Override
118     protected State convertToStateImpl(String channelId, String channelTypeId,
119             Function<String, State> getCurrentStateFunc, Configuration config) {
120         switch (channelId) {
121             case CHANNEL_ROLLERSHUTTER:
122                 return getPositionData();
123         }
124
125         return UnDefType.UNDEF;
126     }
127 }