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;
17 import static org.openhab.binding.rfxcom.internal.messages.RFXComLighting5Message.SubType.*;
19 import java.math.BigDecimal;
20 import java.util.Arrays;
21 import java.util.List;
23 import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
24 import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedChannelException;
25 import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedValueException;
26 import org.openhab.binding.rfxcom.internal.handler.DeviceState;
27 import org.openhab.core.library.types.DecimalType;
28 import org.openhab.core.library.types.IncreaseDecreaseType;
29 import org.openhab.core.library.types.OnOffType;
30 import org.openhab.core.library.types.OpenClosedType;
31 import org.openhab.core.library.types.PercentType;
32 import org.openhab.core.library.types.StringType;
33 import org.openhab.core.types.State;
34 import org.openhab.core.types.Type;
35 import org.openhab.core.types.UnDefType;
38 * RFXCOM data class for lighting5 message.
40 * @author Paul Hampson, Neil Renaud - Initial contribution
41 * @author Pauli Anttila
42 * @author Martin van Wingerden - added support for IT and some other subtypes
44 public class RFXComLighting5Message extends RFXComDeviceMessageImpl<RFXComLighting5Message.SubType> {
46 public enum SubType implements ByteEnumWrapper {
64 private final int subType;
66 SubType(int subType) {
67 this.subType = subType;
71 public byte toByte() {
72 return (byte) subType;
77 * Note: for the lighting5 commands, some command are only supported for certain sub types and
78 * command-bytes might even have a different meaning for another sub type.
80 * If no sub types are specified for a command, its supported by all sub types.
81 * An example is the command OFF which is represented by the byte 0x00 for all subtypes.
83 * Otherwise the list of sub types after the command-bytes indicates the sub types
84 * which support this command with this byte.
85 * Example byte value 0x03 means GROUP_ON for IT and some others while it means MOOD1 for LIGHTWAVERF
87 public enum Commands implements ByteEnumWrapperWithSupportedSubTypes<SubType> {
90 GROUP_OFF(0x02, LIGHTWAVERF, BBSB_NEW, CONRAD_RSL2, EURODOMEST, AVANTEK, IT, KANGTAI),
92 GROUP_ON(0x03, BBSB_NEW, CONRAD_RSL2, EURODOMEST, AVANTEK, IT, KANGTAI),
93 MOOD1(0x03, LIGHTWAVERF),
94 MOOD2(0x04, LIGHTWAVERF),
95 MOOD3(0x05, LIGHTWAVERF),
96 MOOD4(0x06, LIGHTWAVERF),
97 MOOD5(0x07, LIGHTWAVERF),
98 RESERVED1(0x08, LIGHTWAVERF),
99 RESERVED2(0x09, LIGHTWAVERF),
100 UNLOCK(0x0A, LIGHTWAVERF),
101 LOCK(0x0B, LIGHTWAVERF),
102 ALL_LOCK(0x0C, LIGHTWAVERF),
103 CLOSE_RELAY(0x0D, LIGHTWAVERF),
104 STOP_RELAY(0x0E, LIGHTWAVERF),
105 OPEN_RELAY(0x0F, LIGHTWAVERF),
106 SET_LEVEL(0x10, LIGHTWAVERF, IT),
107 COLOUR_PALETTE(0x11, LIGHTWAVERF),
108 COLOUR_TONE(0x12, LIGHTWAVERF),
109 COLOUR_CYCLE(0x13, LIGHTWAVERF),
110 TOGGLE_1(0x01, LIVOLO_APPLIANCE),
111 TOGGLE_2(0x02, LIVOLO_APPLIANCE),
112 TOGGLE_3(0x03, LIVOLO_APPLIANCE),
113 TOGGLE_4(0x04, LIVOLO_APPLIANCE),
114 TOGGLE_5(0x07, LIVOLO_APPLIANCE),
115 TOGGLE_6(0x0B, LIVOLO_APPLIANCE),
116 TOGGLE_7(0x06, LIVOLO_APPLIANCE),
117 TOGGLE_8(0x0A, LIVOLO_APPLIANCE),
118 TOGGLE_9(0x05, LIVOLO_APPLIANCE);
120 private final int command;
121 private final List<SubType> supportedBySubTypes;
123 Commands(int command) {
124 this(command, SubType.values());
127 Commands(int command, SubType... supportedBySubTypes) {
128 this.command = command;
129 this.supportedBySubTypes = Arrays.asList(supportedBySubTypes);
133 public List<SubType> supportedBySubTypes() {
134 return supportedBySubTypes;
138 public byte toByte() {
139 return (byte) command;
143 public SubType subType;
145 public byte unitCode;
146 public Commands command;
147 public byte dimmingLevel;
149 public RFXComLighting5Message() {
150 super(PacketType.LIGHTING5);
153 public RFXComLighting5Message(byte[] data) throws RFXComException {
158 public String toString() {
161 str += super.toString();
162 str += ", Sub type = " + subType;
163 str += ", Device Id = " + getDeviceId();
164 str += ", Command = " + command;
165 str += ", Dim level = " + dimmingLevel;
166 str += ", Signal level = " + signalLevel;
172 public void encodeMessage(byte[] data) throws RFXComException {
173 super.encodeMessage(data);
175 subType = fromByte(SubType.class, super.subType);
177 sensorId = (data[4] & 0xFF) << 16 | (data[5] & 0xFF) << 8 | (data[6] & 0xFF);
180 command = fromByte(Commands.class, data[8], subType);
182 dimmingLevel = data[9];
183 signalLevel = (byte) ((data[10] & 0xF0) >> 4);
187 public byte[] decodeMessage() {
188 byte[] data = new byte[11];
191 data[1] = RFXComBaseMessage.PacketType.LIGHTING5.toByte();
192 data[2] = subType.toByte();
194 data[4] = (byte) ((sensorId >> 16) & 0xFF);
195 data[5] = (byte) ((sensorId >> 8) & 0xFF);
196 data[6] = (byte) (sensorId & 0xFF);
199 data[8] = command.toByte();
200 data[9] = dimmingLevel;
201 data[10] = (byte) ((signalLevel & 0x0F) << 4);
207 public String getDeviceId() {
208 return sensorId + ID_DELIMITER + unitCode;
212 * Convert a 0-31 scale value to a percent type.
214 * @param pt percent type to convert
215 * @return converted value 0-31
217 public static int getDimLevelFromPercentType(PercentType pt) {
218 return pt.toBigDecimal().multiply(BigDecimal.valueOf(31))
219 .divide(PercentType.HUNDRED.toBigDecimal(), 0, BigDecimal.ROUND_UP).intValue();
223 * Convert a 0-31 scale value to a percent type.
225 * @param value percent type to convert
226 * @return converted value 0-31
228 public static PercentType getPercentTypeFromDimLevel(int value) {
229 value = Math.min(value, 31);
231 return new PercentType(BigDecimal.valueOf(value).multiply(BigDecimal.valueOf(100))
232 .divide(BigDecimal.valueOf(31), 0, BigDecimal.ROUND_UP).intValue());
236 public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
241 return new DecimalType(0);
243 return new DecimalType(1);
245 return new DecimalType(2);
247 return new DecimalType(3);
249 return new DecimalType(4);
251 return new DecimalType(5);
253 throw new RFXComUnsupportedChannelException(
254 "Unexpected mood command: " + command + " for " + channelId);
257 case CHANNEL_DIMMING_LEVEL:
258 return RFXComLighting5Message.getPercentTypeFromDimLevel(dimmingLevel);
260 case CHANNEL_COMMAND:
264 return OnOffType.OFF;
272 throw new RFXComUnsupportedChannelException("Can't convert " + command + " for " + channelId);
275 case CHANNEL_COMMAND_STRING:
276 return command == null ? UnDefType.UNDEF : StringType.valueOf(command.toString());
278 case CHANNEL_CONTACT:
282 return OpenClosedType.CLOSED;
286 return OpenClosedType.OPEN;
290 throw new RFXComUnsupportedChannelException("Can't convert " + command + " for " + channelId);
294 return super.convertToState(channelId, deviceState);
299 public void setSubType(SubType subType) {
300 this.subType = subType;
304 public void setDeviceId(String deviceId) throws RFXComException {
305 String[] ids = deviceId.split("\\" + ID_DELIMITER);
306 if (ids.length != 2) {
307 throw new RFXComException("Invalid device id '" + deviceId + "'");
310 sensorId = Integer.parseInt(ids[0]);
311 unitCode = Byte.parseByte(ids[1]);
315 public void convertFromState(String channelId, Type type) throws RFXComUnsupportedChannelException {
317 case CHANNEL_COMMAND:
318 if (type instanceof OnOffType) {
319 command = (type == OnOffType.ON ? Commands.ON : Commands.OFF);
323 throw new RFXComUnsupportedChannelException("Channel " + channelId + " does not accept " + type);
327 case CHANNEL_COMMAND_STRING:
328 command = Commands.valueOf(type.toString().toUpperCase());
331 case CHANNEL_DIMMING_LEVEL:
332 if (type instanceof OnOffType) {
333 command = (type == OnOffType.ON ? Commands.ON : Commands.OFF);
336 } else if (type instanceof PercentType) {
337 command = Commands.SET_LEVEL;
338 dimmingLevel = (byte) getDimLevelFromPercentType((PercentType) type);
340 if (dimmingLevel == 0) {
341 command = Commands.OFF;
344 } else if (type instanceof IncreaseDecreaseType) {
345 command = Commands.SET_LEVEL;
346 // Evert: I do not know how to get previous object state...
350 throw new RFXComUnsupportedChannelException("Channel " + channelId + " does not accept " + type);
355 throw new RFXComUnsupportedChannelException("Channel " + channelId + " is not relevant here");
360 public SubType convertSubType(String subType) throws RFXComUnsupportedValueException {
361 return ByteEnumUtil.convertSubType(SubType.class, subType);