2 * Copyright (c) 2010-2023 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 StateTileEffect packet
29 * @author Pawel Pieczul - Initial contribution
31 public class StateTileEffectResponse extends Packet {
33 public static final int TYPE = 0x2D0;
35 // size.from.to....what
36 // ------------------------------
37 // 1....0....0.....reserved
38 // 4....1....4.....instance ID
39 // 1....5....5.....effect type
40 // 4....6....9.....speed
41 // 8....10...17....duration
42 // 4....18...21....reserved
43 // 4....22...25....reserved
44 // 32...26...57....parameters (8*32 bits)
45 // 1....58...58....palette count
46 // 128..59...186...palette (16*8 bits)
48 private static final Field<Integer> FIELD_RESERVED_0 = new UInt8Field();
49 private static final Field<Long> FIELD_INSTANCE_ID = new UInt32Field().little();
50 private static final Field<Integer> FIELD_TYPE = new UInt8Field();
51 private static final Field<Long> FIELD_SPEED = new UInt32Field().little();
52 private static final Field<Long> FIELD_DURATION = new UInt64Field().little();
53 private static final Field<Long> FIELD_RESERVED_18_TO_25 = new UInt32Field().little();
54 private static final Field<Long> FIELD_PARAMETER_26_TO_57 = new UInt32Field().little();
55 private static final Field<Integer> FIELD_PALETTE_COUNT = new UInt8Field();
56 private static final Field<HSBK> FIELD_PALETTE_59_TO_186 = new HSBKField();
58 private Integer reserved0 = 0;
59 private Long reserved18to21 = 0L;
60 private Long reserved22to25 = 0L;
61 private Effect effect;
63 private final Logger logger = LoggerFactory.getLogger(StateTileEffectResponse.class);
65 public Effect getEffect() {
70 public int packetType() {
75 protected int packetLength() {
80 protected void parsePacket(ByteBuffer bytes) {
81 reserved0 = FIELD_RESERVED_0.value(bytes);
82 Long instanceId = FIELD_INSTANCE_ID.value(bytes);
83 Integer effectType = FIELD_TYPE.value(bytes);
84 Long speed = FIELD_SPEED.value(bytes);
85 Long duration = FIELD_DURATION.value(bytes);
86 reserved18to21 = FIELD_RESERVED_18_TO_25.value(bytes);
87 reserved22to25 = FIELD_RESERVED_18_TO_25.value(bytes);
88 Long[] parameters = new Long[8];
89 for (int i = 0; i < 8; i++) {
90 parameters[i] = FIELD_PARAMETER_26_TO_57.value(bytes);
92 Integer paletteCount = FIELD_PALETTE_COUNT.value(bytes);
93 HSBK[] palette = new HSBK[paletteCount];
94 for (int i = 0; i < palette.length; i++) {
95 palette[i] = FIELD_PALETTE_59_TO_186.value(bytes);
98 effect = new Effect(effectType, speed, duration, palette);
99 } catch (IllegalArgumentException e) {
100 logger.debug("Wrong effect type received: {}", effectType);
103 if (logger.isDebugEnabled()) {
104 logger.debug("StateTileEffectResponse: instanceId={}, type={}, speed={}, duration={}, palette_count={}",
105 instanceId, effectType, speed, duration, paletteCount);
106 logger.debug("StateTileEffectResponse parameters=[{}, {}, {}, {}, {}, {}, {}, {}]", parameters[0],
107 parameters[1], parameters[2], parameters[3], parameters[4], parameters[5], parameters[6],
109 for (int i = 0; i < palette.length; i++) {
110 logger.debug("StateTileEffectResponse palette[{}] = {}", i, palette[i]);
116 protected ByteBuffer packetBytes() {
117 ByteBuffer buffer = ByteBuffer.allocate(packetLength());
118 buffer.put(FIELD_RESERVED_0.bytes(reserved0));
119 buffer.put(FIELD_INSTANCE_ID.bytes(0L));
120 buffer.put(FIELD_TYPE.bytes(effect.getType().intValue()));
121 buffer.put(FIELD_SPEED.bytes(effect.getSpeed()));
122 buffer.put(FIELD_DURATION.bytes(effect.getDuration()));
123 buffer.put(FIELD_RESERVED_18_TO_25.bytes(reserved18to21));
124 buffer.put(FIELD_RESERVED_18_TO_25.bytes(reserved22to25));
125 for (int i = 0; i < 8; i++) {
126 buffer.put(FIELD_PARAMETER_26_TO_57.bytes(0L));
128 HSBK[] palette = effect.getPalette();
129 buffer.put(FIELD_PALETTE_COUNT.bytes(palette.length));
130 for (int i = 0; i < palette.length; i++) {
131 buffer.put(FIELD_PALETTE_59_TO_186.bytes(palette[i]));
133 HSBK hsbkZero = new HSBK(0, 0, 0, 0);
134 for (int i = 0; i < 16 - palette.length; i++) {
135 buffer.put(FIELD_PALETTE_59_TO_186.bytes(hsbkZero));
141 public int[] expectedResponses() {