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