]> git.basschouten.com Git - openhab-addons.git/blob
d5cf2e19a971ac05f81e525c44fd65bf9d06e230
[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.IncreaseDecreaseType;
23 import org.openhab.core.library.types.OnOffType;
24 import org.openhab.core.library.types.OpenClosedType;
25 import org.openhab.core.library.types.StopMoveType;
26 import org.openhab.core.library.types.UpDownType;
27 import org.openhab.core.types.State;
28 import org.openhab.core.types.Type;
29
30 /**
31  * RFXCOM data class for RFY (Somfy RTS) message.
32  *
33  * @author Jürgen Richtsfeld - Initial contribution
34  * @author Pauli Anttila - Ported from OpenHAB1
35  * @author Mike Jagdis - Added venetian support and sun+wind detector
36  */
37 public class RFXComRfyMessage extends RFXComDeviceMessageImpl<RFXComRfyMessage.SubType> {
38
39     public enum Commands implements ByteEnumWrapper {
40         STOP(0x00),
41         UP(0x01),
42         DOWN(0x03),
43         PROGRAM(0x07),
44         UP_SHORT(0x0F),
45         DOWN_SHORT(0x10),
46         UP_LONG(0x11),
47         DOWN_LONG(0x12),
48         ENABLE_SUN_WIND_DETECTOR(0x13),
49         DISABLE_SUN_DETECTOR(0x14);
50
51         private final int command;
52
53         Commands(int command) {
54             this.command = command;
55         }
56
57         @Override
58         public byte toByte() {
59             return (byte) command;
60         }
61     }
62
63     public enum SubType implements ByteEnumWrapper {
64         RFY(0),
65         RFY_EXT(1),
66         RESERVED(2),
67         ASA(3);
68
69         private final int subType;
70
71         SubType(int subType) {
72             this.subType = subType;
73         }
74
75         @Override
76         public byte toByte() {
77             return (byte) subType;
78         }
79     }
80
81     public SubType subType;
82     public int unitId;
83     public byte unitCode;
84     public Commands command;
85
86     public RFXComRfyMessage() {
87         super(PacketType.RFY);
88     }
89
90     public RFXComRfyMessage(byte[] data) throws RFXComException {
91         encodeMessage(data);
92     }
93
94     @Override
95     public String toString() {
96         return super.toString() + ", Sub type = " + subType + ", Unit Id = " + getDeviceId() + ", Unit Code = "
97                 + unitCode + ", Command = " + command + ", Signal level = " + signalLevel;
98     }
99
100     @Override
101     public void encodeMessage(byte[] data) throws RFXComException {
102         super.encodeMessage(data);
103
104         subType = fromByte(SubType.class, super.subType);
105
106         unitId = (data[4] & 0xFF) << 16 | (data[5] & 0xFF) << 8 | (data[6] & 0xFF);
107         unitCode = data[7];
108
109         command = fromByte(Commands.class, data[8]);
110         signalLevel = (byte) ((data[12] & 0xF0) >> 4);
111     }
112
113     @Override
114     public byte[] decodeMessage() {
115         final byte[] data = new byte[13];
116
117         data[0] = 12;
118         data[1] = RFXComBaseMessage.PacketType.RFY.toByte();
119         data[2] = subType.toByte();
120         data[3] = seqNbr;
121         data[4] = (byte) ((unitId >> 16) & 0xFF);
122         data[5] = (byte) ((unitId >> 8) & 0xFF);
123         data[6] = (byte) (unitId & 0xFF);
124         data[7] = unitCode;
125         data[8] = command.toByte();
126         data[12] = (byte) ((signalLevel & 0x0F) << 4);
127
128         return data;
129     }
130
131     @Override
132     public String getDeviceId() {
133         return unitId + ID_DELIMITER + unitCode;
134     }
135
136     @Override
137     public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
138         switch (channelId) {
139             case CHANNEL_COMMAND:
140                 return (command == Commands.DOWN ? OpenClosedType.CLOSED : OpenClosedType.OPEN);
141
142             default:
143                 return super.convertToState(channelId, 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 deviceId) throws RFXComException {
154         String[] ids = deviceId.split("\\" + ID_DELIMITER);
155         if (ids.length != 2) {
156             throw new RFXComException("Invalid device id '" + deviceId + "'");
157         }
158
159         this.unitId = Integer.parseInt(ids[0]);
160         this.unitCode = Byte.parseByte(ids[1]);
161     }
162
163     @Override
164     public void convertFromState(String channelId, Type type) throws RFXComUnsupportedChannelException {
165         switch (channelId) {
166             case CHANNEL_SHUTTER:
167                 if (type instanceof OpenClosedType) {
168                     this.command = (type == OpenClosedType.CLOSED ? Commands.DOWN : Commands.UP);
169
170                 } else if (type instanceof UpDownType) {
171                     this.command = (type == UpDownType.DOWN ? Commands.DOWN : Commands.UP);
172
173                 } else if (type instanceof StopMoveType) {
174                     this.command = Commands.STOP;
175
176                 } else {
177                     throw new RFXComUnsupportedChannelException("Channel " + channelId + " does not accept " + type);
178                 }
179                 break;
180
181             case CHANNEL_PROGRAM:
182                 if (type == OnOffType.ON) {
183                     this.command = Commands.PROGRAM;
184                 } else {
185                     throw new RFXComUnsupportedChannelException("Can't convert " + type + " to Command");
186                 }
187                 break;
188
189             case CHANNEL_SUN_WIND_DETECTOR:
190                 if (type instanceof OnOffType) {
191                     this.command = (type == OnOffType.ON ? Commands.ENABLE_SUN_WIND_DETECTOR
192                             : Commands.DISABLE_SUN_DETECTOR);
193                 } else {
194                     throw new RFXComUnsupportedChannelException("Can't convert " + type + " to Command");
195                 }
196                 break;
197
198             case CHANNEL_VENETIAN_BLIND:
199                 if (type instanceof OpenClosedType) {
200                     this.command = (type == OpenClosedType.CLOSED ? Commands.DOWN_SHORT : Commands.UP_SHORT);
201
202                 } else if (type instanceof OnOffType) {
203                     this.command = (type == OnOffType.ON ? Commands.DOWN_SHORT : Commands.UP_SHORT);
204
205                 } else if (type instanceof IncreaseDecreaseType) {
206                     this.command = (type == IncreaseDecreaseType.INCREASE ? Commands.DOWN_LONG : Commands.UP_LONG);
207
208                 } else {
209                     throw new RFXComUnsupportedChannelException("Can't convert " + type + " to Command");
210                 }
211                 break;
212
213             default:
214                 throw new RFXComUnsupportedChannelException("Channel " + channelId + " is not relevant here");
215         }
216     }
217
218     @Override
219     public SubType convertSubType(String subType) throws RFXComUnsupportedValueException {
220         return ByteEnumUtil.convertSubType(SubType.class, subType);
221     }
222 }