]> git.basschouten.com Git - openhab-addons.git/blob
a4184fd3a7ad3f228b47a92105c4ab55c31e56b0
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.rfxcom.internal.messages;
14
15 import static org.openhab.binding.rfxcom.internal.RFXComBindingConstants.*;
16 import static org.openhab.binding.rfxcom.internal.messages.ByteEnumUtil.fromByte;
17
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
26 /**
27  * RFXCOM data class for current message.
28  *
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
33  */
34 public class RFXComCurrentMessage extends RFXComBatteryDeviceMessage<RFXComCurrentMessage.SubType> {
35
36     public enum SubType implements ByteEnumWrapper {
37         ELEC1(1);
38
39         private final int subType;
40
41         SubType(int subType) {
42             this.subType = subType;
43         }
44
45         @Override
46         public byte toByte() {
47             return (byte) subType;
48         }
49     }
50
51     public SubType subType;
52     public int sensorId;
53     public byte count;
54     public double channel1Amps;
55     public double channel2Amps;
56     public double channel3Amps;
57
58     public RFXComCurrentMessage() {
59         super(PacketType.CURRENT);
60     }
61
62     public RFXComCurrentMessage(byte[] data) throws RFXComException {
63         encodeMessage(data);
64     }
65
66     @Override
67     public String toString() {
68         String str = "";
69
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;
79
80         return str;
81     }
82
83     @Override
84     public void encodeMessage(byte[] data) throws RFXComException {
85         super.encodeMessage(data);
86
87         subType = fromByte(SubType.class, super.subType);
88
89         sensorId = (data[4] & 0xFF) << 8 | (data[5] & 0xFF);
90         count = data[6];
91
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;
95
96         signalLevel = (byte) ((data[13] & 0xF0) >> 4);
97         batteryLevel = (byte) (data[13] & 0x0F);
98     }
99
100     @Override
101     public byte[] decodeMessage() {
102         byte[] data = new byte[14];
103
104         data[0] = 0x0D;
105         data[1] = PacketType.CURRENT.toByte();
106         data[2] = subType.toByte();
107         data[3] = seqNbr;
108
109         data[4] = (byte) ((sensorId & 0xFF00) >> 8);
110         data[5] = (byte) (sensorId & 0x00FF);
111         data[6] = count;
112
113         data[7] = (byte) (((int) (channel1Amps * 10) >> 8) & 0xFF);
114         data[8] = (byte) ((int) (channel1Amps * 10) & 0xFF);
115
116         data[9] = (byte) (((int) (channel2Amps * 10) >> 8) & 0xFF);
117         data[10] = (byte) ((int) (channel2Amps * 10) & 0xFF);
118
119         data[11] = (byte) (((int) (channel3Amps * 10) >> 8) & 0xFF);
120         data[12] = (byte) ((int) (channel3Amps * 10) & 0xFF);
121
122         data[13] = (byte) (((signalLevel & 0x0F) << 4) | (batteryLevel & 0x0F));
123
124         return data;
125     }
126
127     @Override
128     public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
129         switch (channelId) {
130             case CHANNEL_CHANNEL1_AMPS:
131                 return new DecimalType(channel1Amps);
132
133             case CHANNEL_CHANNEL2_AMPS:
134                 return new DecimalType(channel2Amps);
135
136             case CHANNEL_CHANNEL3_AMPS:
137                 return new DecimalType(channel3Amps);
138
139             default:
140                 return super.convertToState(channelId, deviceState);
141         }
142     }
143
144     @Override
145     public String getDeviceId() {
146         return String.valueOf(sensorId);
147     }
148
149     @Override
150     public void setDeviceId(String deviceId) {
151         throw new UnsupportedOperationException();
152     }
153
154     @Override
155     public void convertFromState(String channelId, Type type) {
156         throw new UnsupportedOperationException();
157     }
158
159     @Override
160     public SubType convertSubType(String subType) throws RFXComUnsupportedValueException {
161         return ByteEnumUtil.convertSubType(SubType.class, subType);
162     }
163
164     @Override
165     public void setSubType(SubType subType) {
166         this.subType = subType;
167     }
168 }