]> git.basschouten.com Git - openhab-addons.git/blob
a5e289f1468ee08e80ae97e28bc70db5a9965c12
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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_COMMAND;
16 import static org.openhab.binding.rfxcom.internal.messages.ByteEnumUtil.fromByte;
17
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.OnOffType;
23 import org.openhab.core.types.State;
24 import org.openhab.core.types.Type;
25
26 /**
27  * RFXCOM data class for HomeConfort message.
28  *
29  * @author Mike Jagdis - Initial contribution
30  */
31 public class RFXComHomeConfortMessage extends RFXComDeviceMessageImpl<RFXComHomeConfortMessage.SubType> {
32
33     public enum SubType implements ByteEnumWrapper {
34         TEL_010(0);
35
36         private final int subType;
37
38         SubType(int subType) {
39             this.subType = subType;
40         }
41
42         @Override
43         public byte toByte() {
44             return (byte) subType;
45         }
46     }
47
48     public enum Commands implements ByteEnumWrapper {
49         OFF(0),
50         ON(1),
51         GROUP_OFF(2),
52         GROUP_ON(3);
53
54         private final int command;
55
56         Commands(int command) {
57             this.command = command;
58         }
59
60         @Override
61         public byte toByte() {
62             return (byte) command;
63         }
64     }
65
66     public SubType subType;
67     public int deviceId;
68     public char houseCode;
69     public byte unitCode;
70     public Commands command;
71
72     public RFXComHomeConfortMessage() {
73         super(PacketType.HOME_CONFORT);
74     }
75
76     public RFXComHomeConfortMessage(byte[] data) throws RFXComException {
77         encodeMessage(data);
78     }
79
80     @Override
81     public String toString() {
82         return super.toString() + ", Sub type = " + subType + ", Device Id = " + getDeviceId() + ", Command = "
83                 + command + ", Signal level = " + signalLevel;
84     }
85
86     @Override
87     public void encodeMessage(byte[] data) throws RFXComException {
88         super.encodeMessage(data);
89
90         subType = fromByte(SubType.class, super.subType);
91         deviceId = (((data[4] << 8) | data[5]) << 8) | data[6];
92         houseCode = (char) data[7];
93         unitCode = data[8];
94         command = fromByte(Commands.class, data[9]);
95         if (command == Commands.GROUP_ON || command == Commands.GROUP_OFF) {
96             unitCode = 0;
97         }
98         signalLevel = (byte) ((data[12] & 0xF0) >> 4);
99     }
100
101     @Override
102     public byte[] decodeMessage() {
103         byte[] data = new byte[13];
104
105         data[0] = 0x0C;
106         data[1] = PacketType.HOME_CONFORT.toByte();
107         data[2] = subType.toByte();
108         data[3] = seqNbr;
109         data[4] = (byte) ((deviceId >> 16) & 0xff);
110         data[5] = (byte) ((deviceId >> 8) & 0xff);
111         data[6] = (byte) (deviceId & 0xff);
112         data[7] = (byte) houseCode;
113         data[8] = unitCode;
114         data[9] = command.toByte();
115         data[10] = 0;
116         data[11] = 0;
117         data[12] = (byte) ((signalLevel & 0x0F) << 4);
118
119         return data;
120     }
121
122     @Override
123     public String getDeviceId() {
124         return deviceId + ID_DELIMITER + houseCode + ID_DELIMITER + unitCode;
125     }
126
127     @Override
128     public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
129         if (channelId.equals(CHANNEL_COMMAND)) {
130             return (command == Commands.OFF || command == Commands.GROUP_OFF ? OnOffType.OFF : OnOffType.ON);
131         } else {
132             return super.convertToState(channelId, deviceState);
133         }
134     }
135
136     @Override
137     public void setSubType(SubType subType) {
138         this.subType = subType;
139     }
140
141     @Override
142     public void setDeviceId(String deviceId) throws RFXComException {
143         String[] ids = deviceId.split("\\" + ID_DELIMITER);
144         if (ids.length != 3) {
145             throw new RFXComException("Invalid device id '" + deviceId + "'");
146         }
147
148         this.deviceId = Integer.parseInt(ids[0]);
149         houseCode = ids[1].charAt(0);
150         unitCode = Byte.parseByte(ids[2]);
151     }
152
153     @Override
154     public void convertFromState(String channelId, Type type) throws RFXComUnsupportedChannelException {
155         if (CHANNEL_COMMAND.equals(channelId)) {
156             if (type instanceof OnOffType) {
157                 if (unitCode == 0) {
158                     command = (type == OnOffType.ON ? Commands.GROUP_ON : Commands.GROUP_OFF);
159
160                 } else {
161                     command = (type == OnOffType.ON ? Commands.ON : Commands.OFF);
162                 }
163             } else {
164                 throw new RFXComUnsupportedChannelException("Channel " + channelId + " does not accept " + type);
165             }
166
167         } else {
168             throw new RFXComUnsupportedChannelException("Channel " + channelId + " is not relevant here");
169         }
170     }
171
172     @Override
173     public SubType convertSubType(String subType) throws RFXComUnsupportedValueException {
174         return ByteEnumUtil.convertSubType(SubType.class, subType);
175     }
176 }