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