2 * Copyright (c) 2010-2024 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.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;
30 * RFXCOM data class for UV and temperature message.
32 * @author Damien Servant - OpenHAB1 version
33 * @author Mike Jagdis - Initial contribution, OpenHAB2 version
35 public class RFXComUVMessage extends RFXComBatteryDeviceMessage<RFXComUVMessage.SubType> {
37 public enum SubType implements ByteEnumWrapper {
38 UV1(1), // UVN128, UV138
42 private final int subType;
44 SubType(int subType) {
45 this.subType = subType;
49 public byte toByte() {
50 return (byte) subType;
54 public SubType subType;
57 public double temperature;
59 public RFXComUVMessage() {
63 public RFXComUVMessage(byte[] data) throws RFXComException {
68 public String toString() {
70 return super.toString()
71 + ", Sub type = " + subType
72 + ", Device Id = " + getDeviceId()
74 + ", Temperature = " + temperature
75 + ", Signal level = " + signalLevel
76 + ", Battery level = " + batteryLevel;
81 public void encodeMessage(byte[] data) throws RFXComException {
82 super.encodeMessage(data);
84 subType = fromByte(SubType.class, super.subType);
85 sensorId = (data[4] & 0xFF) << 8 | (data[5] & 0xFF);
87 uv = (data[6] & 0xFF) * 0.1;
89 temperature = (short) ((data[7] & 0x7F) << 8 | (data[8] & 0xFF)) * 0.1;
90 if ((data[7] & 0x80) != 0) {
91 temperature = -temperature;
94 signalLevel = (byte) ((data[9] & 0xF0) >> 4);
95 batteryLevel = (byte) (data[9] & 0x0F);
99 public byte[] decodeMessage() {
100 byte[] data = new byte[10];
103 data[1] = RFXComBaseMessage.PacketType.UV.toByte();
104 data[2] = subType.toByte();
107 data[4] = (byte) ((sensorId & 0xFF00) >> 8);
108 data[5] = (byte) (sensorId & 0x00FF);
110 data[6] = (byte) (((short) (uv * 10)) & 0xFF);
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) {
119 data[9] = (byte) (((signalLevel & 0x0F) << 4) | (batteryLevel & 0x0F));
125 public String getDeviceId() {
126 return String.valueOf(sensorId);
130 public State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
131 throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
134 return new DecimalType(uv);
136 case CHANNEL_TEMPERATURE:
137 return (subType == SubType.UV3 ? new DecimalType(temperature) : UnDefType.UNDEF);
140 return super.convertToState(channelId, config, deviceState);
145 public void setSubType(SubType subType) {
146 throw new UnsupportedOperationException();
150 public void setDeviceId(String deviceId) {
151 throw new UnsupportedOperationException();
155 public void convertFromState(String channelId, Type type) {
156 throw new UnsupportedOperationException();
160 public SubType convertSubType(String subType) throws RFXComUnsupportedValueException {
161 return ByteEnumUtil.convertSubType(SubType.class, subType);