2 * Copyright (c) 2010-2020 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.exceptions.RFXComException;
19 import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedChannelException;
20 import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedValueException;
21 import org.openhab.binding.rfxcom.internal.handler.DeviceState;
22 import org.openhab.core.library.types.IncreaseDecreaseType;
23 import org.openhab.core.library.types.OnOffType;
24 import org.openhab.core.library.types.OpenClosedType;
25 import org.openhab.core.library.types.StopMoveType;
26 import org.openhab.core.library.types.UpDownType;
27 import org.openhab.core.types.State;
28 import org.openhab.core.types.Type;
31 * RFXCOM data class for RFY (Somfy RTS) message.
33 * @author Jürgen Richtsfeld - Initial contribution
34 * @author Pauli Anttila - Ported from OpenHAB1
35 * @author Mike Jagdis - Added venetian support and sun+wind detector
37 public class RFXComRfyMessage extends RFXComDeviceMessageImpl<RFXComRfyMessage.SubType> {
39 public enum Commands implements ByteEnumWrapper {
48 ENABLE_SUN_WIND_DETECTOR(0x13),
49 DISABLE_SUN_DETECTOR(0x14);
51 private final int command;
53 Commands(int command) {
54 this.command = command;
58 public byte toByte() {
59 return (byte) command;
63 public enum SubType implements ByteEnumWrapper {
69 private final int subType;
71 SubType(int subType) {
72 this.subType = subType;
76 public byte toByte() {
77 return (byte) subType;
81 public SubType subType;
84 public Commands command;
86 public RFXComRfyMessage() {
87 super(PacketType.RFY);
90 public RFXComRfyMessage(byte[] data) throws RFXComException {
95 public String toString() {
96 return super.toString() + ", Sub type = " + subType + ", Unit Id = " + getDeviceId() + ", Unit Code = "
97 + unitCode + ", Command = " + command + ", Signal level = " + signalLevel;
101 public void encodeMessage(byte[] data) throws RFXComException {
102 super.encodeMessage(data);
104 subType = fromByte(SubType.class, super.subType);
106 unitId = (data[4] & 0xFF) << 16 | (data[5] & 0xFF) << 8 | (data[6] & 0xFF);
109 command = fromByte(Commands.class, data[8]);
110 signalLevel = (byte) ((data[12] & 0xF0) >> 4);
114 public byte[] decodeMessage() {
115 final byte[] data = new byte[13];
118 data[1] = RFXComBaseMessage.PacketType.RFY.toByte();
119 data[2] = subType.toByte();
121 data[4] = (byte) ((unitId >> 16) & 0xFF);
122 data[5] = (byte) ((unitId >> 8) & 0xFF);
123 data[6] = (byte) (unitId & 0xFF);
125 data[8] = command.toByte();
126 data[12] = (byte) ((signalLevel & 0x0F) << 4);
132 public String getDeviceId() {
133 return unitId + ID_DELIMITER + unitCode;
137 public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
139 case CHANNEL_COMMAND:
140 return (command == Commands.DOWN ? OpenClosedType.CLOSED : OpenClosedType.OPEN);
143 return super.convertToState(channelId, deviceState);
148 public void setSubType(SubType subType) {
149 this.subType = subType;
153 public void setDeviceId(String deviceId) throws RFXComException {
154 String[] ids = deviceId.split("\\" + ID_DELIMITER);
155 if (ids.length != 2) {
156 throw new RFXComException("Invalid device id '" + deviceId + "'");
159 this.unitId = Integer.parseInt(ids[0]);
160 this.unitCode = Byte.parseByte(ids[1]);
164 public void convertFromState(String channelId, Type type) throws RFXComUnsupportedChannelException {
166 case CHANNEL_SHUTTER:
167 if (type instanceof OpenClosedType) {
168 this.command = (type == OpenClosedType.CLOSED ? Commands.DOWN : Commands.UP);
170 } else if (type instanceof UpDownType) {
171 this.command = (type == UpDownType.DOWN ? Commands.DOWN : Commands.UP);
173 } else if (type instanceof StopMoveType) {
174 this.command = Commands.STOP;
177 throw new RFXComUnsupportedChannelException("Channel " + channelId + " does not accept " + type);
181 case CHANNEL_PROGRAM:
182 if (type == OnOffType.ON) {
183 this.command = Commands.PROGRAM;
185 throw new RFXComUnsupportedChannelException("Can't convert " + type + " to Command");
189 case CHANNEL_SUN_WIND_DETECTOR:
190 if (type instanceof OnOffType) {
191 this.command = (type == OnOffType.ON ? Commands.ENABLE_SUN_WIND_DETECTOR
192 : Commands.DISABLE_SUN_DETECTOR);
194 throw new RFXComUnsupportedChannelException("Can't convert " + type + " to Command");
198 case CHANNEL_VENETIAN_BLIND:
199 if (type instanceof OpenClosedType) {
200 this.command = (type == OpenClosedType.CLOSED ? Commands.DOWN_SHORT : Commands.UP_SHORT);
202 } else if (type instanceof OnOffType) {
203 this.command = (type == OnOffType.ON ? Commands.DOWN_SHORT : Commands.UP_SHORT);
205 } else if (type instanceof IncreaseDecreaseType) {
206 this.command = (type == IncreaseDecreaseType.INCREASE ? Commands.DOWN_LONG : Commands.UP_LONG);
209 throw new RFXComUnsupportedChannelException("Can't convert " + type + " to Command");
214 throw new RFXComUnsupportedChannelException("Channel " + channelId + " is not relevant here");
219 public SubType convertSubType(String subType) throws RFXComUnsupportedValueException {
220 return ByteEnumUtil.convertSubType(SubType.class, subType);