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