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