2 * Copyright (c) 2010-2022 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.lifx.internal.dto;
15 import java.nio.ByteBuffer;
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.UInt32Field;
21 import org.openhab.binding.lifx.internal.fields.UInt64Field;
22 import org.openhab.binding.lifx.internal.fields.UInt8Field;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
27 * Implementation of SetTileEffect packet
29 * @author Pawel Pieczul - Initial contribution
31 public class SetTileEffectRequest extends Packet {
33 public static final int TYPE = 0x2CF;
35 // size.from.to....what
36 // ------------------------------
37 // 1....0....0.....reserved
38 // 1....1....1.....reserved
39 // 4....2....5.....instance ID
40 // 1....6....6.....effect type
41 // 4....7....10....speed
42 // 8....11...18....duration
43 // 4....19...22....reserved
44 // 4....23...26....reserved
45 // 32...27...58....parameters (8*32 bits)
46 // 1....59...59....palette count
47 // 128..60...187...palette (16*8 bits)
49 private static final Field<Integer> FIELD_RESERVED_0 = new UInt8Field();
50 private static final Field<Integer> FIELD_RESERVED_1 = new UInt8Field();
51 private static final Field<Long> FIELD_INSTANCE_ID = new UInt32Field().little();
52 private static final Field<Integer> FIELD_TYPE = new UInt8Field();
53 private static final Field<Long> FIELD_SPEED = new UInt32Field().little();
54 private static final Field<Long> FIELD_DURATION = new UInt64Field().little();
55 private static final Field<Long> FIELD_RESERVED_19_TO_26 = new UInt32Field().little();
56 private static final Field<Long> FIELD_PARAMETER_27_TO_58 = new UInt32Field().little();
57 private static final Field<Integer> FIELD_PALETTE_COUNT = new UInt8Field();
58 private static final Field<HSBK> FIELD_PALETTE_60_TO_187 = new HSBKField();
60 private final Logger logger = LoggerFactory.getLogger(SetTileEffectRequest.class);
62 private Integer reserved0 = 0;
63 private Integer reserved1 = 0;
64 private Long reserved19to22 = 0L;
65 private Long reserved23to26 = 0L;
66 private Effect effect;
68 public SetTileEffectRequest() {
74 public SetTileEffectRequest(Effect effect) {
80 public int packetType() {
85 protected int packetLength() {
90 protected void parsePacket(ByteBuffer bytes) {
91 reserved0 = FIELD_RESERVED_0.value(bytes);
92 reserved1 = FIELD_RESERVED_1.value(bytes);
93 FIELD_INSTANCE_ID.value(bytes);
94 Integer effectType = FIELD_TYPE.value(bytes);
95 Long speed = FIELD_SPEED.value(bytes);
96 Long duration = FIELD_DURATION.value(bytes);
97 reserved19to22 = FIELD_RESERVED_19_TO_26.value(bytes);
98 reserved23to26 = FIELD_RESERVED_19_TO_26.value(bytes);
99 Long[] parameters = new Long[8];
100 for (int i = 0; i < parameters.length; i++) {
101 parameters[i] = FIELD_PARAMETER_27_TO_58.value(bytes);
103 Integer paletteCount = FIELD_PALETTE_COUNT.value(bytes);
104 HSBK[] palette = new HSBK[paletteCount];
105 for (int i = 0; i < palette.length; i++) {
106 palette[i] = FIELD_PALETTE_60_TO_187.value(bytes);
109 effect = new Effect(effectType, speed, duration, palette);
110 } catch (IllegalArgumentException e) {
111 logger.debug("Wrong effect type received: {}", effectType);
117 protected ByteBuffer packetBytes() {
118 ByteBuffer buffer = ByteBuffer.allocate(packetLength());
119 buffer.put(FIELD_RESERVED_0.bytes(reserved0));
120 buffer.put(FIELD_RESERVED_1.bytes(reserved1));
121 buffer.put(FIELD_INSTANCE_ID.bytes(0L));
122 buffer.put(FIELD_TYPE.bytes(effect.getType().intValue()));
123 buffer.put(FIELD_SPEED.bytes(effect.getSpeed()));
124 buffer.put(FIELD_DURATION.bytes(effect.getDuration()));
125 buffer.put(FIELD_RESERVED_19_TO_26.bytes(reserved19to22));
126 buffer.put(FIELD_RESERVED_19_TO_26.bytes(reserved23to26));
127 for (int i = 0; i < 8; i++) {
128 buffer.put(FIELD_PARAMETER_27_TO_58.bytes(0L));
130 buffer.put(FIELD_PALETTE_COUNT.bytes(effect.getPalette().length));
131 HSBK[] palette = effect.getPalette();
132 for (int i = 0; i < palette.length; i++) {
133 buffer.put(FIELD_PALETTE_60_TO_187.bytes(palette[i]));
135 HSBK hsbkZero = new HSBK(0, 0, 0, 0);
136 for (int i = 0; i < 16 - palette.length; i++) {
137 buffer.put(FIELD_PALETTE_60_TO_187.bytes(hsbkZero));
139 if (logger.isDebugEnabled()) {
140 logger.debug("SetTileEffectRequest: type={}, speed={}, duration={}, palette_count={}", effect.getType(),
141 effect.getSpeed(), effect.getDuration(), palette.length);
142 for (int i = 0; i < palette.length; i++) {
143 logger.debug("SetTileEffectRequest palette[{}] = {}", i, palette[i]);
150 public int[] expectedResponses() {