]> git.basschouten.com Git - openhab-addons.git/blob
2ebe1760fe7a27a0a51531586816b9e0425453fb
[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.lifx.internal.dto;
14
15 import java.nio.ByteBuffer;
16
17 import org.openhab.binding.lifx.internal.fields.ByteField;
18 import org.openhab.binding.lifx.internal.fields.Field;
19 import org.openhab.binding.lifx.internal.fields.HSBK;
20 import org.openhab.binding.lifx.internal.fields.HSBKField;
21 import org.openhab.binding.lifx.internal.fields.UInt32Field;
22
23 /**
24  * @author Tim Buckley - Initial contribution
25  * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification
26  */
27 public class SetColorRequest extends Packet {
28
29     public static final int TYPE = 0x66;
30
31     public static final Field<ByteBuffer> FIELD_STREAM = new ByteField(1);
32     public static final HSBKField FIELD_COLOR = new HSBKField();
33     public static final Field<Long> FIELD_FADE_TIME = new UInt32Field().little();
34
35     private ByteBuffer stream;
36
37     private HSBK color;
38     private long fadeTime;
39
40     public ByteBuffer getStream() {
41         return stream;
42     }
43
44     public HSBK getColor() {
45         return color;
46     }
47
48     public long getFadeTime() {
49         return fadeTime;
50     }
51
52     public SetColorRequest() {
53         stream = ByteBuffer.allocate(1);
54         setTagged(false);
55         setAddressable(true);
56         setResponseRequired(true);
57     }
58
59     public SetColorRequest(HSBK color, long fadeTime) {
60         this();
61         this.color = color;
62         this.fadeTime = fadeTime;
63     }
64
65     @Override
66     public int packetType() {
67         return TYPE;
68     }
69
70     @Override
71     protected int packetLength() {
72         return 13;
73     }
74
75     @Override
76     protected void parsePacket(ByteBuffer bytes) {
77         stream = FIELD_STREAM.value(bytes);
78         color = FIELD_COLOR.value(bytes);
79         fadeTime = FIELD_FADE_TIME.value(bytes);
80     }
81
82     @Override
83     protected ByteBuffer packetBytes() {
84         return ByteBuffer.allocate(packetLength()).put(FIELD_STREAM.bytes(stream)).put(FIELD_COLOR.bytes(color))
85                 .put(FIELD_FADE_TIME.bytes(fadeTime));
86     }
87
88     @Override
89     public int[] expectedResponses() {
90         return new int[] { StateResponse.TYPE };
91     }
92 }