]> git.basschouten.com Git - openhab-addons.git/blob
e6bcbb654181dd97e4b177a67e7dd621aeb93547
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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 import static org.openhab.binding.rfxcom.internal.messages.RFXComBaseMessage.PacketType.LIGHTING2;
18 import static org.openhab.binding.rfxcom.internal.messages.RFXComLighting2Message.Commands.*;
19
20 import java.math.BigDecimal;
21
22 import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
23 import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedChannelException;
24 import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedValueException;
25 import org.openhab.binding.rfxcom.internal.handler.DeviceState;
26 import org.openhab.core.library.types.IncreaseDecreaseType;
27 import org.openhab.core.library.types.OnOffType;
28 import org.openhab.core.library.types.OpenClosedType;
29 import org.openhab.core.library.types.PercentType;
30 import org.openhab.core.types.State;
31 import org.openhab.core.types.Type;
32
33 /**
34  * RFXCOM data class for lighting2 message.
35  *
36  * @author Pauli Anttila - Initial contribution
37  */
38 public class RFXComLighting2Message extends RFXComDeviceMessageImpl<RFXComLighting2Message.SubType> {
39     public enum SubType implements ByteEnumWrapper {
40         AC(0),
41         HOME_EASY_EU(1),
42         ANSLUT(2),
43         KAMBROOK(3);
44
45         private final int subType;
46
47         SubType(int subType) {
48             this.subType = subType;
49         }
50
51         @Override
52         public byte toByte() {
53             return (byte) subType;
54         }
55     }
56
57     public enum Commands implements ByteEnumWrapper {
58         OFF(0),
59         ON(1),
60         SET_LEVEL(2),
61         GROUP_OFF(3),
62         GROUP_ON(4),
63         SET_GROUP_LEVEL(5);
64
65         private final int command;
66
67         Commands(int command) {
68             this.command = command;
69         }
70
71         @Override
72         public byte toByte() {
73             return (byte) command;
74         }
75     }
76
77     public SubType subType;
78     public int sensorId;
79     public byte unitCode;
80     public Commands command;
81     public byte dimmingLevel;
82     public boolean group;
83
84     public RFXComLighting2Message() {
85         super(PacketType.LIGHTING2);
86     }
87
88     public RFXComLighting2Message(byte[] data) throws RFXComException {
89         encodeMessage(data);
90     }
91
92     @Override
93     public String toString() {
94         String str = "";
95
96         str += super.toString();
97         str += ", Sub type = " + subType;
98         str += ", Device Id = " + getDeviceId();
99         str += ", Command = " + command;
100         str += ", Dim level = " + dimmingLevel;
101         str += ", Signal level = " + signalLevel;
102
103         return str;
104     }
105
106     @Override
107     public void encodeMessage(byte[] data) throws RFXComException {
108         super.encodeMessage(data);
109
110         subType = fromByte(SubType.class, super.subType);
111         sensorId = (data[4] & 0xFF) << 24 | (data[5] & 0xFF) << 16 | (data[6] & 0xFF) << 8 | (data[7] & 0xFF);
112         command = fromByte(Commands.class, data[9]);
113
114         if ((command == Commands.GROUP_ON) || (command == Commands.GROUP_OFF)) {
115             unitCode = 0;
116         } else {
117             unitCode = data[8];
118         }
119
120         dimmingLevel = data[10];
121         signalLevel = (byte) ((data[11] & 0xF0) >> 4);
122     }
123
124     @Override
125     public byte[] decodeMessage() {
126         byte[] data = new byte[12];
127
128         data[0] = 0x0B;
129         data[1] = LIGHTING2.toByte();
130         data[2] = subType.toByte();
131         data[3] = seqNbr;
132         data[4] = (byte) ((sensorId >> 24) & 0xFF);
133         data[5] = (byte) ((sensorId >> 16) & 0xFF);
134         data[6] = (byte) ((sensorId >> 8) & 0xFF);
135         data[7] = (byte) (sensorId & 0xFF);
136
137         data[8] = unitCode;
138         data[9] = command.toByte();
139         data[10] = dimmingLevel;
140         data[11] = (byte) ((signalLevel & 0x0F) << 4);
141
142         return data;
143     }
144
145     @Override
146     public String getDeviceId() {
147         return sensorId + ID_DELIMITER + unitCode;
148     }
149
150     /**
151      * Convert a 0-15 scale value to a percent type.
152      *
153      * @param pt percent type to convert
154      * @return converted value 0-15
155      */
156     public static int getDimLevelFromPercentType(PercentType pt) {
157         return pt.toBigDecimal().multiply(BigDecimal.valueOf(15))
158                 .divide(PercentType.HUNDRED.toBigDecimal(), 0, BigDecimal.ROUND_UP).intValue();
159     }
160
161     /**
162      * Convert a 0-15 scale value to a percent type.
163      *
164      * @param value percent type to convert
165      * @return converted value 0-15
166      */
167     public static PercentType getPercentTypeFromDimLevel(int value) {
168         value = Math.min(value, 15);
169
170         return new PercentType(BigDecimal.valueOf(value).multiply(BigDecimal.valueOf(100))
171                 .divide(BigDecimal.valueOf(15), 0, BigDecimal.ROUND_UP).intValue());
172     }
173
174     @Override
175     public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
176         switch (channelId) {
177             case CHANNEL_DIMMING_LEVEL:
178                 return RFXComLighting2Message.getPercentTypeFromDimLevel(dimmingLevel);
179
180             case CHANNEL_COMMAND:
181                 switch (command) {
182                     case OFF:
183                     case GROUP_OFF:
184                         return OnOffType.OFF;
185
186                     case ON:
187                     case GROUP_ON:
188                         return OnOffType.ON;
189
190                     case SET_GROUP_LEVEL:
191                     case SET_LEVEL:
192                     default:
193                         throw new RFXComUnsupportedChannelException("Can't convert " + command + " for " + channelId);
194                 }
195
196             case CHANNEL_CONTACT:
197                 switch (command) {
198                     case OFF:
199                     case GROUP_OFF:
200                         return OpenClosedType.CLOSED;
201
202                     case ON:
203                     case GROUP_ON:
204                         return OpenClosedType.OPEN;
205
206                     case SET_GROUP_LEVEL:
207                     case SET_LEVEL:
208                     default:
209                         throw new RFXComUnsupportedChannelException("Can't convert " + command + " for " + channelId);
210                 }
211
212             default:
213                 return super.convertToState(channelId, deviceState);
214         }
215     }
216
217     @Override
218     public void setSubType(SubType subType) {
219         this.subType = subType;
220     }
221
222     @Override
223     public void setDeviceId(String deviceId) throws RFXComException {
224         String[] ids = deviceId.split("\\" + ID_DELIMITER);
225         if (ids.length != 2) {
226             throw new RFXComException("Invalid device id '" + deviceId + "'");
227         }
228
229         sensorId = Integer.parseInt(ids[0]);
230
231         // Get unitcode, 0 means group
232         unitCode = Byte.parseByte(ids[1]);
233         if (unitCode == 0) {
234             unitCode = 1;
235             group = true;
236         }
237     }
238
239     @Override
240     public void convertFromState(String channelId, Type type) throws RFXComUnsupportedChannelException {
241         switch (channelId) {
242             case CHANNEL_COMMAND:
243                 if (type instanceof OnOffType) {
244                     if (group) {
245                         command = (type == OnOffType.ON ? GROUP_ON : GROUP_OFF);
246
247                     } else {
248                         command = (type == OnOffType.ON ? Commands.ON : Commands.OFF);
249                     }
250
251                     dimmingLevel = 0;
252                 } else {
253                     throw new RFXComUnsupportedChannelException("Channel " + channelId + " does not accept " + type);
254                 }
255                 break;
256
257             case CHANNEL_DIMMING_LEVEL:
258                 if (type instanceof OnOffType) {
259                     command = (type == OnOffType.ON ? Commands.ON : Commands.OFF);
260                     dimmingLevel = 0;
261
262                 } else if (type instanceof PercentType) {
263                     command = Commands.SET_LEVEL;
264                     dimmingLevel = (byte) getDimLevelFromPercentType((PercentType) type);
265
266                     if (dimmingLevel == 0) {
267                         command = Commands.OFF;
268                     }
269                 } else if (type instanceof IncreaseDecreaseType) {
270                     command = Commands.SET_LEVEL;
271                     // Evert: I do not know how to get previous object state...
272                     dimmingLevel = 5;
273
274                 } else {
275                     throw new RFXComUnsupportedChannelException("Channel " + channelId + " does not accept " + type);
276                 }
277                 break;
278
279             default:
280                 throw new RFXComUnsupportedChannelException("Channel " + channelId + " is not relevant here");
281         }
282     }
283
284     @Override
285     public SubType convertSubType(String subType) throws RFXComUnsupportedValueException {
286         return ByteEnumUtil.convertSubType(SubType.class, subType);
287     }
288 }