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