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.OnOffType;
25 import org.openhab.core.library.types.OpenClosedType;
26 import org.openhab.core.types.State;
27 import org.openhab.core.types.Type;
30 * RFXCOM data class for lighting6 message. See Blyss.
32 * @author Damien Servant - Initial contribution
33 * @author Pauli Anttila - Migrated for OH2
35 public class RFXComLighting6Message extends RFXComDeviceMessageImpl<RFXComLighting6Message.SubType> {
37 public enum SubType implements ByteEnumWrapper {
40 private final int subType;
42 SubType(int subType) {
43 this.subType = subType;
47 public byte toByte() {
48 return (byte) subType;
52 public enum Commands implements ByteEnumWrapper {
58 private final int command;
60 Commands(int command) {
61 this.command = command;
65 public byte toByte() {
66 return (byte) command;
70 public SubType subType;
72 public char groupCode;
74 public Commands command;
76 public RFXComLighting6Message() {
77 super(PacketType.LIGHTING6);
80 public RFXComLighting6Message(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;
98 public void encodeMessage(byte[] data) throws RFXComException {
99 super.encodeMessage(data);
101 subType = fromByte(SubType.class, super.subType);
102 sensorId = (data[4] & 0xFF) << 8 | (data[5] & 0xFF);
103 groupCode = (char) data[6];
105 command = fromByte(Commands.class, data[8]);
107 signalLevel = (byte) ((data[11] & 0xF0) >> 4);
111 public byte[] decodeMessage() {
112 // Example data 0B 15 00 02 01 01 41 01 00 04 8E 00
113 // 0B 15 00 02 01 01 41 01 01 04 8E 00
115 byte[] data = new byte[12];
118 data[1] = RFXComBaseMessage.PacketType.LIGHTING6.toByte();
119 data[2] = subType.toByte();
121 data[4] = (byte) ((sensorId >> 8) & 0xFF);
122 data[5] = (byte) (sensorId & 0xFF);
123 data[6] = (byte) groupCode;
125 data[8] = command.toByte();
126 data[9] = 0x00; // CmdSeqNbr1 - 0 to 4 - Useless for a Blyss Switch
127 data[10] = 0x00; // CmdSeqNbr2 - 0 to 145 - Useless for a Blyss Switch
128 data[11] = (byte) ((signalLevel & 0x0F) << 4);
134 public String getDeviceId() {
135 return sensorId + ID_DELIMITER + groupCode + ID_DELIMITER + unitCode;
139 public State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
140 throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
142 case CHANNEL_COMMAND:
146 return OnOffType.OFF;
153 throw new RFXComUnsupportedChannelException("Can't convert " + command + " for " + channelId);
156 case CHANNEL_CONTACT:
160 return OpenClosedType.CLOSED;
164 return OpenClosedType.OPEN;
167 throw new RFXComUnsupportedChannelException("Can't convert " + command + " for " + channelId);
171 return super.convertToState(channelId, config, deviceState);
176 public void setSubType(SubType subType) {
177 this.subType = subType;
181 public void setDeviceId(String deviceId) throws RFXComException {
182 String[] ids = deviceId.split("\\" + ID_DELIMITER);
183 if (ids.length != 3) {
184 throw new RFXComException("Invalid device id '" + deviceId + "'");
187 sensorId = Integer.parseInt(ids[0]);
188 groupCode = ids[1].charAt(0);
189 unitCode = Byte.parseByte(ids[2]);
193 public void convertFromState(String channelId, Type type) throws RFXComUnsupportedChannelException {
195 case CHANNEL_COMMAND:
196 if (type instanceof OnOffType) {
197 command = (type == OnOffType.ON ? Commands.ON : Commands.OFF);
200 throw new RFXComUnsupportedChannelException("Channel " + channelId + " does not accept " + type);
205 throw new RFXComUnsupportedChannelException("Channel " + channelId + " is not relevant here");
210 public SubType convertSubType(String subType) throws RFXComUnsupportedValueException {
211 return ByteEnumUtil.convertSubType(SubType.class, subType);