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