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