]> git.basschouten.com Git - openhab-addons.git/blob
ae35088023646d662b423782a40ff92026d6987f
[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.OnOffType;
23 import org.openhab.core.library.types.OpenClosedType;
24 import org.openhab.core.types.State;
25 import org.openhab.core.types.Type;
26
27 /**
28  * RFXCOM data class for lighting6 message. See Blyss.
29  *
30  * @author Damien Servant - Initial contribution
31  * @author Pauli Anttila - Migrated for OH2
32  */
33 public class RFXComLighting6Message extends RFXComDeviceMessageImpl<RFXComLighting6Message.SubType> {
34
35     public enum SubType implements ByteEnumWrapper {
36         BLYSS(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         ON(0),
52         OFF(1),
53         GROUP_ON(2),
54         GROUP_OFF(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 sensorId;
70     public char groupCode;
71     public byte unitCode;
72     public Commands command;
73
74     public RFXComLighting6Message() {
75         super(PacketType.LIGHTING6);
76     }
77
78     public RFXComLighting6Message(byte[] data) throws RFXComException {
79         encodeMessage(data);
80     }
81
82     @Override
83     public String toString() {
84         String str = "";
85
86         str += super.toString();
87         str += ", Sub type = " + subType;
88         str += ", Device Id = " + getDeviceId();
89         str += ", Command = " + command;
90         str += ", Signal level = " + signalLevel;
91
92         return str;
93     }
94
95     @Override
96     public void encodeMessage(byte[] data) throws RFXComException {
97         super.encodeMessage(data);
98
99         subType = fromByte(SubType.class, super.subType);
100         sensorId = (data[4] & 0xFF) << 8 | (data[5] & 0xFF);
101         groupCode = (char) data[6];
102         unitCode = data[7];
103         command = fromByte(Commands.class, data[8]);
104
105         signalLevel = (byte) ((data[11] & 0xF0) >> 4);
106     }
107
108     @Override
109     public byte[] decodeMessage() {
110         // Example data 0B 15 00 02 01 01 41 01 00 04 8E 00
111         // 0B 15 00 02 01 01 41 01 01 04 8E 00
112
113         byte[] data = new byte[12];
114
115         data[0] = 0x0B;
116         data[1] = RFXComBaseMessage.PacketType.LIGHTING6.toByte();
117         data[2] = subType.toByte();
118         data[3] = seqNbr;
119         data[4] = (byte) ((sensorId >> 8) & 0xFF);
120         data[5] = (byte) (sensorId & 0xFF);
121         data[6] = (byte) groupCode;
122         data[7] = unitCode;
123         data[8] = command.toByte();
124         data[9] = 0x00; // CmdSeqNbr1 - 0 to 4 - Useless for a Blyss Switch
125         data[10] = 0x00; // CmdSeqNbr2 - 0 to 145 - Useless for a Blyss Switch
126         data[11] = (byte) ((signalLevel & 0x0F) << 4);
127
128         return data;
129     }
130
131     @Override
132     public String getDeviceId() {
133         return sensorId + ID_DELIMITER + groupCode + 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                 switch (command) {
141                     case OFF:
142                     case GROUP_OFF:
143                         return OnOffType.OFF;
144
145                     case ON:
146                     case GROUP_ON:
147                         return OnOffType.ON;
148
149                     default:
150                         throw new RFXComUnsupportedChannelException("Can't convert " + command + " for " + channelId);
151                 }
152
153             case CHANNEL_CONTACT:
154                 switch (command) {
155                     case OFF:
156                     case GROUP_OFF:
157                         return OpenClosedType.CLOSED;
158
159                     case ON:
160                     case GROUP_ON:
161                         return OpenClosedType.OPEN;
162
163                     default:
164                         throw new RFXComUnsupportedChannelException("Can't convert " + command + " for " + channelId);
165                 }
166
167             default:
168                 return super.convertToState(channelId, deviceState);
169         }
170     }
171
172     @Override
173     public void setSubType(SubType subType) {
174         this.subType = subType;
175     }
176
177     @Override
178     public void setDeviceId(String deviceId) throws RFXComException {
179         String[] ids = deviceId.split("\\" + ID_DELIMITER);
180         if (ids.length != 3) {
181             throw new RFXComException("Invalid device id '" + deviceId + "'");
182         }
183
184         sensorId = Integer.parseInt(ids[0]);
185         groupCode = ids[1].charAt(0);
186         unitCode = Byte.parseByte(ids[2]);
187     }
188
189     @Override
190     public void convertFromState(String channelId, Type type) throws RFXComUnsupportedChannelException {
191         switch (channelId) {
192             case CHANNEL_COMMAND:
193                 if (type instanceof OnOffType) {
194                     command = (type == OnOffType.ON ? Commands.ON : Commands.OFF);
195
196                 } else {
197                     throw new RFXComUnsupportedChannelException("Channel " + channelId + " does not accept " + type);
198                 }
199                 break;
200
201             default:
202                 throw new RFXComUnsupportedChannelException("Channel " + channelId + " is not relevant here");
203         }
204     }
205
206     @Override
207     public SubType convertSubType(String subType) throws RFXComUnsupportedValueException {
208         return ByteEnumUtil.convertSubType(SubType.class, subType);
209     }
210 }