]> git.basschouten.com Git - openhab-addons.git/blob
1d4d7b485158ea737f4d67a2f2ff9bcdc79a7033
[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 Rain message.
30  *
31  * @author Damien Servant - Initial contribution
32  * @author Martin van Wingerden - ported to openHAB 2.0
33  */
34 public class RFXComTemperatureRainMessage extends RFXComBatteryDeviceMessage<RFXComTemperatureRainMessage.SubType> {
35
36     public enum SubType implements ByteEnumWrapper {
37         WS1200(1);
38
39         private final int subType;
40
41         SubType(int subType) {
42             this.subType = subType;
43         }
44
45         @Override
46         public byte toByte() {
47             return (byte) subType;
48         }
49     }
50
51     public SubType subType;
52     public int sensorId;
53     public double temperature;
54     public double rainTotal;
55
56     public RFXComTemperatureRainMessage() {
57         super(PacketType.TEMPERATURE_RAIN);
58     }
59
60     public RFXComTemperatureRainMessage(byte[] data) throws RFXComException {
61         encodeMessage(data);
62     }
63
64     @Override
65     public String toString() {
66         String str = super.toString();
67         str += ", Sub type = " + subType;
68         str += ", Device Id = " + sensorId;
69         str += ", Temperature = " + temperature;
70         str += ", Rain total = " + rainTotal;
71         str += ", Signal level = " + signalLevel;
72         str += ", Battery level = " + batteryLevel;
73
74         return str;
75     }
76
77     @Override
78     public String getDeviceId() {
79         return String.valueOf(sensorId);
80     }
81
82     @Override
83     public void encodeMessage(byte[] data) throws RFXComException {
84         super.encodeMessage(data);
85
86         subType = fromByte(SubType.class, super.subType);
87         sensorId = (data[4] & 0xFF) << 8 | (data[5] & 0xFF);
88
89         temperature = ((data[6] & 0x7F) << 8 | (data[7] & 0xFF)) * 0.1;
90         if ((data[6] & 0x80) != 0) {
91             temperature = -temperature;
92         }
93         rainTotal = ((data[8] & 0xFF) << 8 | (data[9] & 0xFF)) * 0.1;
94         signalLevel = (byte) ((data[10] & 0xF0) >> 4);
95         batteryLevel = (byte) (data[10] & 0x0F);
96     }
97
98     @Override
99     public byte[] decodeMessage() {
100         byte[] data = new byte[11];
101
102         data[0] = (byte) (data.length - 1);
103         data[1] = RFXComBaseMessage.PacketType.TEMPERATURE_RAIN.toByte();
104         data[2] = subType.toByte();
105         data[3] = seqNbr;
106         data[4] = (byte) ((sensorId & 0xFF00) >> 8);
107         data[5] = (byte) (sensorId & 0x00FF);
108
109         short temp = (short) Math.abs(temperature * 10);
110         data[6] = (byte) ((temp >> 8) & 0xFF);
111         data[7] = (byte) (temp & 0xFF);
112         if (temperature < 0) {
113             data[6] |= 0x80;
114         }
115
116         short rainT = (short) Math.abs(rainTotal * 10);
117         data[8] = (byte) ((rainT >> 8) & 0xFF);
118         data[9] = (byte) (rainT & 0xFF);
119         data[10] = (byte) (((signalLevel & 0x0F) << 4) | (batteryLevel & 0x0F));
120
121         return data;
122     }
123
124     @Override
125     public State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
126             throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
127         switch (channelId) {
128             case CHANNEL_TEMPERATURE:
129                 return new DecimalType(temperature);
130
131             case CHANNEL_RAIN_TOTAL:
132                 return new DecimalType(rainTotal);
133
134             default:
135                 return super.convertToState(channelId, config, deviceState);
136         }
137     }
138
139     @Override
140     public void convertFromState(String channelId, Type type) {
141         throw new UnsupportedOperationException();
142     }
143
144     @Override
145     public SubType convertSubType(String subType) throws RFXComUnsupportedValueException {
146         return ByteEnumUtil.convertSubType(SubType.class, subType);
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 }