]> git.basschouten.com Git - openhab-addons.git/blob
168401653764faf9c4b2809d9754b7be78776b0c
[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.library.types.StringType;
24 import org.openhab.core.types.State;
25 import org.openhab.core.types.Type;
26
27 /**
28  * RFXCOM data class for humidity message.
29  *
30  * @author Pauli Anttila - Initial contribution
31  */
32 public class RFXComHumidityMessage extends RFXComBatteryDeviceMessage<RFXComHumidityMessage.SubType> {
33
34     public enum SubType implements ByteEnumWrapper {
35         HUM1(1),
36         HUM2(2),
37         HUM3(3);
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 enum HumidityStatus implements ByteEnumWrapper {
52         NORMAL(0),
53         COMFORT(1),
54         DRY(2),
55         WET(3);
56
57         private final int humidityStatus;
58
59         HumidityStatus(int humidityStatus) {
60             this.humidityStatus = humidityStatus;
61         }
62
63         @Override
64         public byte toByte() {
65             return (byte) humidityStatus;
66         }
67     }
68
69     public SubType subType;
70     public int sensorId;
71     public byte humidity;
72     public HumidityStatus humidityStatus;
73
74     public RFXComHumidityMessage() {
75         super(PacketType.HUMIDITY);
76     }
77
78     public RFXComHumidityMessage(byte[] data) throws RFXComException {
79         encodeMessage(data);
80     }
81
82     @Override
83     public String toString() {
84         String str = "";
85
86         str += super.toString();
87         str += ", Sub type = " + subType;
88         str += ", Device Id = " + getDeviceId();
89         str += ", Humidity = " + humidity;
90         str += ", Humidity status = " + humidityStatus;
91         str += ", Signal level = " + signalLevel;
92         str += ", Battery level = " + batteryLevel;
93
94         return str;
95     }
96
97     @Override
98     public void encodeMessage(byte[] data) throws RFXComException {
99         super.encodeMessage(data);
100
101         subType = fromByte(SubType.class, super.subType);
102         sensorId = (data[4] & 0xFF) << 8 | (data[5] & 0xFF);
103         humidity = data[6];
104         humidityStatus = fromByte(HumidityStatus.class, data[7]);
105         signalLevel = (byte) ((data[8] & 0xF0) >> 4);
106         batteryLevel = (byte) (data[8] & 0x0F);
107     }
108
109     @Override
110     public byte[] decodeMessage() {
111         byte[] data = new byte[9];
112
113         data[0] = 0x08;
114         data[1] = RFXComBaseMessage.PacketType.HUMIDITY.toByte();
115         data[2] = subType.toByte();
116         data[3] = seqNbr;
117         data[4] = (byte) ((sensorId & 0xFF00) >> 8);
118         data[5] = (byte) (sensorId & 0x00FF);
119         data[6] = humidity;
120         data[7] = humidityStatus.toByte();
121         data[8] = (byte) (((signalLevel & 0x0F) << 4) | (batteryLevel & 0x0F));
122
123         return data;
124     }
125
126     @Override
127     public String getDeviceId() {
128         return String.valueOf(sensorId);
129     }
130
131     @Override
132     public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
133         switch (channelId) {
134             case CHANNEL_HUMIDITY:
135                 return new DecimalType(humidity);
136
137             case CHANNEL_HUMIDITY_STATUS:
138                 return new StringType(humidityStatus.toString());
139
140             default:
141                 return super.convertToState(channelId, deviceState);
142         }
143     }
144
145     @Override
146     public void setSubType(SubType subType) {
147         throw new UnsupportedOperationException();
148     }
149
150     @Override
151     public void setDeviceId(String deviceId) {
152         throw new UnsupportedOperationException();
153     }
154
155     @Override
156     public void convertFromState(String channelId, Type type) {
157         throw new UnsupportedOperationException();
158     }
159
160     @Override
161     public SubType convertSubType(String subType) throws RFXComUnsupportedValueException {
162         return ByteEnumUtil.convertSubType(SubType.class, subType);
163     }
164 }