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.CHANNEL_CHIME_SOUND;
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 chime messages.
31 * @author Mike Jagdis - Initial contribution
33 public class RFXComChimeMessage extends RFXComDeviceMessageImpl<RFXComChimeMessage.SubType> {
35 public enum SubType implements ByteEnumWrapper {
42 private final int subType;
44 SubType(int subType) {
45 this.subType = subType;
49 public byte toByte() {
50 return (byte) subType;
54 public SubType subType;
56 public int chimeSound;
58 public RFXComChimeMessage() {
59 super(PacketType.CHIME);
62 public RFXComChimeMessage(byte[] data) throws RFXComException {
67 public String toString() {
70 str += super.toString();
71 str += ", Sub type = " + subType;
72 str += ", Device Id = " + getDeviceId();
73 str += ", Chime Sound = " + chimeSound;
74 str += ", Signal level = " + signalLevel;
80 public void encodeMessage(byte[] data) throws RFXComException {
81 super.encodeMessage(data);
83 subType = fromByte(SubType.class, super.subType);
87 sensorId = (data[4] & 0xFF) << 8 | (data[5] & 0xFF);
94 sensorId = (data[4] & 0xFF) << 16 | (data[5] & 0xFF) << 8 | (data[6] & 0xFF);
99 signalLevel = (byte) ((data[7] & 0xF0) >> 4);
103 public byte[] decodeMessage() {
104 byte[] data = new byte[8];
107 data[1] = getPacketType().toByte();
108 data[2] = subType.toByte();
113 data[4] = (byte) ((sensorId & 0xFF00) >> 8);
114 data[5] = (byte) (sensorId & 0x00FF);
115 data[6] = (byte) chimeSound;
121 data[4] = (byte) ((sensorId & 0xFF0000) >> 16);
122 data[5] = (byte) ((sensorId & 0x00FF00) >> 8);
123 data[6] = (byte) ((sensorId & 0x0000FF));
127 data[7] = (byte) ((signalLevel & 0x0F) << 4);
133 public String getDeviceId() {
134 return String.valueOf(sensorId);
138 public State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
139 throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
140 if (CHANNEL_CHIME_SOUND.equals(channelId)) {
141 return new DecimalType(chimeSound);
143 return super.convertToState(channelId, config, deviceState);
148 public void setSubType(SubType subType) {
149 this.subType = subType;
153 public void setDeviceId(String sensorId) {
154 this.sensorId = Integer.parseInt(sensorId);
158 public void convertFromState(String channelId, Type type) throws RFXComUnsupportedChannelException {
159 if (CHANNEL_CHIME_SOUND.equals(channelId)) {
160 if (type instanceof DecimalType) {
161 chimeSound = ((DecimalType) type).intValue();
163 throw new RFXComUnsupportedChannelException("Channel " + channelId + " does not accept " + type);
166 throw new RFXComUnsupportedChannelException("Channel " + channelId + " is not relevant here");
171 public SubType convertSubType(String subType) throws RFXComUnsupportedValueException {
172 return ByteEnumUtil.convertSubType(SubType.class, subType);