]> git.basschouten.com Git - openhab-addons.git/blob
caa8db85392e46196cdddc4aa6a6b3e307f1fb7a
[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.*;
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.OpenClosedType;
23 import org.openhab.core.library.types.StopMoveType;
24 import org.openhab.core.library.types.UpDownType;
25 import org.openhab.core.types.State;
26 import org.openhab.core.types.Type;
27
28 /**
29  * RFXCOM data class for blinds1 message.
30  *
31  * @author Peter Janson / Pål Edman - Initial contribution
32  * @author Pauli Anttila - Migration to OH2
33  * @author Fabien Le Bars - Added support for Cherubini blinds
34  */
35 public class RFXComBlinds1Message extends RFXComBatteryDeviceMessage<RFXComBlinds1Message.SubType> {
36
37     public enum SubType implements ByteEnumWrapper {
38         T0(0), // Hasta new/RollerTrol
39         T1(1),
40         T2(2),
41         T3(3),
42         T4(4), // Additional commands.
43         T5(5), // MEDIA MOUNT have different direction commands than the rest!! Needs to be fixed.
44         T6(6),
45         T7(7),
46         T8(8), // Chamberlain CS4330
47         T9(9), // Sunpery/BTX
48         T10(10), // Dolat DLM-1, Topstar
49         T11(11), // ASP
50         T12(12), // Confexx CNF24-2435
51         T13(13), // Screenline
52         T18(18); // Cherubini
53
54         private final int subType;
55
56         SubType(int subType) {
57             this.subType = subType;
58         }
59
60         @Override
61         public byte toByte() {
62             return (byte) subType;
63         }
64     }
65
66     public enum Commands implements ByteEnumWrapper {
67         OPEN(0), // MediaMount DOWN(0),
68         CLOSE(1), // MediaMount UPP(1),
69         STOP(2),
70         CONFIRM(3),
71         SET_LIMIT(4), // YR1326 SET_UPPER_LIMIT(4),
72         SET_LOWER_LIMIT(5), // YR1326
73         DELETE_LIMITS(6), // YR1326
74         CHANGE_DIRECTON(7); // YR1326
75
76         private final int command;
77
78         Commands(int command) {
79             this.command = command;
80         }
81
82         @Override
83         public byte toByte() {
84             return (byte) command;
85         }
86     }
87
88     public SubType subType;
89     public int sensorId;
90     public byte unitCode;
91     public Commands command;
92
93     public RFXComBlinds1Message() {
94         super(PacketType.BLINDS1);
95     }
96
97     public RFXComBlinds1Message(byte[] data) throws RFXComException {
98         encodeMessage(data);
99     }
100
101     @Override
102     public String toString() {
103         String str = "";
104
105         str += super.toString();
106         str += ", Sub type = " + subType;
107         str += ", Device Id = " + getDeviceId();
108         str += ", Command = " + command;
109         str += ", Signal level = " + signalLevel;
110         str += ", Battery level = " + batteryLevel;
111
112         return str;
113     }
114
115     @Override
116     public void encodeMessage(byte[] data) throws RFXComException {
117         super.encodeMessage(data);
118
119         subType = fromByte(SubType.class, super.subType);
120
121         if (subType == SubType.T6) {
122             sensorId = (data[4] & 0xFF) << 20 | (data[5] & 0xFF) << 12 | (data[6] & 0xFF) << 4 | (data[7] & 0xF0) >> 4;
123             unitCode = (byte) (data[7] & 0x0F);
124         } else {
125             sensorId = (data[4] & 0xFF) << 16 | (data[5] & 0xFF) << 8 | (data[6] & 0xFF);
126             unitCode = data[7];
127         }
128
129         command = fromByte(Commands.class, data[8]);
130
131         signalLevel = (byte) ((data[9] & 0xF0) >> 4);
132         batteryLevel = (byte) (data[9] & 0x0F);
133     }
134
135     @Override
136     public byte[] decodeMessage() {
137         // Example data
138         // BLINDS1 09 19 00 06 00 B1 8F 01 00 70
139
140         byte[] data = new byte[10];
141
142         data[0] = 0x09;
143         data[1] = RFXComBaseMessage.PacketType.BLINDS1.toByte();
144         data[2] = subType.toByte();
145         data[3] = seqNbr;
146
147         if (subType == SubType.T6) {
148             data[4] = (byte) ((sensorId >>> 20) & 0xFF);
149             data[5] = (byte) ((sensorId >>> 12) & 0xFF);
150             data[6] = (byte) ((sensorId >>> 4) & 0xFF);
151             data[7] = (byte) (((sensorId & 0x0F) << 4) | (unitCode & 0x0F));
152         } else {
153             data[4] = (byte) ((sensorId >> 16) & 0xFF);
154             data[5] = (byte) ((sensorId >> 8) & 0xFF);
155             data[6] = (byte) (sensorId & 0xFF);
156             data[7] = unitCode;
157         }
158
159         data[8] = command.toByte();
160         data[9] = (byte) (((signalLevel & 0x0F) << 4) | (batteryLevel & 0x0F));
161
162         return data;
163     }
164
165     @Override
166     public String getDeviceId() {
167         return sensorId + ID_DELIMITER + unitCode;
168     }
169
170     @Override
171     public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
172         if (CHANNEL_COMMAND.equals(channelId)) {
173             return (command == Commands.CLOSE ? OpenClosedType.CLOSED : OpenClosedType.OPEN);
174         } else {
175             return super.convertToState(channelId, deviceState);
176         }
177     }
178
179     @Override
180     public void setSubType(SubType subType) {
181         this.subType = subType;
182     }
183
184     @Override
185     public void setDeviceId(String deviceId) throws RFXComException {
186         String[] ids = deviceId.split("\\" + ID_DELIMITER);
187         if (ids.length != 2) {
188             throw new RFXComException("Invalid device id '" + deviceId + "'");
189         }
190
191         sensorId = Integer.parseInt(ids[0]);
192         unitCode = Byte.parseByte(ids[1]);
193     }
194
195     @Override
196     public void convertFromState(String channelId, Type type) throws RFXComUnsupportedChannelException {
197         if (CHANNEL_SHUTTER.equals(channelId)) {
198             if (type instanceof OpenClosedType) {
199                 command = (type == OpenClosedType.CLOSED ? Commands.CLOSE : Commands.OPEN);
200             } else if (type instanceof UpDownType) {
201                 command = (type == UpDownType.UP ? Commands.OPEN : Commands.CLOSE);
202             } else if (type instanceof StopMoveType) {
203                 command = Commands.STOP;
204             } else {
205                 throw new RFXComUnsupportedChannelException("Channel " + channelId + " does not accept " + type);
206             }
207         } else {
208             throw new RFXComUnsupportedChannelException("Channel " + channelId + " is not relevant here");
209         }
210     }
211
212     @Override
213     public SubType convertSubType(String subType) throws RFXComUnsupportedValueException {
214         return ByteEnumUtil.convertSubType(SubType.class, subType);
215     }
216 }