]> git.basschouten.com Git - openhab-addons.git/blob
eee10cd3ec86be22cb3631ad2c23a36bab4b78ab
[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  */
32 public class RFXComRainMessage extends RFXComBatteryDeviceMessage<RFXComRainMessage.SubType> {
33
34     public enum SubType implements ByteEnumWrapper {
35         RAIN1(1),
36         RAIN2(2),
37         RAIN3(3),
38         RAIN4(4),
39         RAIN5(5),
40         RAIN6(6),
41         RAIN7(7);
42
43         private final int subType;
44
45         SubType(int subType) {
46             this.subType = subType;
47         }
48
49         @Override
50         public byte toByte() {
51             return (byte) subType;
52         }
53     }
54
55     public SubType subType;
56     public int sensorId;
57     public double rainRate;
58     public double rainTotal;
59
60     public RFXComRainMessage() {
61         super(PacketType.RAIN);
62     }
63
64     public RFXComRainMessage(byte[] data) throws RFXComException {
65         encodeMessage(data);
66     }
67
68     @Override
69     public String toString() {
70         String str = "";
71
72         str += super.toString();
73         str += ", Sub type = " + subType;
74         str += ", Device Id = " + getDeviceId();
75         str += ", Rain rate = " + rainRate;
76         str += ", Rain total = " + rainTotal;
77         str += ", Signal level = " + signalLevel;
78         str += ", Battery level = " + batteryLevel;
79
80         return str;
81     }
82
83     @Override
84     public void encodeMessage(byte[] data) throws RFXComException {
85         super.encodeMessage(data);
86
87         subType = fromByte(SubType.class, super.subType);
88         sensorId = (data[4] & 0xFF) << 8 | (data[5] & 0xFF);
89
90         rainRate = (short) ((data[6] & 0xFF) << 8 | (data[7] & 0xFF));
91         if (subType == SubType.RAIN2) {
92             rainRate *= 0.01;
93         }
94
95         if (subType == SubType.RAIN6) {
96             rainTotal = (short) ((data[10] & 0xFF)) * 0.266;
97         } else {
98             rainTotal = (short) ((data[8] & 0xFF) << 8 | (data[9] & 0xFF) << 8 | (data[10] & 0xFF)) * 0.1;
99         }
100
101         signalLevel = (byte) ((data[11] & 0xF0) >> 4);
102         batteryLevel = (byte) (data[11] & 0x0F);
103     }
104
105     @Override
106     public byte[] decodeMessage() {
107         byte[] data = new byte[12];
108
109         data[0] = 0x0B;
110         data[1] = RFXComBaseMessage.PacketType.RAIN.toByte();
111         data[2] = subType.toByte();
112         data[3] = seqNbr;
113         data[4] = (byte) ((sensorId & 0xFF00) >> 8);
114         data[5] = (byte) (sensorId & 0x00FF);
115
116         short rainR = (short) Math.abs(rainRate * 100);
117         data[6] = (byte) ((rainR >> 8) & 0xFF);
118         data[7] = (byte) (rainR & 0xFF);
119
120         short rainT = (short) Math.abs(rainTotal * 10);
121         data[8] = (byte) ((rainT >> 16) & 0xFF);
122         data[9] = (byte) ((rainT >> 8) & 0xFF);
123         data[10] = (byte) (rainT & 0xFF);
124
125         data[11] = (byte) (((signalLevel & 0x0F) << 4) | (batteryLevel & 0x0F));
126
127         return data;
128     }
129
130     @Override
131     public String getDeviceId() {
132         return String.valueOf(sensorId);
133     }
134
135     @Override
136     public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
137         switch (channelId) {
138             case CHANNEL_RAIN_RATE:
139                 return new DecimalType(rainRate);
140
141             case CHANNEL_RAIN_TOTAL:
142                 return new DecimalType(rainTotal);
143
144             default:
145                 return super.convertToState(channelId, deviceState);
146         }
147     }
148
149     @Override
150     public void setSubType(SubType subType) {
151         throw new UnsupportedOperationException();
152     }
153
154     @Override
155     public void setDeviceId(String deviceId) {
156         throw new UnsupportedOperationException();
157     }
158
159     @Override
160     public void convertFromState(String channelId, Type type) {
161         throw new UnsupportedOperationException();
162     }
163
164     @Override
165     public SubType convertSubType(String subType) throws RFXComUnsupportedValueException {
166         return ByteEnumUtil.convertSubType(SubType.class, subType);
167     }
168 }