2 * Copyright (c) 2010-2023 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 {
43 private final int subType;
45 SubType(int subType) {
46 this.subType = subType;
50 public byte toByte() {
51 return (byte) subType;
55 public SubType subType;
57 public int chimeSound;
59 public RFXComChimeMessage() {
60 super(PacketType.CHIME);
63 public RFXComChimeMessage(byte[] data) throws RFXComException {
68 public String toString() {
71 str += super.toString();
72 str += ", Sub type = " + subType;
73 str += ", Device Id = " + getDeviceId();
74 str += ", Chime Sound = " + chimeSound;
75 str += ", Signal level = " + signalLevel;
81 public void encodeMessage(byte[] data) throws RFXComException {
82 super.encodeMessage(data);
84 subType = fromByte(SubType.class, super.subType);
88 sensorId = (data[4] & 0xFF) << 8 | (data[5] & 0xFF);
96 sensorId = (data[4] & 0xFF) << 16 | (data[5] & 0xFF) << 8 | (data[6] & 0xFF);
101 signalLevel = (byte) ((data[7] & 0xF0) >> 4);
105 public byte[] decodeMessage() {
106 byte[] data = new byte[8];
109 data[1] = getPacketType().toByte();
110 data[2] = subType.toByte();
115 data[4] = (byte) ((sensorId & 0xFF00) >> 8);
116 data[5] = (byte) (sensorId & 0x00FF);
117 data[6] = (byte) chimeSound;
124 data[4] = (byte) ((sensorId & 0xFF0000) >> 16);
125 data[5] = (byte) ((sensorId & 0x00FF00) >> 8);
126 data[6] = (byte) ((sensorId & 0x0000FF));
130 data[7] = (byte) ((signalLevel & 0x0F) << 4);
136 public String getDeviceId() {
137 return String.valueOf(sensorId);
141 public State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
142 throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
143 if (CHANNEL_CHIME_SOUND.equals(channelId)) {
144 return new DecimalType(chimeSound);
146 return super.convertToState(channelId, config, deviceState);
151 public void setSubType(SubType subType) {
152 this.subType = subType;
156 public void setDeviceId(String sensorId) {
157 this.sensorId = Integer.parseInt(sensorId);
161 public void convertFromState(String channelId, Type type) throws RFXComUnsupportedChannelException {
162 if (CHANNEL_CHIME_SOUND.equals(channelId)) {
163 if (type instanceof DecimalType decimalCommand) {
164 chimeSound = decimalCommand.intValue();
166 throw new RFXComUnsupportedChannelException("Channel " + channelId + " does not accept " + type);
169 throw new RFXComUnsupportedChannelException("Channel " + channelId + " is not relevant here");
174 public SubType convertSubType(String subType) throws RFXComUnsupportedValueException {
175 return ByteEnumUtil.convertSubType(SubType.class, subType);