]> git.basschouten.com Git - openhab-addons.git/blob
75da2f7fc27cc96fc010205ec4e99cd1537f6a4e
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.CHANNEL_CHIME_SOUND;
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 chime messages.
30  *
31  * @author Mike Jagdis - Initial contribution
32  */
33 public class RFXComChimeMessage extends RFXComDeviceMessageImpl<RFXComChimeMessage.SubType> {
34
35     public enum SubType implements ByteEnumWrapper {
36         BYRONSX(0),
37         BYRONMP001(1),
38         SELECTPLUS(2),
39         SELECTPLUS3(3),
40         ENVIVO(4),
41         ALFAWISE_DBELL(5);
42
43         private final int subType;
44
45         SubType(int subType) {
46             this.subType = subType;
47         }
48
49         @Override
50         public byte toByte() {
51             return (byte) subType;
52         }
53     }
54
55     public SubType subType;
56     public int sensorId;
57     public int chimeSound;
58
59     public RFXComChimeMessage() {
60         super(PacketType.CHIME);
61     }
62
63     public RFXComChimeMessage(byte[] data) throws RFXComException {
64         encodeMessage(data);
65     }
66
67     @Override
68     public String toString() {
69         String str = "";
70
71         str += super.toString();
72         str += ", Sub type = " + subType;
73         str += ", Device Id = " + getDeviceId();
74         str += ", Chime Sound = " + chimeSound;
75         str += ", Signal level = " + signalLevel;
76
77         return str;
78     }
79
80     @Override
81     public void encodeMessage(byte[] data) throws RFXComException {
82         super.encodeMessage(data);
83
84         subType = fromByte(SubType.class, super.subType);
85
86         switch (subType) {
87             case BYRONSX:
88                 sensorId = (data[4] & 0xFF) << 8 | (data[5] & 0xFF);
89                 chimeSound = data[6];
90                 break;
91             case BYRONMP001:
92             case SELECTPLUS:
93             case SELECTPLUS3:
94             case ENVIVO:
95             case ALFAWISE_DBELL:
96                 sensorId = (data[4] & 0xFF) << 16 | (data[5] & 0xFF) << 8 | (data[6] & 0xFF);
97                 chimeSound = 1;
98                 break;
99         }
100
101         signalLevel = (byte) ((data[7] & 0xF0) >> 4);
102     }
103
104     @Override
105     public byte[] decodeMessage() {
106         byte[] data = new byte[8];
107
108         data[0] = 0x07;
109         data[1] = getPacketType().toByte();
110         data[2] = subType.toByte();
111         data[3] = seqNbr;
112
113         switch (subType) {
114             case BYRONSX:
115                 data[4] = (byte) ((sensorId & 0xFF00) >> 8);
116                 data[5] = (byte) (sensorId & 0x00FF);
117                 data[6] = (byte) chimeSound;
118                 break;
119             case BYRONMP001:
120             case SELECTPLUS:
121             case SELECTPLUS3:
122             case ENVIVO:
123             case ALFAWISE_DBELL:
124                 data[4] = (byte) ((sensorId & 0xFF0000) >> 16);
125                 data[5] = (byte) ((sensorId & 0x00FF00) >> 8);
126                 data[6] = (byte) ((sensorId & 0x0000FF));
127                 break;
128         }
129
130         data[7] = (byte) ((signalLevel & 0x0F) << 4);
131
132         return data;
133     }
134
135     @Override
136     public String getDeviceId() {
137         return String.valueOf(sensorId);
138     }
139
140     @Override
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);
145         } else {
146             return super.convertToState(channelId, config, deviceState);
147         }
148     }
149
150     @Override
151     public void setSubType(SubType subType) {
152         this.subType = subType;
153     }
154
155     @Override
156     public void setDeviceId(String sensorId) {
157         this.sensorId = Integer.parseInt(sensorId);
158     }
159
160     @Override
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();
165             } else {
166                 throw new RFXComUnsupportedChannelException("Channel " + channelId + " does not accept " + type);
167             }
168         } else {
169             throw new RFXComUnsupportedChannelException("Channel " + channelId + " is not relevant here");
170         }
171     }
172
173     @Override
174     public SubType convertSubType(String subType) throws RFXComUnsupportedValueException {
175         return ByteEnumUtil.convertSubType(SubType.class, subType);
176     }
177 }