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