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.RFXComBaseMessage.PacketType.LIGHTING2;
18 import static org.openhab.binding.rfxcom.internal.messages.RFXComLighting2Message.Commands.*;
20 import java.math.BigDecimal;
22 import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
23 import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedChannelException;
24 import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedValueException;
25 import org.openhab.binding.rfxcom.internal.handler.DeviceState;
26 import org.openhab.core.library.types.IncreaseDecreaseType;
27 import org.openhab.core.library.types.OnOffType;
28 import org.openhab.core.library.types.OpenClosedType;
29 import org.openhab.core.library.types.PercentType;
30 import org.openhab.core.types.State;
31 import org.openhab.core.types.Type;
34 * RFXCOM data class for lighting2 message.
36 * @author Pauli Anttila - Initial contribution
38 public class RFXComLighting2Message extends RFXComDeviceMessageImpl<RFXComLighting2Message.SubType> {
39 public enum SubType implements ByteEnumWrapper {
45 private final int subType;
47 SubType(int subType) {
48 this.subType = subType;
52 public byte toByte() {
53 return (byte) subType;
57 public enum Commands implements ByteEnumWrapper {
65 private final int command;
67 Commands(int command) {
68 this.command = command;
72 public byte toByte() {
73 return (byte) command;
77 public SubType subType;
80 public Commands command;
81 public byte dimmingLevel;
84 public RFXComLighting2Message() {
85 super(PacketType.LIGHTING2);
88 public RFXComLighting2Message(byte[] data) throws RFXComException {
93 public String toString() {
96 str += super.toString();
97 str += ", Sub type = " + subType;
98 str += ", Device Id = " + getDeviceId();
99 str += ", Command = " + command;
100 str += ", Dim level = " + dimmingLevel;
101 str += ", Signal level = " + signalLevel;
107 public void encodeMessage(byte[] data) throws RFXComException {
108 super.encodeMessage(data);
110 subType = fromByte(SubType.class, super.subType);
111 sensorId = (data[4] & 0xFF) << 24 | (data[5] & 0xFF) << 16 | (data[6] & 0xFF) << 8 | (data[7] & 0xFF);
112 command = fromByte(Commands.class, data[9]);
114 if ((command == Commands.GROUP_ON) || (command == Commands.GROUP_OFF)) {
120 dimmingLevel = data[10];
121 signalLevel = (byte) ((data[11] & 0xF0) >> 4);
125 public byte[] decodeMessage() {
126 byte[] data = new byte[12];
129 data[1] = LIGHTING2.toByte();
130 data[2] = subType.toByte();
132 data[4] = (byte) ((sensorId >> 24) & 0xFF);
133 data[5] = (byte) ((sensorId >> 16) & 0xFF);
134 data[6] = (byte) ((sensorId >> 8) & 0xFF);
135 data[7] = (byte) (sensorId & 0xFF);
138 data[9] = command.toByte();
139 data[10] = dimmingLevel;
140 data[11] = (byte) ((signalLevel & 0x0F) << 4);
146 public String getDeviceId() {
147 return sensorId + ID_DELIMITER + unitCode;
151 * Convert a 0-15 scale value to a percent type.
153 * @param pt percent type to convert
154 * @return converted value 0-15
156 public static int getDimLevelFromPercentType(PercentType pt) {
157 return pt.toBigDecimal().multiply(BigDecimal.valueOf(15))
158 .divide(PercentType.HUNDRED.toBigDecimal(), 0, BigDecimal.ROUND_UP).intValue();
162 * Convert a 0-15 scale value to a percent type.
164 * @param value percent type to convert
165 * @return converted value 0-15
167 public static PercentType getPercentTypeFromDimLevel(int value) {
168 value = Math.min(value, 15);
170 return new PercentType(BigDecimal.valueOf(value).multiply(BigDecimal.valueOf(100))
171 .divide(BigDecimal.valueOf(15), 0, BigDecimal.ROUND_UP).intValue());
175 public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
177 case CHANNEL_DIMMING_LEVEL:
178 return RFXComLighting2Message.getPercentTypeFromDimLevel(dimmingLevel);
180 case CHANNEL_COMMAND:
184 return OnOffType.OFF;
190 case SET_GROUP_LEVEL:
193 throw new RFXComUnsupportedChannelException("Can't convert " + command + " for " + channelId);
196 case CHANNEL_CONTACT:
200 return OpenClosedType.CLOSED;
204 return OpenClosedType.OPEN;
206 case SET_GROUP_LEVEL:
209 throw new RFXComUnsupportedChannelException("Can't convert " + command + " for " + channelId);
213 return super.convertToState(channelId, deviceState);
218 public void setSubType(SubType subType) {
219 this.subType = subType;
223 public void setDeviceId(String deviceId) throws RFXComException {
224 String[] ids = deviceId.split("\\" + ID_DELIMITER);
225 if (ids.length != 2) {
226 throw new RFXComException("Invalid device id '" + deviceId + "'");
229 sensorId = Integer.parseInt(ids[0]);
231 // Get unitcode, 0 means group
232 unitCode = Byte.parseByte(ids[1]);
240 public void convertFromState(String channelId, Type type) throws RFXComUnsupportedChannelException {
242 case CHANNEL_COMMAND:
243 if (type instanceof OnOffType) {
245 command = (type == OnOffType.ON ? GROUP_ON : GROUP_OFF);
248 command = (type == OnOffType.ON ? Commands.ON : Commands.OFF);
253 throw new RFXComUnsupportedChannelException("Channel " + channelId + " does not accept " + type);
257 case CHANNEL_DIMMING_LEVEL:
258 if (type instanceof OnOffType) {
259 command = (type == OnOffType.ON ? Commands.ON : Commands.OFF);
262 } else if (type instanceof PercentType) {
263 command = Commands.SET_LEVEL;
264 dimmingLevel = (byte) getDimLevelFromPercentType((PercentType) type);
266 if (dimmingLevel == 0) {
267 command = Commands.OFF;
269 } else if (type instanceof IncreaseDecreaseType) {
270 command = Commands.SET_LEVEL;
271 // Evert: I do not know how to get previous object state...
275 throw new RFXComUnsupportedChannelException("Channel " + channelId + " does not accept " + type);
280 throw new RFXComUnsupportedChannelException("Channel " + channelId + " is not relevant here");
285 public SubType convertSubType(String subType) throws RFXComUnsupportedValueException {
286 return ByteEnumUtil.convertSubType(SubType.class, subType);