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.DecimalType;
25 import org.openhab.core.library.types.StringType;
26 import org.openhab.core.types.State;
27 import org.openhab.core.types.Type;
30 * RFXCOM data class for humidity message.
32 * @author Pauli Anttila - Initial contribution
34 public class RFXComHumidityMessage extends RFXComBatteryDeviceMessage<RFXComHumidityMessage.SubType> {
36 public enum SubType implements ByteEnumWrapper {
41 private final int subType;
43 SubType(int subType) {
44 this.subType = subType;
48 public byte toByte() {
49 return (byte) subType;
53 public enum HumidityStatus implements ByteEnumWrapper {
59 private final int humidityStatus;
61 HumidityStatus(int humidityStatus) {
62 this.humidityStatus = humidityStatus;
66 public byte toByte() {
67 return (byte) humidityStatus;
71 public SubType subType;
74 public HumidityStatus humidityStatus;
76 public RFXComHumidityMessage() {
77 super(PacketType.HUMIDITY);
80 public RFXComHumidityMessage(byte[] data) throws RFXComException {
85 public String toString() {
88 str += super.toString();
89 str += ", Sub type = " + subType;
90 str += ", Device Id = " + getDeviceId();
91 str += ", Humidity = " + humidity;
92 str += ", Humidity status = " + humidityStatus;
93 str += ", Signal level = " + signalLevel;
94 str += ", Battery level = " + batteryLevel;
100 public void encodeMessage(byte[] data) throws RFXComException {
101 super.encodeMessage(data);
103 subType = fromByte(SubType.class, super.subType);
104 sensorId = (data[4] & 0xFF) << 8 | (data[5] & 0xFF);
106 humidityStatus = fromByte(HumidityStatus.class, data[7]);
107 signalLevel = (byte) ((data[8] & 0xF0) >> 4);
108 batteryLevel = (byte) (data[8] & 0x0F);
112 public byte[] decodeMessage() {
113 byte[] data = new byte[9];
116 data[1] = RFXComBaseMessage.PacketType.HUMIDITY.toByte();
117 data[2] = subType.toByte();
119 data[4] = (byte) ((sensorId & 0xFF00) >> 8);
120 data[5] = (byte) (sensorId & 0x00FF);
122 data[7] = humidityStatus.toByte();
123 data[8] = (byte) (((signalLevel & 0x0F) << 4) | (batteryLevel & 0x0F));
129 public String getDeviceId() {
130 return String.valueOf(sensorId);
134 public State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
135 throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
137 case CHANNEL_HUMIDITY:
138 return new DecimalType(humidity);
140 case CHANNEL_HUMIDITY_STATUS:
141 return new StringType(humidityStatus.toString());
144 return super.convertToState(channelId, config, deviceState);
149 public void setSubType(SubType subType) {
150 throw new UnsupportedOperationException();
154 public void setDeviceId(String deviceId) {
155 throw new UnsupportedOperationException();
159 public void convertFromState(String channelId, Type type) {
160 throw new UnsupportedOperationException();
164 public SubType convertSubType(String subType) throws RFXComUnsupportedValueException {
165 return ByteEnumUtil.convertSubType(SubType.class, subType);