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;
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.types.State;
24 import org.openhab.core.types.Type;
27 * RFXCOM data class for temperature and humidity message.
29 * @author Marc SAUVEUR - Initial contribution
30 * @author Pauli Anttila - Migrated for OH2
31 * @author Mike Jagdis - Support all available data from sensors
33 public class RFXComWindMessage extends RFXComBatteryDeviceMessage<RFXComWindMessage.SubType> {
35 public enum SubType implements ByteEnumWrapper {
44 private final int subType;
46 SubType(int subType) {
47 this.subType = subType;
51 public byte toByte() {
52 return (byte) subType;
56 public SubType subType;
58 public double windDirection;
59 public double windSpeed;
60 public double avgWindSpeed;
61 public double temperature;
62 public double chillTemperature;
64 public RFXComWindMessage() {
65 super(PacketType.WIND);
68 public RFXComWindMessage(byte[] data) throws RFXComException {
73 public String toString() {
75 return super.toString()
76 + ", Sub type = " + subType
77 + ", Device Id = " + getDeviceId()
78 + ", Wind direction = " + windDirection
79 + ", Wind gust = " + windSpeed
80 + ", Average wind speed = " + avgWindSpeed
81 + ", Temperature = " + temperature
82 + ", Chill temperature = " + chillTemperature
83 + ", Signal level = " + signalLevel
84 + ", Battery level = " + batteryLevel;
89 public void encodeMessage(byte[] data) throws RFXComException {
90 super.encodeMessage(data);
92 subType = fromByte(SubType.class, super.subType);
93 sensorId = (data[4] & 0xFF) << 8 | (data[5] & 0xFF);
95 windDirection = (short) ((data[6] & 0xFF) << 8 | (data[7] & 0xFF));
97 if (subType != SubType.WIND5) {
98 avgWindSpeed = (short) ((data[8] & 0xFF) << 8 | (data[9] & 0xFF)) * 0.1;
101 windSpeed = (short) ((data[10] & 0xFF) << 8 | (data[11] & 0xFF)) * 0.1;
103 if (subType == SubType.WIND4) {
104 temperature = (short) ((data[12] & 0x7F) << 8 | (data[13] & 0xFF)) * 0.1;
105 if ((data[12] & 0x80) != 0) {
106 temperature = -temperature;
109 chillTemperature = (short) ((data[14] & 0x7F) << 8 | (data[15] & 0xFF)) * 0.1;
110 if ((data[14] & 0x80) != 0) {
111 chillTemperature = -chillTemperature;
115 signalLevel = (byte) ((data[16] & 0xF0) >> 4);
116 batteryLevel = (byte) (data[16] & 0x0F);
120 public byte[] decodeMessage() {
121 byte[] data = new byte[17];
124 data[1] = PacketType.WIND.toByte();
125 data[2] = subType.toByte();
127 data[4] = (byte) ((sensorId & 0xFF00) >> 8);
128 data[5] = (byte) (sensorId & 0x00FF);
130 short absWindDirection = (short) Math.abs(windDirection);
131 data[6] = (byte) ((absWindDirection >> 8) & 0xFF);
132 data[7] = (byte) (absWindDirection & 0xFF);
134 if (subType != SubType.WIND5) {
135 int absAvgWindSpeedTimesTen = (short) Math.abs(avgWindSpeed) * 10;
136 data[8] = (byte) ((absAvgWindSpeedTimesTen >> 8) & 0xFF);
137 data[9] = (byte) (absAvgWindSpeedTimesTen & 0xFF);
140 int absWindSpeedTimesTen = (short) Math.abs(windSpeed) * 10;
141 data[10] = (byte) ((absWindSpeedTimesTen >> 8) & 0xFF);
142 data[11] = (byte) (absWindSpeedTimesTen & 0xFF);
144 if (subType == SubType.WIND4) {
145 int temp = (short) Math.abs(temperature) * 10;
146 data[12] = (byte) ((temp >> 8) & 0x7F);
147 data[13] = (byte) (temp & 0xFF);
148 if (temperature < 0) {
152 int chill = (short) Math.abs(chillTemperature) * 10;
153 data[14] = (byte) ((chill >> 8) & 0x7F);
154 data[15] = (byte) (chill & 0xFF);
155 if (chillTemperature < 0) {
160 data[16] = (byte) (((signalLevel & 0x0F) << 4) | (batteryLevel & 0x0F));
166 public String getDeviceId() {
167 return String.valueOf(sensorId);
171 public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
173 case CHANNEL_WIND_DIRECTION:
174 return new DecimalType(windDirection);
176 case CHANNEL_AVG_WIND_SPEED:
177 return new DecimalType(avgWindSpeed);
179 case CHANNEL_WIND_SPEED:
180 return new DecimalType(windSpeed);
182 case CHANNEL_TEMPERATURE:
183 return new DecimalType(temperature);
185 case CHANNEL_CHILL_TEMPERATURE:
186 return new DecimalType(chillTemperature);
189 return super.convertToState(channelId, deviceState);
194 public void setSubType(SubType subType) {
195 throw new UnsupportedOperationException();
199 public void setDeviceId(String deviceId) {
200 throw new UnsupportedOperationException();
204 public void convertFromState(String channelId, Type type) {
205 throw new UnsupportedOperationException();
209 public SubType convertSubType(String subType) throws RFXComUnsupportedValueException {
210 return ByteEnumUtil.convertSubType(SubType.class, subType);