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;
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.config.RFXComDeviceConfiguration;
24 import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
25 import org.openhab.binding.rfxcom.internal.exceptions.RFXComInvalidStateException;
26 import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedChannelException;
27 import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedValueException;
28 import org.openhab.binding.rfxcom.internal.handler.DeviceState;
29 import org.openhab.core.library.types.IncreaseDecreaseType;
30 import org.openhab.core.library.types.OnOffType;
31 import org.openhab.core.library.types.OpenClosedType;
32 import org.openhab.core.library.types.PercentType;
33 import org.openhab.core.types.State;
34 import org.openhab.core.types.Type;
37 * RFXCOM data class for lighting2 message.
39 * @author Pauli Anttila - Initial contribution
41 public class RFXComLighting2Message extends RFXComDeviceMessageImpl<RFXComLighting2Message.SubType> {
42 public enum SubType implements ByteEnumWrapper {
48 private final int subType;
50 SubType(int subType) {
51 this.subType = subType;
55 public byte toByte() {
56 return (byte) subType;
60 public enum Commands implements ByteEnumWrapper {
68 private final int command;
70 Commands(int command) {
71 this.command = command;
75 public byte toByte() {
76 return (byte) command;
80 public SubType subType;
83 public Commands command;
84 public byte dimmingLevel;
87 public RFXComLighting2Message() {
88 super(PacketType.LIGHTING2);
91 public RFXComLighting2Message(byte[] data) throws RFXComException {
96 public String toString() {
99 str += super.toString();
100 str += ", Sub type = " + subType;
101 str += ", Device Id = " + getDeviceId();
102 str += ", Command = " + command;
103 str += ", Dim level = " + dimmingLevel;
104 str += ", Signal level = " + signalLevel;
110 public void encodeMessage(byte[] data) throws RFXComException {
111 super.encodeMessage(data);
113 subType = fromByte(SubType.class, super.subType);
114 sensorId = (data[4] & 0xFF) << 24 | (data[5] & 0xFF) << 16 | (data[6] & 0xFF) << 8 | (data[7] & 0xFF);
115 command = fromByte(Commands.class, data[9]);
117 if ((command == Commands.GROUP_ON) || (command == Commands.GROUP_OFF)) {
123 dimmingLevel = data[10];
124 signalLevel = (byte) ((data[11] & 0xF0) >> 4);
128 public byte[] decodeMessage() {
129 byte[] data = new byte[12];
132 data[1] = LIGHTING2.toByte();
133 data[2] = subType.toByte();
135 data[4] = (byte) ((sensorId >> 24) & 0xFF);
136 data[5] = (byte) ((sensorId >> 16) & 0xFF);
137 data[6] = (byte) ((sensorId >> 8) & 0xFF);
138 data[7] = (byte) (sensorId & 0xFF);
141 data[9] = command.toByte();
142 data[10] = dimmingLevel;
143 data[11] = (byte) ((signalLevel & 0x0F) << 4);
149 public String getDeviceId() {
150 return sensorId + ID_DELIMITER + unitCode;
154 * Convert a 0-15 scale value to a percent type.
156 * @param pt percent type to convert
157 * @return converted value 0-15
159 public static int getDimLevelFromPercentType(PercentType pt) {
160 return pt.toBigDecimal().multiply(BigDecimal.valueOf(15))
161 .divide(PercentType.HUNDRED.toBigDecimal(), 0, RoundingMode.UP).intValue();
165 * Convert a 0-15 scale value to a percent type.
167 * @param value percent type to convert
168 * @return converted value 0-15
170 public static PercentType getPercentTypeFromDimLevel(int value) {
171 value = Math.min(value, 15);
173 return new PercentType(BigDecimal.valueOf(value).multiply(BigDecimal.valueOf(100))
174 .divide(BigDecimal.valueOf(15), 0, RoundingMode.UP).intValue());
178 public State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
179 throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
181 case CHANNEL_DIMMING_LEVEL:
182 return RFXComLighting2Message.getPercentTypeFromDimLevel(dimmingLevel);
184 case CHANNEL_COMMAND:
188 return OnOffType.OFF;
194 case SET_GROUP_LEVEL:
197 throw new RFXComUnsupportedChannelException("Can't convert " + command + " for " + channelId);
200 case CHANNEL_CONTACT:
204 return OpenClosedType.CLOSED;
208 return OpenClosedType.OPEN;
210 case SET_GROUP_LEVEL:
213 throw new RFXComUnsupportedChannelException("Can't convert " + command + " for " + channelId);
217 return super.convertToState(channelId, config, deviceState);
222 public void setSubType(SubType subType) {
223 this.subType = subType;
227 public void setDeviceId(String deviceId) throws RFXComException {
228 String[] ids = deviceId.split("\\" + ID_DELIMITER);
229 if (ids.length != 2) {
230 throw new RFXComException("Invalid device id '" + deviceId + "'");
233 sensorId = Integer.parseInt(ids[0]);
235 // Get unitcode, 0 means group
236 unitCode = Byte.parseByte(ids[1]);
244 public void convertFromState(String channelId, Type type) throws RFXComUnsupportedChannelException {
246 case CHANNEL_COMMAND:
247 if (type instanceof OnOffType) {
249 command = (type == OnOffType.ON ? GROUP_ON : GROUP_OFF);
252 command = (type == OnOffType.ON ? Commands.ON : Commands.OFF);
257 throw new RFXComUnsupportedChannelException("Channel " + channelId + " does not accept " + type);
261 case CHANNEL_DIMMING_LEVEL:
262 if (type instanceof OnOffType) {
263 command = (type == OnOffType.ON ? Commands.ON : Commands.OFF);
266 } else if (type instanceof PercentType) {
267 command = Commands.SET_LEVEL;
268 dimmingLevel = (byte) getDimLevelFromPercentType((PercentType) type);
270 if (dimmingLevel == 0) {
271 command = Commands.OFF;
273 } else if (type instanceof IncreaseDecreaseType) {
274 command = Commands.SET_LEVEL;
275 // Evert: I do not know how to get previous object state...
279 throw new RFXComUnsupportedChannelException("Channel " + channelId + " does not accept " + type);
284 throw new RFXComUnsupportedChannelException("Channel " + channelId + " is not relevant here");
289 public SubType convertSubType(String subType) throws RFXComUnsupportedValueException {
290 return ByteEnumUtil.convertSubType(SubType.class, subType);