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.CHANNEL_CHIME_SOUND;
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 chime messages.
29 * @author Mike Jagdis - Initial contribution
31 public class RFXComChimeMessage extends RFXComDeviceMessageImpl<RFXComChimeMessage.SubType> {
33 public enum SubType implements ByteEnumWrapper {
40 private final int subType;
42 SubType(int subType) {
43 this.subType = subType;
47 public byte toByte() {
48 return (byte) subType;
52 public SubType subType;
54 public int chimeSound;
56 public RFXComChimeMessage() {
57 super(PacketType.CHIME);
60 public RFXComChimeMessage(byte[] data) throws RFXComException {
65 public String toString() {
68 str += super.toString();
69 str += ", Sub type = " + subType;
70 str += ", Device Id = " + getDeviceId();
71 str += ", Chime Sound = " + chimeSound;
72 str += ", Signal level = " + signalLevel;
78 public void encodeMessage(byte[] data) throws RFXComException {
79 super.encodeMessage(data);
81 subType = fromByte(SubType.class, super.subType);
85 sensorId = (data[4] & 0xFF) << 8 | (data[5] & 0xFF);
92 sensorId = (data[4] & 0xFF) << 16 | (data[5] & 0xFF) << 8 | (data[6] & 0xFF);
97 signalLevel = (byte) ((data[7] & 0xF0) >> 4);
101 public byte[] decodeMessage() {
102 byte[] data = new byte[8];
105 data[1] = getPacketType().toByte();
106 data[2] = subType.toByte();
111 data[4] = (byte) ((sensorId & 0xFF00) >> 8);
112 data[5] = (byte) (sensorId & 0x00FF);
113 data[6] = (byte) chimeSound;
119 data[4] = (byte) ((sensorId & 0xFF0000) >> 16);
120 data[5] = (byte) ((sensorId & 0x00FF00) >> 8);
121 data[6] = (byte) ((sensorId & 0x0000FF));
125 data[7] = (byte) ((signalLevel & 0x0F) << 4);
131 public String getDeviceId() {
132 return String.valueOf(sensorId);
136 public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
137 if (CHANNEL_CHIME_SOUND.equals(channelId)) {
138 return new DecimalType(chimeSound);
140 return super.convertToState(channelId, deviceState);
145 public void setSubType(SubType subType) {
146 this.subType = subType;
150 public void setDeviceId(String sensorId) {
151 this.sensorId = Integer.parseInt(sensorId);
155 public void convertFromState(String channelId, Type type) throws RFXComUnsupportedChannelException {
156 if (CHANNEL_CHIME_SOUND.equals(channelId)) {
157 if (type instanceof DecimalType) {
158 chimeSound = ((DecimalType) type).intValue();
160 throw new RFXComUnsupportedChannelException("Channel " + channelId + " does not accept " + type);
163 throw new RFXComUnsupportedChannelException("Channel " + channelId + " is not relevant here");
168 public SubType convertSubType(String subType) throws RFXComUnsupportedValueException {
169 return ByteEnumUtil.convertSubType(SubType.class, subType);