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.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 curtain1 message. See Harrison.
33 * @author Evert van Es - Initial contribution
34 * @author Pauli Anttila - Migrated to OH2
36 public class RFXComCurtain1Message extends RFXComBatteryDeviceMessage<RFXComCurtain1Message.SubType> {
38 public enum SubType implements ByteEnumWrapper {
41 private final int subType;
43 SubType(int subType) {
44 this.subType = subType;
48 public byte toByte() {
49 return (byte) subType;
53 public enum Commands implements ByteEnumWrapper {
59 private final int command;
61 Commands(int command) {
62 this.command = command;
66 public byte toByte() {
67 return (byte) command;
71 public SubType subType;
74 public Commands command;
76 public RFXComCurtain1Message() {
77 super(PacketType.CURTAIN1);
80 public RFXComCurtain1Message(byte[] data) throws RFXComException {
85 public String toString() {
88 str += super.toString();
89 str += ", Sub type = " + subType;
90 str += ", Device Id = " + getDeviceId();
91 str += ", Command = " + command;
92 str += ", Signal level = " + signalLevel;
93 str += ", Battery level = " + batteryLevel;
99 public void encodeMessage(byte[] data) throws RFXComException {
100 super.encodeMessage(data);
102 subType = fromByte(SubType.class, super.subType);
103 sensorId = (char) data[4];
105 command = fromByte(Commands.class, data[6]);
107 signalLevel = (byte) ((data[7] & 0xF0) >> 4);
108 batteryLevel = (byte) ((data[7] & 0x0F));
112 public byte[] decodeMessage() {
113 // Example data 07 18 00 00 65 01 00 00
114 // 07 18 00 00 65 02 00 00
116 byte[] data = new byte[8];
120 data[2] = subType.toByte();
122 data[4] = (byte) sensorId;
124 data[6] = command.toByte();
125 data[7] = (byte) (((signalLevel & 0x0F) << 4) + batteryLevel);
131 public String getDeviceId() {
132 return sensorId + ID_DELIMITER + unitCode;
136 public State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
137 throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
138 if (channelId.equals(CHANNEL_COMMAND)) {
139 return (command == Commands.CLOSE ? OpenClosedType.CLOSED : OpenClosedType.OPEN);
141 return super.convertToState(channelId, config, deviceState);
146 public void setSubType(SubType subType) {
147 this.subType = subType;
151 public void setDeviceId(String deviceId) throws RFXComException {
152 String[] ids = deviceId.split("\\" + ID_DELIMITER);
153 if (ids.length != 2) {
154 throw new RFXComException("Invalid device id '" + deviceId + "'");
157 sensorId = ids[0].charAt(0);
158 unitCode = Byte.parseByte(ids[1]);
162 public void convertFromState(String channelId, Type type) throws RFXComUnsupportedChannelException {
163 if (channelId.equals(CHANNEL_SHUTTER)) {
164 if (type instanceof OpenClosedType) {
165 command = (type == OpenClosedType.CLOSED ? Commands.CLOSE : Commands.OPEN);
166 } else if (type instanceof UpDownType) {
167 command = (type == UpDownType.UP ? Commands.CLOSE : Commands.OPEN);
168 } else if (type instanceof StopMoveType) {
169 command = Commands.STOP;
171 throw new RFXComUnsupportedChannelException("Channel " + channelId + " does not accept " + type);
174 throw new RFXComUnsupportedChannelException("Channel " + channelId + " is not relevant here");
179 public SubType convertSubType(String subType) throws RFXComUnsupportedValueException {
180 return ByteEnumUtil.convertSubType(SubType.class, subType);