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.rfxcom.internal.messages;
15 import static org.openhab.binding.rfxcom.internal.RFXComBindingConstants.*;
16 import static org.openhab.binding.rfxcom.internal.messages.ByteEnumUtil.fromByte;
18 import org.openhab.binding.rfxcom.internal.config.RFXComDeviceConfiguration;
19 import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
20 import org.openhab.binding.rfxcom.internal.exceptions.RFXComInvalidStateException;
21 import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedChannelException;
22 import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedValueException;
23 import org.openhab.binding.rfxcom.internal.handler.DeviceState;
24 import org.openhab.core.library.types.IncreaseDecreaseType;
25 import org.openhab.core.library.types.OnOffType;
26 import org.openhab.core.library.types.OpenClosedType;
27 import org.openhab.core.library.types.StopMoveType;
28 import org.openhab.core.library.types.UpDownType;
29 import org.openhab.core.types.State;
30 import org.openhab.core.types.Type;
33 * RFXCOM data class for RFY (Somfy RTS) message.
35 * @author Jürgen Richtsfeld - Initial contribution
36 * @author Pauli Anttila - Ported from OpenHAB1
37 * @author Mike Jagdis - Added venetian support and sun+wind detector
39 public class RFXComRfyMessage extends RFXComDeviceMessageImpl<RFXComRfyMessage.SubType> {
41 public enum Commands implements ByteEnumWrapper {
50 ENABLE_SUN_WIND_DETECTOR(0x13),
51 DISABLE_SUN_DETECTOR(0x14);
53 private final int command;
55 Commands(int command) {
56 this.command = command;
60 public byte toByte() {
61 return (byte) command;
65 public enum SubType implements ByteEnumWrapper {
71 private final int subType;
73 SubType(int subType) {
74 this.subType = subType;
78 public byte toByte() {
79 return (byte) subType;
83 public SubType subType;
86 public Commands command;
88 public RFXComRfyMessage() {
89 super(PacketType.RFY);
92 public RFXComRfyMessage(byte[] data) throws RFXComException {
97 public String toString() {
98 return super.toString() + ", Sub type = " + subType + ", Unit Id = " + getDeviceId() + ", Unit Code = "
99 + unitCode + ", Command = " + command + ", Signal level = " + signalLevel;
103 public void encodeMessage(byte[] data) throws RFXComException {
104 super.encodeMessage(data);
106 subType = fromByte(SubType.class, super.subType);
108 unitId = (data[4] & 0xFF) << 16 | (data[5] & 0xFF) << 8 | (data[6] & 0xFF);
111 command = fromByte(Commands.class, data[8]);
112 signalLevel = (byte) ((data[12] & 0xF0) >> 4);
116 public byte[] decodeMessage() {
117 final byte[] data = new byte[13];
120 data[1] = RFXComBaseMessage.PacketType.RFY.toByte();
121 data[2] = subType.toByte();
123 data[4] = (byte) ((unitId >> 16) & 0xFF);
124 data[5] = (byte) ((unitId >> 8) & 0xFF);
125 data[6] = (byte) (unitId & 0xFF);
127 data[8] = command.toByte();
128 data[12] = (byte) ((signalLevel & 0x0F) << 4);
134 public String getDeviceId() {
135 return unitId + ID_DELIMITER + unitCode;
139 public State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
140 throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
142 case CHANNEL_COMMAND:
143 return (command == Commands.DOWN ? OpenClosedType.CLOSED : OpenClosedType.OPEN);
146 return super.convertToState(channelId, config, deviceState);
151 public void setSubType(SubType subType) {
152 this.subType = subType;
156 public void setDeviceId(String deviceId) throws RFXComException {
157 String[] ids = deviceId.split("\\" + ID_DELIMITER);
158 if (ids.length != 2) {
159 throw new RFXComException("Invalid device id '" + deviceId + "'");
162 this.unitId = Integer.parseInt(ids[0]);
163 this.unitCode = Byte.parseByte(ids[1]);
167 public void convertFromState(String channelId, Type type) throws RFXComUnsupportedChannelException {
169 case CHANNEL_SHUTTER:
170 if (type instanceof OpenClosedType) {
171 this.command = (type == OpenClosedType.CLOSED ? Commands.DOWN : Commands.UP);
173 } else if (type instanceof UpDownType) {
174 this.command = (type == UpDownType.DOWN ? Commands.DOWN : Commands.UP);
176 } else if (type instanceof StopMoveType) {
177 this.command = Commands.STOP;
180 throw new RFXComUnsupportedChannelException("Channel " + channelId + " does not accept " + type);
184 case CHANNEL_PROGRAM:
185 if (type == OnOffType.ON) {
186 this.command = Commands.PROGRAM;
188 throw new RFXComUnsupportedChannelException("Can't convert " + type + " to Command");
192 case CHANNEL_SUN_WIND_DETECTOR:
193 if (type instanceof OnOffType) {
194 this.command = (type == OnOffType.ON ? Commands.ENABLE_SUN_WIND_DETECTOR
195 : Commands.DISABLE_SUN_DETECTOR);
197 throw new RFXComUnsupportedChannelException("Can't convert " + type + " to Command");
201 case CHANNEL_VENETIAN_BLIND:
202 if (type instanceof OpenClosedType) {
203 this.command = (type == OpenClosedType.CLOSED ? Commands.DOWN_SHORT : Commands.UP_SHORT);
205 } else if (type instanceof OnOffType) {
206 this.command = (type == OnOffType.ON ? Commands.DOWN_SHORT : Commands.UP_SHORT);
208 } else if (type instanceof IncreaseDecreaseType) {
209 this.command = (type == IncreaseDecreaseType.INCREASE ? Commands.DOWN_LONG : Commands.UP_LONG);
212 throw new RFXComUnsupportedChannelException("Can't convert " + type + " to Command");
217 throw new RFXComUnsupportedChannelException("Channel " + channelId + " is not relevant here");
222 public SubType convertSubType(String subType) throws RFXComUnsupportedValueException {
223 return ByteEnumUtil.convertSubType(SubType.class, subType);