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.CHANNEL_COMMAND;
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.OnOffType;
25 import org.openhab.core.types.State;
26 import org.openhab.core.types.Type;
29 * RFXCOM data class for HomeConfort message.
31 * @author Mike Jagdis - Initial contribution
33 public class RFXComHomeConfortMessage extends RFXComDeviceMessageImpl<RFXComHomeConfortMessage.SubType> {
35 public enum SubType implements ByteEnumWrapper {
38 private final int subType;
40 SubType(int subType) {
41 this.subType = subType;
45 public byte toByte() {
46 return (byte) subType;
50 public enum Commands implements ByteEnumWrapper {
56 private final int command;
58 Commands(int command) {
59 this.command = command;
63 public byte toByte() {
64 return (byte) command;
68 public SubType subType;
70 public char houseCode;
72 public Commands command;
74 public RFXComHomeConfortMessage() {
75 super(PacketType.HOME_CONFORT);
78 public RFXComHomeConfortMessage(byte[] data) throws RFXComException {
83 public String toString() {
84 return super.toString() + ", Sub type = " + subType + ", Device Id = " + getDeviceId() + ", Command = "
85 + command + ", Signal level = " + signalLevel;
89 public void encodeMessage(byte[] data) throws RFXComException {
90 super.encodeMessage(data);
92 subType = fromByte(SubType.class, super.subType);
93 deviceId = (((data[4] << 8) | data[5]) << 8) | data[6];
94 houseCode = (char) data[7];
96 command = fromByte(Commands.class, data[9]);
97 if (command == Commands.GROUP_ON || command == Commands.GROUP_OFF) {
100 signalLevel = (byte) ((data[12] & 0xF0) >> 4);
104 public byte[] decodeMessage() {
105 byte[] data = new byte[13];
108 data[1] = PacketType.HOME_CONFORT.toByte();
109 data[2] = subType.toByte();
111 data[4] = (byte) ((deviceId >> 16) & 0xff);
112 data[5] = (byte) ((deviceId >> 8) & 0xff);
113 data[6] = (byte) (deviceId & 0xff);
114 data[7] = (byte) houseCode;
116 data[9] = command.toByte();
119 data[12] = (byte) ((signalLevel & 0x0F) << 4);
125 public String getDeviceId() {
126 return deviceId + ID_DELIMITER + houseCode + ID_DELIMITER + unitCode;
130 public State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
131 throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
132 if (channelId.equals(CHANNEL_COMMAND)) {
133 return OnOffType.from(command != Commands.OFF && command != Commands.GROUP_OFF);
135 return super.convertToState(channelId, config, deviceState);
140 public void setSubType(SubType subType) {
141 this.subType = subType;
145 public void setDeviceId(String deviceId) throws RFXComException {
146 String[] ids = deviceId.split("\\" + ID_DELIMITER);
147 if (ids.length != 3) {
148 throw new RFXComException("Invalid device id '" + deviceId + "'");
151 this.deviceId = Integer.parseInt(ids[0]);
152 houseCode = ids[1].charAt(0);
153 unitCode = Byte.parseByte(ids[2]);
157 public void convertFromState(String channelId, Type type) throws RFXComUnsupportedChannelException {
158 if (CHANNEL_COMMAND.equals(channelId)) {
159 if (type instanceof OnOffType) {
161 command = (type == OnOffType.ON ? Commands.GROUP_ON : Commands.GROUP_OFF);
164 command = (type == OnOffType.ON ? Commands.ON : Commands.OFF);
167 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);