2 * Copyright (c) 2010-2022 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 and Energy message.
31 * @author Damien Servant - Initial contribution
33 public class RFXComCurrentEnergyMessage extends RFXComBatteryDeviceMessage<RFXComCurrentEnergyMessage.SubType> {
34 private static final float TOTAL_USAGE_CONVERSION_FACTOR = 223.666F;
36 public enum SubType implements ByteEnumWrapper {
37 ELEC4(1); // OWL - CM180i
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;
57 public double totalUsage;
59 public RFXComCurrentEnergyMessage() {
60 super(PacketType.CURRENT_ENERGY);
63 public RFXComCurrentEnergyMessage(byte[] data) throws RFXComException {
68 public String toString() {
71 str += super.toString();
72 str += ", Sub type = " + subType;
73 str += ", Id = " + sensorId;
74 str += ", Count = " + count;
75 str += ", Channel1 Amps = " + channel1Amps;
76 str += ", Channel2 Amps = " + channel2Amps;
77 str += ", Channel3 Amps = " + channel3Amps;
78 str += ", Total Usage = " + totalUsage;
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);
90 sensorId = (data[4] & 0xFF) << 8 | (data[5] & 0xFF);
93 // Current = Field / 10
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 totalUsage = ((long) (data[13] & 0xFF) << 40 | (long) (data[14] & 0xFF) << 32 | (data[15] & 0xFF) << 24
99 | (data[16] & 0xFF) << 16 | (data[17] & 0xFF) << 8 | (data[18] & 0xFF));
100 totalUsage = totalUsage / TOTAL_USAGE_CONVERSION_FACTOR;
102 signalLevel = (byte) ((data[19] & 0xF0) >> 4);
103 batteryLevel = (byte) (data[19] & 0x0F);
107 public byte[] decodeMessage() {
108 byte[] data = new byte[20];
110 data[0] = (byte) (data.length - 1);
111 data[1] = RFXComBaseMessage.PacketType.CURRENT_ENERGY.toByte();
112 data[2] = subType.toByte();
115 data[4] = (byte) ((sensorId & 0xFF00) >> 8);
116 data[5] = (byte) (sensorId & 0x00FF);
119 data[7] = (byte) (((int) (channel1Amps * 10.0) >> 8) & 0xFF);
120 data[8] = (byte) ((int) (channel1Amps * 10.0) & 0xFF);
121 data[9] = (byte) (((int) (channel2Amps * 10.0) >> 8) & 0xFF);
122 data[10] = (byte) ((int) (channel2Amps * 10.0) & 0xFF);
123 data[11] = (byte) (((int) (channel3Amps * 10.0) >> 8) & 0xFF);
124 data[12] = (byte) ((int) (channel3Amps * 10.0) & 0xFF);
126 long totalUsageLoc = (long) (totalUsage * TOTAL_USAGE_CONVERSION_FACTOR);
128 data[13] = (byte) ((totalUsageLoc >> 40) & 0xFF);
129 data[14] = (byte) ((totalUsageLoc >> 32) & 0xFF);
130 data[15] = (byte) ((totalUsageLoc >> 24) & 0xFF);
131 data[16] = (byte) ((totalUsageLoc >> 16) & 0xFF);
132 data[17] = (byte) ((totalUsageLoc >> 8) & 0xFF);
133 data[18] = (byte) (totalUsageLoc & 0xFF);
135 data[19] = (byte) (((signalLevel & 0x0F) << 4) | (batteryLevel & 0x0F));
141 public State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
142 throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
144 case CHANNEL_CHANNEL1_AMPS:
145 return new DecimalType(channel1Amps);
147 case CHANNEL_CHANNEL2_AMPS:
148 return new DecimalType(channel2Amps);
150 case CHANNEL_CHANNEL3_AMPS:
151 return new DecimalType(channel3Amps);
153 case CHANNEL_TOTAL_USAGE:
154 return new DecimalType(totalUsage);
157 return super.convertToState(channelId, config, deviceState);
162 public String getDeviceId() {
163 return String.valueOf(sensorId);
167 public void setDeviceId(String deviceId) {
168 throw new UnsupportedOperationException();
172 public void convertFromState(String channelId, Type type) {
173 throw new UnsupportedOperationException();
177 public SubType convertSubType(String subType) throws RFXComUnsupportedValueException {
178 return ByteEnumUtil.convertSubType(SubType.class, subType);
182 public void setSubType(SubType subType) {
183 this.subType = subType;