2 * Copyright (c) 2010-2021 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.rfxcom.internal.messages;
15 import static org.openhab.binding.rfxcom.internal.RFXComBindingConstants.*;
16 import static org.openhab.binding.rfxcom.internal.messages.ByteEnumUtil.fromByte;
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;
28 * RFXCOM data class for UV and temperature message.
30 * @author Damien Servant - OpenHAB1 version
31 * @author Mike Jagdis - Initial contribution, OpenHAB2 version
33 public class RFXComUVMessage extends RFXComBatteryDeviceMessage<RFXComUVMessage.SubType> {
35 public enum SubType implements ByteEnumWrapper {
36 UV1(1), // UVN128, UV138
40 private final int subType;
42 SubType(int subType) {
43 this.subType = subType;
47 public byte toByte() {
48 return (byte) subType;
52 public SubType subType;
55 public double temperature;
57 public RFXComUVMessage() {
61 public RFXComUVMessage(byte[] data) throws RFXComException {
66 public String toString() {
68 return super.toString()
69 + ", Sub type = " + subType
70 + ", Device Id = " + getDeviceId()
72 + ", Temperature = " + temperature
73 + ", Signal level = " + signalLevel
74 + ", Battery level = " + batteryLevel;
79 public void encodeMessage(byte[] data) throws RFXComException {
80 super.encodeMessage(data);
82 subType = fromByte(SubType.class, super.subType);
83 sensorId = (data[4] & 0xFF) << 8 | (data[5] & 0xFF);
85 uv = (data[6] & 0xFF) * 0.1;
87 temperature = (short) ((data[7] & 0x7F) << 8 | (data[8] & 0xFF)) * 0.1;
88 if ((data[7] & 0x80) != 0) {
89 temperature = -temperature;
92 signalLevel = (byte) ((data[9] & 0xF0) >> 4);
93 batteryLevel = (byte) (data[9] & 0x0F);
97 public byte[] decodeMessage() {
98 byte[] data = new byte[10];
101 data[1] = RFXComBaseMessage.PacketType.UV.toByte();
102 data[2] = subType.toByte();
105 data[4] = (byte) ((sensorId & 0xFF00) >> 8);
106 data[5] = (byte) (sensorId & 0x00FF);
108 data[6] = (byte) (((short) (uv * 10)) & 0xFF);
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) {
117 data[9] = (byte) (((signalLevel & 0x0F) << 4) | (batteryLevel & 0x0F));
123 public String getDeviceId() {
124 return String.valueOf(sensorId);
128 public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
131 return new DecimalType(uv);
133 case CHANNEL_TEMPERATURE:
134 return (subType == SubType.UV3 ? new DecimalType(temperature) : UnDefType.UNDEF);
137 return super.convertToState(channelId, deviceState);
142 public void setSubType(SubType subType) {
143 throw new UnsupportedOperationException();
147 public void setDeviceId(String deviceId) {
148 throw new UnsupportedOperationException();
152 public void convertFromState(String channelId, Type type) {
153 throw new UnsupportedOperationException();
157 public SubType convertSubType(String subType) throws RFXComUnsupportedValueException {
158 return ByteEnumUtil.convertSubType(SubType.class, subType);