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