2 * Copyright (c) 2010-2023 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;
29 * RFXCOM data class for current message.
31 * @author Ben Jones - Initial contribution
32 * @author Pauli Anttila - for the Similar RFXComEnergyMessage code
33 * @author Jordan Cook - Added support for CURRENT devices, such as OWL CM113
34 * @author Martin van Wingerden - Updated CurrentMessage code to new style
36 public class RFXComCurrentMessage extends RFXComBatteryDeviceMessage<RFXComCurrentMessage.SubType> {
38 public enum SubType implements ByteEnumWrapper {
41 private final int subType;
43 SubType(int subType) {
44 this.subType = subType;
48 public byte toByte() {
49 return (byte) subType;
53 public SubType subType;
56 public double channel1Amps;
57 public double channel2Amps;
58 public double channel3Amps;
60 public RFXComCurrentMessage() {
61 super(PacketType.CURRENT);
64 public RFXComCurrentMessage(byte[] data) throws RFXComException {
69 public String toString() {
72 str += super.toString();
73 str += ", Sub type = " + subType;
74 str += ", Device Id = " + sensorId;
75 str += ", Count = " + count;
76 str += ", Channel 1 Amps = " + channel1Amps;
77 str += ", Channel 2 Amps = " + channel2Amps;
78 str += ", Channel 3 Amps = " + channel3Amps;
79 str += ", Signal level = " + signalLevel;
80 str += ", Battery level = " + batteryLevel;
86 public void encodeMessage(byte[] data) throws RFXComException {
87 super.encodeMessage(data);
89 subType = fromByte(SubType.class, super.subType);
91 sensorId = (data[4] & 0xFF) << 8 | (data[5] & 0xFF);
94 channel1Amps = ((data[7] & 0xFF) << 8 | (data[8] & 0xFF)) / 10.0;
95 channel2Amps = ((data[9] & 0xFF) << 8 | (data[10] & 0xFF)) / 10.0;
96 channel3Amps = ((data[11] & 0xFF) << 8 | (data[12] & 0xFF)) / 10.0;
98 signalLevel = (byte) ((data[13] & 0xF0) >> 4);
99 batteryLevel = (byte) (data[13] & 0x0F);
103 public byte[] decodeMessage() {
104 byte[] data = new byte[14];
107 data[1] = PacketType.CURRENT.toByte();
108 data[2] = subType.toByte();
111 data[4] = (byte) ((sensorId & 0xFF00) >> 8);
112 data[5] = (byte) (sensorId & 0x00FF);
115 data[7] = (byte) (((int) (channel1Amps * 10) >> 8) & 0xFF);
116 data[8] = (byte) ((int) (channel1Amps * 10) & 0xFF);
118 data[9] = (byte) (((int) (channel2Amps * 10) >> 8) & 0xFF);
119 data[10] = (byte) ((int) (channel2Amps * 10) & 0xFF);
121 data[11] = (byte) (((int) (channel3Amps * 10) >> 8) & 0xFF);
122 data[12] = (byte) ((int) (channel3Amps * 10) & 0xFF);
124 data[13] = (byte) (((signalLevel & 0x0F) << 4) | (batteryLevel & 0x0F));
130 public State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
131 throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
133 case CHANNEL_CHANNEL1_AMPS:
134 return new DecimalType(channel1Amps);
136 case CHANNEL_CHANNEL2_AMPS:
137 return new DecimalType(channel2Amps);
139 case CHANNEL_CHANNEL3_AMPS:
140 return new DecimalType(channel3Amps);
143 return super.convertToState(channelId, config, deviceState);
148 public String getDeviceId() {
149 return String.valueOf(sensorId);
153 public void setDeviceId(String deviceId) {
154 throw new UnsupportedOperationException();
158 public void convertFromState(String channelId, Type type) {
159 throw new UnsupportedOperationException();
163 public SubType convertSubType(String subType) throws RFXComUnsupportedValueException {
164 return ByteEnumUtil.convertSubType(SubType.class, subType);
168 public void setSubType(SubType subType) {
169 this.subType = subType;