2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.enocean.internal.eep.D2_05;
15 import static org.openhab.binding.enocean.internal.EnOceanBindingConstants.*;
17 import java.util.function.Function;
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;
35 * @author Daniel Weber - Initial contribution
38 public class D2_05_00 extends _VLDMessage {
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;
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;
49 protected static final byte ALL_CHANNELS_MASK = 0x1e;
50 protected static final byte CHANNEL_A_MASK = 0x00;
52 protected static final byte DOWN = 0x64; // 100%
53 protected static final byte UP = 0x00; // 0%
59 public D2_05_00(ERP1Message packet) {
63 protected byte getCMD() {
64 return (byte) (bytes[bytes.length - 1] & CMD_MASK);
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));
72 setData(UP, (byte) 0x00, (byte) 0x00, (byte) (outputChannel + CMD_ACTUATOR_SET_POSITION));
74 } else if (command instanceof StopMoveType) {
75 if (command == StopMoveType.STOP) {
76 setData((byte) (outputChannel + CMD_ACTUATOR_STOP));
78 } else if (command instanceof PercentType percentCommand) {
79 setData((byte) (percentCommand.intValue()), (byte) 0x00, (byte) 0x00,
80 (byte) (outputChannel + CMD_ACTUATOR_SET_POSITION));
84 protected void setPositionQueryData(byte outputChannel) {
85 setData((byte) (outputChannel + CMD_ACTUATOR_POSITION_QUERY));
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);
96 return UnDefType.UNDEF;
99 protected byte getChannel() {
100 return (byte) (bytes[1] & OUTPUT_CHANNEL_MASK);
104 public void addConfigPropertiesTo(DiscoveryResultBuilder discoveredThingResultBuilder) {
105 discoveredThingResultBuilder.withProperty(PARAMETER_SENDINGEEPID, getEEPType().getId())
106 .withProperty(PARAMETER_RECEIVINGEEPID, getEEPType().getId());
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);
116 setPositionData(command, CHANNEL_A_MASK);
122 protected State convertToStateImpl(String channelId, String channelTypeId,
123 Function<String, @Nullable State> getCurrentStateFunc, Configuration config) {
125 case CHANNEL_ROLLERSHUTTER:
126 return getPositionData();
129 return UnDefType.UNDEF;