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;
21 import java.math.RoundingMode;
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.IncreaseDecreaseType;
28 import org.openhab.core.library.types.OnOffType;
29 import org.openhab.core.library.types.OpenClosedType;
30 import org.openhab.core.library.types.PercentType;
31 import org.openhab.core.types.State;
32 import org.openhab.core.types.Type;
35 * RFXCOM data class for lighting2 message.
37 * @author Pauli Anttila - Initial contribution
39 public class RFXComLighting2Message extends RFXComDeviceMessageImpl<RFXComLighting2Message.SubType> {
40 public enum SubType implements ByteEnumWrapper {
46 private final int subType;
48 SubType(int subType) {
49 this.subType = subType;
53 public byte toByte() {
54 return (byte) subType;
58 public enum Commands implements ByteEnumWrapper {
66 private final int command;
68 Commands(int command) {
69 this.command = command;
73 public byte toByte() {
74 return (byte) command;
78 public SubType subType;
81 public Commands command;
82 public byte dimmingLevel;
85 public RFXComLighting2Message() {
86 super(PacketType.LIGHTING2);
89 public RFXComLighting2Message(byte[] data) throws RFXComException {
94 public String toString() {
97 str += super.toString();
98 str += ", Sub type = " + subType;
99 str += ", Device Id = " + getDeviceId();
100 str += ", Command = " + command;
101 str += ", Dim level = " + dimmingLevel;
102 str += ", Signal level = " + signalLevel;
108 public void encodeMessage(byte[] data) throws RFXComException {
109 super.encodeMessage(data);
111 subType = fromByte(SubType.class, super.subType);
112 sensorId = (data[4] & 0xFF) << 24 | (data[5] & 0xFF) << 16 | (data[6] & 0xFF) << 8 | (data[7] & 0xFF);
113 command = fromByte(Commands.class, data[9]);
115 if ((command == Commands.GROUP_ON) || (command == Commands.GROUP_OFF)) {
121 dimmingLevel = data[10];
122 signalLevel = (byte) ((data[11] & 0xF0) >> 4);
126 public byte[] decodeMessage() {
127 byte[] data = new byte[12];
130 data[1] = LIGHTING2.toByte();
131 data[2] = subType.toByte();
133 data[4] = (byte) ((sensorId >> 24) & 0xFF);
134 data[5] = (byte) ((sensorId >> 16) & 0xFF);
135 data[6] = (byte) ((sensorId >> 8) & 0xFF);
136 data[7] = (byte) (sensorId & 0xFF);
139 data[9] = command.toByte();
140 data[10] = dimmingLevel;
141 data[11] = (byte) ((signalLevel & 0x0F) << 4);
147 public String getDeviceId() {
148 return sensorId + ID_DELIMITER + unitCode;
152 * Convert a 0-15 scale value to a percent type.
154 * @param pt percent type to convert
155 * @return converted value 0-15
157 public static int getDimLevelFromPercentType(PercentType pt) {
158 return pt.toBigDecimal().multiply(BigDecimal.valueOf(15))
159 .divide(PercentType.HUNDRED.toBigDecimal(), 0, RoundingMode.UP).intValue();
163 * Convert a 0-15 scale value to a percent type.
165 * @param value percent type to convert
166 * @return converted value 0-15
168 public static PercentType getPercentTypeFromDimLevel(int value) {
169 value = Math.min(value, 15);
171 return new PercentType(BigDecimal.valueOf(value).multiply(BigDecimal.valueOf(100))
172 .divide(BigDecimal.valueOf(15), 0, RoundingMode.UP).intValue());
176 public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
178 case CHANNEL_DIMMING_LEVEL:
179 return RFXComLighting2Message.getPercentTypeFromDimLevel(dimmingLevel);
181 case CHANNEL_COMMAND:
185 return OnOffType.OFF;
191 case SET_GROUP_LEVEL:
194 throw new RFXComUnsupportedChannelException("Can't convert " + command + " for " + channelId);
197 case CHANNEL_CONTACT:
201 return OpenClosedType.CLOSED;
205 return OpenClosedType.OPEN;
207 case SET_GROUP_LEVEL:
210 throw new RFXComUnsupportedChannelException("Can't convert " + command + " for " + channelId);
214 return super.convertToState(channelId, deviceState);
219 public void setSubType(SubType subType) {
220 this.subType = subType;
224 public void setDeviceId(String deviceId) throws RFXComException {
225 String[] ids = deviceId.split("\\" + ID_DELIMITER);
226 if (ids.length != 2) {
227 throw new RFXComException("Invalid device id '" + deviceId + "'");
230 sensorId = Integer.parseInt(ids[0]);
232 // Get unitcode, 0 means group
233 unitCode = Byte.parseByte(ids[1]);
241 public void convertFromState(String channelId, Type type) throws RFXComUnsupportedChannelException {
243 case CHANNEL_COMMAND:
244 if (type instanceof OnOffType) {
246 command = (type == OnOffType.ON ? GROUP_ON : GROUP_OFF);
249 command = (type == OnOffType.ON ? Commands.ON : Commands.OFF);
254 throw new RFXComUnsupportedChannelException("Channel " + channelId + " does not accept " + type);
258 case CHANNEL_DIMMING_LEVEL:
259 if (type instanceof OnOffType) {
260 command = (type == OnOffType.ON ? Commands.ON : Commands.OFF);
263 } else if (type instanceof PercentType) {
264 command = Commands.SET_LEVEL;
265 dimmingLevel = (byte) getDimLevelFromPercentType((PercentType) type);
267 if (dimmingLevel == 0) {
268 command = Commands.OFF;
270 } else if (type instanceof IncreaseDecreaseType) {
271 command = Commands.SET_LEVEL;
272 // Evert: I do not know how to get previous object state...
276 throw new RFXComUnsupportedChannelException("Channel " + channelId + " does not accept " + type);
281 throw new RFXComUnsupportedChannelException("Channel " + channelId + " is not relevant here");
286 public SubType convertSubType(String subType) throws RFXComUnsupportedValueException {
287 return ByteEnumUtil.convertSubType(SubType.class, subType);