]> git.basschouten.com Git - openhab-addons.git/blob
af8184f6868aa126220f960cd153aaa473ee8214
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.rfxcom.internal.messages;
14
15 import static org.openhab.binding.rfxcom.internal.RFXComBindingConstants.*;
16 import static org.openhab.binding.rfxcom.internal.messages.ByteEnumUtil.fromByte;
17
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;
25
26 /**
27  * RFXCOM data class for temperature and humidity message.
28  *
29  * @author Marc SAUVEUR - Initial contribution
30  * @author Pauli Anttila - Migrated for OH2
31  * @author Mike Jagdis - Support all available data from sensors
32  */
33 public class RFXComWindMessage extends RFXComBatteryDeviceMessage<RFXComWindMessage.SubType> {
34
35     public enum SubType implements ByteEnumWrapper {
36         WIND1(1),
37         WIND2(2),
38         WIND3(3),
39         WIND4(4),
40         WIND5(5),
41         WIND6(6),
42         WIND7(7);
43
44         private final int subType;
45
46         SubType(int subType) {
47             this.subType = subType;
48         }
49
50         @Override
51         public byte toByte() {
52             return (byte) subType;
53         }
54     }
55
56     public SubType subType;
57     public int sensorId;
58     public double windDirection;
59     public double windSpeed;
60     public double avgWindSpeed;
61     public double temperature;
62     public double chillTemperature;
63
64     public RFXComWindMessage() {
65         super(PacketType.WIND);
66     }
67
68     public RFXComWindMessage(byte[] data) throws RFXComException {
69         encodeMessage(data);
70     }
71
72     @Override
73     public String toString() {
74         //@formatter:off
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;
85         //@formatter:on
86     }
87
88     @Override
89     public void encodeMessage(byte[] data) throws RFXComException {
90         super.encodeMessage(data);
91
92         subType = fromByte(SubType.class, super.subType);
93         sensorId = (data[4] & 0xFF) << 8 | (data[5] & 0xFF);
94
95         windDirection = (short) ((data[6] & 0xFF) << 8 | (data[7] & 0xFF));
96
97         if (subType != SubType.WIND5) {
98             avgWindSpeed = (short) ((data[8] & 0xFF) << 8 | (data[9] & 0xFF)) * 0.1;
99         }
100
101         windSpeed = (short) ((data[10] & 0xFF) << 8 | (data[11] & 0xFF)) * 0.1;
102
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;
107             }
108
109             chillTemperature = (short) ((data[14] & 0x7F) << 8 | (data[15] & 0xFF)) * 0.1;
110             if ((data[14] & 0x80) != 0) {
111                 chillTemperature = -chillTemperature;
112             }
113         }
114
115         signalLevel = (byte) ((data[16] & 0xF0) >> 4);
116         batteryLevel = (byte) (data[16] & 0x0F);
117     }
118
119     @Override
120     public byte[] decodeMessage() {
121         byte[] data = new byte[17];
122
123         data[0] = 0x10;
124         data[1] = PacketType.WIND.toByte();
125         data[2] = subType.toByte();
126         data[3] = seqNbr;
127         data[4] = (byte) ((sensorId & 0xFF00) >> 8);
128         data[5] = (byte) (sensorId & 0x00FF);
129
130         short absWindDirection = (short) Math.abs(windDirection);
131         data[6] = (byte) ((absWindDirection >> 8) & 0xFF);
132         data[7] = (byte) (absWindDirection & 0xFF);
133
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);
138         }
139
140         int absWindSpeedTimesTen = (short) Math.abs(windSpeed) * 10;
141         data[10] = (byte) ((absWindSpeedTimesTen >> 8) & 0xFF);
142         data[11] = (byte) (absWindSpeedTimesTen & 0xFF);
143
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) {
149                 data[12] |= 0x80;
150             }
151
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) {
156                 data[14] |= 0x80;
157             }
158         }
159
160         data[16] = (byte) (((signalLevel & 0x0F) << 4) | (batteryLevel & 0x0F));
161
162         return data;
163     }
164
165     @Override
166     public String getDeviceId() {
167         return String.valueOf(sensorId);
168     }
169
170     @Override
171     public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
172         switch (channelId) {
173             case CHANNEL_WIND_DIRECTION:
174                 return new DecimalType(windDirection);
175
176             case CHANNEL_AVG_WIND_SPEED:
177                 return new DecimalType(avgWindSpeed);
178
179             case CHANNEL_WIND_SPEED:
180                 return new DecimalType(windSpeed);
181
182             case CHANNEL_TEMPERATURE:
183                 return new DecimalType(temperature);
184
185             case CHANNEL_CHILL_TEMPERATURE:
186                 return new DecimalType(chillTemperature);
187
188             default:
189                 return super.convertToState(channelId, deviceState);
190         }
191     }
192
193     @Override
194     public void setSubType(SubType subType) {
195         throw new UnsupportedOperationException();
196     }
197
198     @Override
199     public void setDeviceId(String deviceId) {
200         throw new UnsupportedOperationException();
201     }
202
203     @Override
204     public void convertFromState(String channelId, Type type) {
205         throw new UnsupportedOperationException();
206     }
207
208     @Override
209     public SubType convertSubType(String subType) throws RFXComUnsupportedValueException {
210         return ByteEnumUtil.convertSubType(SubType.class, subType);
211     }
212 }