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