2 * Copyright (c) 2010-2020 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;
27 * RFXCOM data class for current message.
29 * @author Ben Jones - Initial contribution
30 * @author Pauli Anttila - for the Similar RFXComEnergyMessage code
31 * @author Jordan Cook - Added support for CURRENT devices, such as OWL CM113
32 * @author Martin van Wingerden - Updated CurrentMessage code to new style
34 public class RFXComCurrentMessage extends RFXComBatteryDeviceMessage<RFXComCurrentMessage.SubType> {
36 public enum SubType implements ByteEnumWrapper {
39 private final int subType;
41 SubType(int subType) {
42 this.subType = subType;
46 public byte toByte() {
47 return (byte) subType;
51 public SubType subType;
54 public double channel1Amps;
55 public double channel2Amps;
56 public double channel3Amps;
58 public RFXComCurrentMessage() {
59 super(PacketType.CURRENT);
62 public RFXComCurrentMessage(byte[] data) throws RFXComException {
67 public String toString() {
70 str += super.toString();
71 str += ", Sub type = " + subType;
72 str += ", Device Id = " + sensorId;
73 str += ", Count = " + count;
74 str += ", Channel 1 Amps = " + channel1Amps;
75 str += ", Channel 2 Amps = " + channel2Amps;
76 str += ", Channel 3 Amps = " + channel3Amps;
77 str += ", Signal level = " + signalLevel;
78 str += ", Battery level = " + batteryLevel;
84 public void encodeMessage(byte[] data) throws RFXComException {
85 super.encodeMessage(data);
87 subType = fromByte(SubType.class, super.subType);
89 sensorId = (data[4] & 0xFF) << 8 | (data[5] & 0xFF);
92 channel1Amps = ((data[7] & 0xFF) << 8 | (data[8] & 0xFF)) / 10.0;
93 channel2Amps = ((data[9] & 0xFF) << 8 | (data[10] & 0xFF)) / 10.0;
94 channel3Amps = ((data[11] & 0xFF) << 8 | (data[12] & 0xFF)) / 10.0;
96 signalLevel = (byte) ((data[13] & 0xF0) >> 4);
97 batteryLevel = (byte) (data[13] & 0x0F);
101 public byte[] decodeMessage() {
102 byte[] data = new byte[14];
105 data[1] = PacketType.CURRENT.toByte();
106 data[2] = subType.toByte();
109 data[4] = (byte) ((sensorId & 0xFF00) >> 8);
110 data[5] = (byte) (sensorId & 0x00FF);
113 data[7] = (byte) (((int) (channel1Amps * 10) >> 8) & 0xFF);
114 data[8] = (byte) ((int) (channel1Amps * 10) & 0xFF);
116 data[9] = (byte) (((int) (channel2Amps * 10) >> 8) & 0xFF);
117 data[10] = (byte) ((int) (channel2Amps * 10) & 0xFF);
119 data[11] = (byte) (((int) (channel3Amps * 10) >> 8) & 0xFF);
120 data[12] = (byte) ((int) (channel3Amps * 10) & 0xFF);
122 data[13] = (byte) (((signalLevel & 0x0F) << 4) | (batteryLevel & 0x0F));
128 public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
130 case CHANNEL_CHANNEL1_AMPS:
131 return new DecimalType(channel1Amps);
133 case CHANNEL_CHANNEL2_AMPS:
134 return new DecimalType(channel2Amps);
136 case CHANNEL_CHANNEL3_AMPS:
137 return new DecimalType(channel3Amps);
140 return super.convertToState(channelId, deviceState);
145 public String getDeviceId() {
146 return String.valueOf(sensorId);
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);
165 public void setSubType(SubType subType) {
166 this.subType = subType;