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.OnOffType;
23 import org.openhab.core.library.types.OpenClosedType;
24 import org.openhab.core.types.State;
25 import org.openhab.core.types.Type;
28 * RFXCOM data class for lighting6 message. See Blyss.
30 * @author Damien Servant - Initial contribution
31 * @author Pauli Anttila - Migrated for OH2
33 public class RFXComLighting6Message extends RFXComDeviceMessageImpl<RFXComLighting6Message.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 groupCode;
72 public Commands command;
74 public RFXComLighting6Message() {
75 super(PacketType.LIGHTING6);
78 public RFXComLighting6Message(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;
96 public void encodeMessage(byte[] data) throws RFXComException {
97 super.encodeMessage(data);
99 subType = fromByte(SubType.class, super.subType);
100 sensorId = (data[4] & 0xFF) << 8 | (data[5] & 0xFF);
101 groupCode = (char) data[6];
103 command = fromByte(Commands.class, data[8]);
105 signalLevel = (byte) ((data[11] & 0xF0) >> 4);
109 public byte[] decodeMessage() {
110 // Example data 0B 15 00 02 01 01 41 01 00 04 8E 00
111 // 0B 15 00 02 01 01 41 01 01 04 8E 00
113 byte[] data = new byte[12];
116 data[1] = RFXComBaseMessage.PacketType.LIGHTING6.toByte();
117 data[2] = subType.toByte();
119 data[4] = (byte) ((sensorId >> 8) & 0xFF);
120 data[5] = (byte) (sensorId & 0xFF);
121 data[6] = (byte) groupCode;
123 data[8] = command.toByte();
124 data[9] = 0x00; // CmdSeqNbr1 - 0 to 4 - Useless for a Blyss Switch
125 data[10] = 0x00; // CmdSeqNbr2 - 0 to 145 - Useless for a Blyss Switch
126 data[11] = (byte) ((signalLevel & 0x0F) << 4);
132 public String getDeviceId() {
133 return sensorId + ID_DELIMITER + groupCode + ID_DELIMITER + unitCode;
137 public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
139 case CHANNEL_COMMAND:
143 return OnOffType.OFF;
150 throw new RFXComUnsupportedChannelException("Can't convert " + command + " for " + channelId);
153 case CHANNEL_CONTACT:
157 return OpenClosedType.CLOSED;
161 return OpenClosedType.OPEN;
164 throw new RFXComUnsupportedChannelException("Can't convert " + command + " for " + channelId);
168 return super.convertToState(channelId, deviceState);
173 public void setSubType(SubType subType) {
174 this.subType = subType;
178 public void setDeviceId(String deviceId) throws RFXComException {
179 String[] ids = deviceId.split("\\" + ID_DELIMITER);
180 if (ids.length != 3) {
181 throw new RFXComException("Invalid device id '" + deviceId + "'");
184 sensorId = Integer.parseInt(ids[0]);
185 groupCode = ids[1].charAt(0);
186 unitCode = Byte.parseByte(ids[2]);
190 public void convertFromState(String channelId, Type type) throws RFXComUnsupportedChannelException {
192 case CHANNEL_COMMAND:
193 if (type instanceof OnOffType) {
194 command = (type == OnOffType.ON ? Commands.ON : Commands.OFF);
197 throw new RFXComUnsupportedChannelException("Channel " + channelId + " does not accept " + type);
202 throw new RFXComUnsupportedChannelException("Channel " + channelId + " is not relevant here");
207 public SubType convertSubType(String subType) throws RFXComUnsupportedValueException {
208 return ByteEnumUtil.convertSubType(SubType.class, subType);