]> git.basschouten.com Git - openhab-addons.git/blob
158996fe8441316680b1fee0fd6de8b60f11132a
[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.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         setAddressable(true);
55         setResponseRequired(true);
56     }
57
58     public SetColorRequest(HSBK color, long fadeTime) {
59         this();
60         this.color = color;
61         this.fadeTime = fadeTime;
62     }
63
64     @Override
65     public int packetType() {
66         return TYPE;
67     }
68
69     @Override
70     protected int packetLength() {
71         return 13;
72     }
73
74     @Override
75     protected void parsePacket(ByteBuffer bytes) {
76         stream = FIELD_STREAM.value(bytes);
77         color = FIELD_COLOR.value(bytes);
78         fadeTime = FIELD_FADE_TIME.value(bytes);
79     }
80
81     @Override
82     protected ByteBuffer packetBytes() {
83         return ByteBuffer.allocate(packetLength()).put(FIELD_STREAM.bytes(stream)).put(FIELD_COLOR.bytes(color))
84                 .put(FIELD_FADE_TIME.bytes(fadeTime));
85     }
86
87     @Override
88     public int[] expectedResponses() {
89         return new int[] { StateResponse.TYPE };
90     }
91 }