2 * Copyright (c) 2010-2020 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.exceptions.RFXComException;
19 import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedChannelException;
20 import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedValueException;
21 import org.openhab.binding.rfxcom.internal.handler.DeviceState;
22 import org.openhab.core.library.types.DecimalType;
23 import org.openhab.core.library.types.StringType;
24 import org.openhab.core.types.State;
25 import org.openhab.core.types.Type;
28 * RFXCOM data class for humidity message.
30 * @author Pauli Anttila - Initial contribution
32 public class RFXComHumidityMessage extends RFXComBatteryDeviceMessage<RFXComHumidityMessage.SubType> {
34 public enum SubType implements ByteEnumWrapper {
39 private final int subType;
41 SubType(int subType) {
42 this.subType = subType;
46 public byte toByte() {
47 return (byte) subType;
51 public enum HumidityStatus implements ByteEnumWrapper {
57 private final int humidityStatus;
59 HumidityStatus(int humidityStatus) {
60 this.humidityStatus = humidityStatus;
64 public byte toByte() {
65 return (byte) humidityStatus;
69 public SubType subType;
72 public HumidityStatus humidityStatus;
74 public RFXComHumidityMessage() {
75 super(PacketType.HUMIDITY);
78 public RFXComHumidityMessage(byte[] data) throws RFXComException {
83 public String toString() {
86 str += super.toString();
87 str += ", Sub type = " + subType;
88 str += ", Device Id = " + getDeviceId();
89 str += ", Humidity = " + humidity;
90 str += ", Humidity status = " + humidityStatus;
91 str += ", Signal level = " + signalLevel;
92 str += ", Battery level = " + batteryLevel;
98 public void encodeMessage(byte[] data) throws RFXComException {
99 super.encodeMessage(data);
101 subType = fromByte(SubType.class, super.subType);
102 sensorId = (data[4] & 0xFF) << 8 | (data[5] & 0xFF);
104 humidityStatus = fromByte(HumidityStatus.class, data[7]);
105 signalLevel = (byte) ((data[8] & 0xF0) >> 4);
106 batteryLevel = (byte) (data[8] & 0x0F);
110 public byte[] decodeMessage() {
111 byte[] data = new byte[9];
114 data[1] = RFXComBaseMessage.PacketType.HUMIDITY.toByte();
115 data[2] = subType.toByte();
117 data[4] = (byte) ((sensorId & 0xFF00) >> 8);
118 data[5] = (byte) (sensorId & 0x00FF);
120 data[7] = humidityStatus.toByte();
121 data[8] = (byte) (((signalLevel & 0x0F) << 4) | (batteryLevel & 0x0F));
127 public String getDeviceId() {
128 return String.valueOf(sensorId);
132 public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
134 case CHANNEL_HUMIDITY:
135 return new DecimalType(humidity);
137 case CHANNEL_HUMIDITY_STATUS:
138 return new StringType(humidityStatus.toString());
141 return super.convertToState(channelId, deviceState);
146 public void setSubType(SubType subType) {
147 throw new UnsupportedOperationException();
151 public void setDeviceId(String deviceId) {
152 throw new UnsupportedOperationException();
156 public void convertFromState(String channelId, Type type) {
157 throw new UnsupportedOperationException();
161 public SubType convertSubType(String subType) throws RFXComUnsupportedValueException {
162 return ByteEnumUtil.convertSubType(SubType.class, subType);