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.CHANNEL_COMMAND;
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.OnOffType;
23 import org.openhab.core.types.State;
24 import org.openhab.core.types.Type;
27 * RFXCOM data class for HomeConfort message.
29 * @author Mike Jagdis - Initial contribution
31 public class RFXComHomeConfortMessage extends RFXComDeviceMessageImpl<RFXComHomeConfortMessage.SubType> {
33 public enum SubType implements ByteEnumWrapper {
36 private final int subType;
38 SubType(int subType) {
39 this.subType = subType;
43 public byte toByte() {
44 return (byte) subType;
48 public enum Commands implements ByteEnumWrapper {
54 private final int command;
56 Commands(int command) {
57 this.command = command;
61 public byte toByte() {
62 return (byte) command;
66 public SubType subType;
68 public char houseCode;
70 public Commands command;
72 public RFXComHomeConfortMessage() {
73 super(PacketType.HOME_CONFORT);
76 public RFXComHomeConfortMessage(byte[] data) throws RFXComException {
81 public String toString() {
82 return super.toString() + ", Sub type = " + subType + ", Device Id = " + getDeviceId() + ", Command = "
83 + command + ", Signal level = " + signalLevel;
87 public void encodeMessage(byte[] data) throws RFXComException {
88 super.encodeMessage(data);
90 subType = fromByte(SubType.class, super.subType);
91 deviceId = (((data[4] << 8) | data[5]) << 8) | data[6];
92 houseCode = (char) data[7];
94 command = fromByte(Commands.class, data[9]);
95 if (command == Commands.GROUP_ON || command == Commands.GROUP_OFF) {
98 signalLevel = (byte) ((data[12] & 0xF0) >> 4);
102 public byte[] decodeMessage() {
103 byte[] data = new byte[13];
106 data[1] = PacketType.HOME_CONFORT.toByte();
107 data[2] = subType.toByte();
109 data[4] = (byte) ((deviceId >> 16) & 0xff);
110 data[5] = (byte) ((deviceId >> 8) & 0xff);
111 data[6] = (byte) (deviceId & 0xff);
112 data[7] = (byte) houseCode;
114 data[9] = command.toByte();
117 data[12] = (byte) ((signalLevel & 0x0F) << 4);
123 public String getDeviceId() {
124 return deviceId + ID_DELIMITER + houseCode + ID_DELIMITER + unitCode;
128 public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
129 if (channelId.equals(CHANNEL_COMMAND)) {
130 return (command == Commands.OFF || command == Commands.GROUP_OFF ? OnOffType.OFF : OnOffType.ON);
132 return super.convertToState(channelId, deviceState);
137 public void setSubType(SubType subType) {
138 this.subType = subType;
142 public void setDeviceId(String deviceId) throws RFXComException {
143 String[] ids = deviceId.split("\\" + ID_DELIMITER);
144 if (ids.length != 3) {
145 throw new RFXComException("Invalid device id '" + deviceId + "'");
148 this.deviceId = Integer.parseInt(ids[0]);
149 houseCode = ids[1].charAt(0);
150 unitCode = Byte.parseByte(ids[2]);
154 public void convertFromState(String channelId, Type type) throws RFXComUnsupportedChannelException {
155 if (CHANNEL_COMMAND.equals(channelId)) {
156 if (type instanceof OnOffType) {
158 command = (type == OnOffType.ON ? Commands.GROUP_ON : Commands.GROUP_OFF);
161 command = (type == OnOffType.ON ? Commands.ON : Commands.OFF);
164 throw new RFXComUnsupportedChannelException("Channel " + channelId + " does not accept " + type);
168 throw new RFXComUnsupportedChannelException("Channel " + channelId + " is not relevant here");
173 public SubType convertSubType(String subType) throws RFXComUnsupportedValueException {
174 return ByteEnumUtil.convertSubType(SubType.class, subType);