]> git.basschouten.com Git - openhab-addons.git/blob
af6ac5b1bed624ec53347e1dbda27a2ebfc84d66
[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.UInt8Field;
21
22 /**
23  * @author Wouter Born - Initial contribution
24  */
25 public class StateMultiZoneResponse extends Packet {
26
27     public static final int TYPE = 0x1FA;
28     public static final int ZONES = 8;
29
30     public static final Field<Integer> FIELD_COUNT = new UInt8Field();
31     public static final Field<Integer> FIELD_INDEX = new UInt8Field();
32     public static final HSBKField FIELD_COLOR = new HSBKField();
33
34     private int count;
35     private int index;
36     private HSBK[] colors = new HSBK[ZONES];
37
38     @Override
39     public String toString() {
40         StringBuilder sb = new StringBuilder();
41         sb.append("count=");
42         sb.append(count);
43         sb.append(", index=");
44         sb.append(index);
45
46         for (int i = 0; i < ZONES; i++) {
47             sb.append(", ");
48             sb.append(colors[i].toString("color[" + i + "]"));
49         }
50
51         return sb.toString();
52     }
53
54     public int getCount() {
55         return count;
56     }
57
58     public int getIndex() {
59         return index;
60     }
61
62     public HSBK[] getColors() {
63         return colors;
64     }
65
66     @Override
67     public int packetType() {
68         return TYPE;
69     }
70
71     @Override
72     protected int packetLength() {
73         return 66;
74     }
75
76     @Override
77     protected void parsePacket(ByteBuffer bytes) {
78         count = FIELD_COUNT.value(bytes);
79         index = FIELD_INDEX.value(bytes);
80         for (int i = 0; i < ZONES; i++) {
81             colors[i] = FIELD_COLOR.value(bytes);
82         }
83     }
84
85     @Override
86     protected ByteBuffer packetBytes() {
87         ByteBuffer bb = ByteBuffer.allocate(packetLength()).put(FIELD_COUNT.bytes(count)).put(FIELD_INDEX.bytes(index));
88         for (int i = 0; i < ZONES; i++) {
89             bb.put(FIELD_COLOR.bytes(colors[i]));
90         }
91         return bb;
92     }
93
94     @Override
95     public int[] expectedResponses() {
96         return new int[] {};
97     }
98 }