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.A5_38;
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._4BSMessage;
22 import org.openhab.binding.enocean.internal.messages.ERP1Message;
23 import org.openhab.core.config.core.Configuration;
24 import org.openhab.core.library.types.DecimalType;
25 import org.openhab.core.library.types.OnOffType;
26 import org.openhab.core.library.types.PercentType;
27 import org.openhab.core.library.types.QuantityType;
28 import org.openhab.core.library.types.StopMoveType;
29 import org.openhab.core.library.types.UpDownType;
30 import org.openhab.core.library.unit.Units;
31 import org.openhab.core.types.Command;
32 import org.openhab.core.types.State;
33 import org.openhab.core.types.UnDefType;
37 * @author Daniel Weber - Initial contribution
40 public class A5_38_08_Blinds extends _4BSMessage {
42 static final byte COMMAND_ID = 0x07;
43 static final byte POSITION_AND_ANGLE_AVAILABLE = 0x02;
44 static final byte SEND_NEW_STATE = 0x04;
46 static final byte FUNCTION_STATUS_REQUEST = 0x00;
47 static final byte FUNCTION_BLIND_STOPS = 0x01;
48 static final byte FUNCTION_BLIND_OPENS = 0x02;
49 static final byte FUNCTION_BLIND_CLOSES = 0x03;
50 static final byte FUNCTION_BLIND_POSITION_ANGLE = 0x04;
51 static final byte FUNCTION_BLIND_OPENS_FOR_TIME = 0x05;
52 static final byte FUNCTION_BLIND_CLOSES_FOR_TIME = 0x06;
53 static final byte FUNCTION_SET_RUNTIME_PARAMETERS = 0x07;
54 static final byte FUNCTION_SET_ANGLE_CONFIGURATIONE = 0x08;
55 static final byte FUNCTION_SET_MIN_MAX = 0x09;
56 static final byte FUNCTION_SET_SLAT_ANGLE = 0x0A;
57 static final byte FUNCTION_SET_POSITION_LOGIC = 0x0B;
59 public A5_38_08_Blinds() {
63 public A5_38_08_Blinds(ERP1Message packet) {
68 protected void convertFromCommandImpl(String channelId, String channelTypeId, Command outputCommand,
69 Function<String, State> getCurrentStateFunc, @Nullable Configuration config) {
71 case CHANNEL_ROLLERSHUTTER:
72 byte db0 = ZERO | SEND_NEW_STATE | TEACHIN_BIT;
77 byte angle = 0; // for now, no angle configuration supported
78 boolean doStop = false;
80 if (outputCommand instanceof DecimalType decimalCommand) {
81 position = decimalCommand.byteValue();
82 } else if (outputCommand instanceof OnOffType onOffCommand) {
83 position = (byte) ((onOffCommand == OnOffType.ON) ? 0 : 100);
84 } else if (outputCommand instanceof StopMoveType) {
87 } else if (outputCommand instanceof UpDownType upDownCommand) {
88 position = (byte) ((upDownCommand == UpDownType.UP) ? 0 : 100);
90 logger.warn("Unknown command type {}", outputCommand.getClass().getCanonicalName());
95 db0 |= FUNCTION_BLIND_STOPS << 4;
96 } else if (position <= 0) {
97 db0 |= FUNCTION_BLIND_OPENS << 4;
98 } else if (position >= 100) {
99 db0 |= FUNCTION_BLIND_CLOSES << 4;
101 db0 |= (FUNCTION_BLIND_POSITION_ANGLE << 4) | POSITION_AND_ANGLE_AVAILABLE;
104 db1 = (byte) ((0x01 << 7) | ((angle / -2) & 0x7F));
106 db1 = (byte) (((angle / 2) & 0x7F));
112 setData(COMMAND_ID, db2, db1, db0);
115 return; // for now, no angle configuration supported
119 protected State getPositionData() {
121 boolean paf = getBit(db0, 1);
124 int bsp = getDB2Value();
126 if ((bsp >= 0) && (bsp <= 100)) {
127 return new PercentType(bsp);
131 return UnDefType.UNDEF;
134 protected State getAngleData() {
136 boolean paf = getBit(db0, 1);
141 boolean as = getBit(db1, 7);
142 int an = (db1 & 0x7F) * 2;
144 if ((an >= 0) && (an <= 180)) {
145 return new QuantityType<>(as ? an * -1 : an, Units.DEGREE_ANGLE);
149 return UnDefType.UNDEF;
153 public State convertToStateImpl(String channelId, String channelTypeId,
154 Function<String, @Nullable State> getCurrentStateFunc, Configuration config) {
156 case CHANNEL_ROLLERSHUTTER:
157 return getPositionData();
159 return getAngleData();
162 return UnDefType.UNDEF;