2 * Copyright (c) 2010-2021 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.OpenClosedType;
23 import org.openhab.core.library.types.StopMoveType;
24 import org.openhab.core.library.types.UpDownType;
25 import org.openhab.core.types.State;
26 import org.openhab.core.types.Type;
29 * RFXCOM data class for curtain1 message. See Harrison.
31 * @author Evert van Es - Initial contribution
32 * @author Pauli Anttila - Migrated to OH2
34 public class RFXComCurtain1Message extends RFXComBatteryDeviceMessage<RFXComCurtain1Message.SubType> {
36 public enum SubType implements ByteEnumWrapper {
39 private final int subType;
41 SubType(int subType) {
42 this.subType = subType;
46 public byte toByte() {
47 return (byte) subType;
51 public enum Commands implements ByteEnumWrapper {
57 private final int command;
59 Commands(int command) {
60 this.command = command;
64 public byte toByte() {
65 return (byte) command;
69 public SubType subType;
72 public Commands command;
74 public RFXComCurtain1Message() {
75 super(PacketType.CURTAIN1);
78 public RFXComCurtain1Message(byte[] data) throws RFXComException {
83 public String toString() {
86 str += super.toString();
87 str += ", Sub type = " + subType;
88 str += ", Device Id = " + getDeviceId();
89 str += ", Command = " + command;
90 str += ", Signal level = " + signalLevel;
91 str += ", Battery level = " + batteryLevel;
97 public void encodeMessage(byte[] data) throws RFXComException {
98 super.encodeMessage(data);
100 subType = fromByte(SubType.class, super.subType);
101 sensorId = (char) data[4];
103 command = fromByte(Commands.class, data[6]);
105 signalLevel = (byte) ((data[7] & 0xF0) >> 4);
106 batteryLevel = (byte) ((data[7] & 0x0F));
110 public byte[] decodeMessage() {
111 // Example data 07 18 00 00 65 01 00 00
112 // 07 18 00 00 65 02 00 00
114 byte[] data = new byte[8];
118 data[2] = subType.toByte();
120 data[4] = (byte) sensorId;
122 data[6] = command.toByte();
123 data[7] = (byte) (((signalLevel & 0x0F) << 4) + batteryLevel);
129 public String getDeviceId() {
130 return sensorId + ID_DELIMITER + unitCode;
134 public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
135 if (channelId.equals(CHANNEL_COMMAND)) {
136 return (command == Commands.CLOSE ? OpenClosedType.CLOSED : OpenClosedType.OPEN);
138 return super.convertToState(channelId, deviceState);
143 public void setSubType(SubType subType) {
144 this.subType = subType;
148 public void setDeviceId(String deviceId) throws RFXComException {
149 String[] ids = deviceId.split("\\" + ID_DELIMITER);
150 if (ids.length != 2) {
151 throw new RFXComException("Invalid device id '" + deviceId + "'");
154 sensorId = ids[0].charAt(0);
155 unitCode = Byte.parseByte(ids[1]);
159 public void convertFromState(String channelId, Type type) throws RFXComUnsupportedChannelException {
160 if (channelId.equals(CHANNEL_SHUTTER)) {
161 if (type instanceof OpenClosedType) {
162 command = (type == OpenClosedType.CLOSED ? Commands.CLOSE : Commands.OPEN);
163 } else if (type instanceof UpDownType) {
164 command = (type == UpDownType.UP ? Commands.CLOSE : Commands.OPEN);
165 } else if (type instanceof StopMoveType) {
166 command = Commands.STOP;
168 throw new RFXComUnsupportedChannelException("Channel " + channelId + " does not accept " + type);
171 throw new RFXComUnsupportedChannelException("Channel " + channelId + " is not relevant here");
176 public SubType convertSubType(String subType) throws RFXComUnsupportedValueException {
177 return ByteEnumUtil.convertSubType(SubType.class, subType);