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