]> git.basschouten.com Git - openhab-addons.git/blob
0ba8b9668a61a4f60a11f5279412814e525beade
[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.Field;
18 import org.openhab.binding.lifx.internal.fields.HSBK;
19 import org.openhab.binding.lifx.internal.fields.HSBKField;
20 import org.openhab.binding.lifx.internal.fields.StringField;
21 import org.openhab.binding.lifx.internal.fields.UInt16Field;
22 import org.openhab.binding.lifx.internal.fields.UInt64Field;
23
24 /**
25  * @author Tim Buckley - Initial contribution
26  * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification
27  */
28 public class StateResponse extends Packet {
29
30     public static final int TYPE = 0x6B;
31
32     public static final HSBKField FIELD_COLOR = new HSBKField();
33     public static final Field<Integer> FIELD_DIM = new UInt16Field().little();
34     public static final Field<Integer> FIELD_POWER = new UInt16Field();
35     public static final Field<String> FIELD_LABEL = new StringField(32);
36     public static final Field<Long> FIELD_TAGS = new UInt64Field();
37
38     private HSBK color;
39     private int dim;
40     private PowerState power;
41     private String label;
42     private long tags;
43
44     @Override
45     public String toString() {
46         return color.toString("color") + ", dim=" + dim + ", power=" + power + ", label=" + label;
47     }
48
49     public HSBK getColor() {
50         return color;
51     }
52
53     public int getDim() {
54         return dim;
55     }
56
57     public PowerState getPower() {
58         return power;
59     }
60
61     public String getLabel() {
62         return label;
63     }
64
65     public long getTags() {
66         return tags;
67     }
68
69     @Override
70     public int packetType() {
71         return TYPE;
72     }
73
74     @Override
75     protected int packetLength() {
76         return 52;
77     }
78
79     @Override
80     protected void parsePacket(ByteBuffer bytes) {
81         color = FIELD_COLOR.value(bytes);
82         dim = FIELD_DIM.value(bytes);
83         power = PowerState.fromValue(FIELD_POWER.value(bytes));
84         label = FIELD_LABEL.value(bytes);
85         tags = FIELD_TAGS.value(bytes);
86     }
87
88     @Override
89     protected ByteBuffer packetBytes() {
90         return ByteBuffer.allocate(packetLength()).put(FIELD_COLOR.bytes(color)).put(FIELD_DIM.bytes(dim))
91                 .put(FIELD_POWER.bytes(power.getValue())).put(FIELD_LABEL.bytes(label)).put(FIELD_TAGS.bytes(tags));
92     }
93
94     @Override
95     public int[] expectedResponses() {
96         return new int[] {};
97     }
98 }