]> git.basschouten.com Git - openhab-addons.git/blob
e3b882c3d7fc9c4f5e98441fb1477f50f6b1427e
[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.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;
25
26 /**
27  * Implementation of SetTileEffect packet
28  *
29  * @author Pawel Pieczul - Initial contribution
30  */
31 public class SetTileEffectRequest extends Packet {
32
33     public static final int TYPE = 0x2CF;
34
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)
48
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();
59
60     private final Logger logger = LoggerFactory.getLogger(SetTileEffectRequest.class);
61
62     private Integer reserved0 = 0;
63     private Integer reserved1 = 0;
64     private Long reserved19to22 = 0L;
65     private Long reserved23to26 = 0L;
66     private Effect effect;
67
68     public SetTileEffectRequest() {
69         setAddressable(true);
70         setAckRequired(true);
71     }
72
73     public SetTileEffectRequest(Effect effect) {
74         this();
75         this.effect = effect;
76     }
77
78     @Override
79     public int packetType() {
80         return TYPE;
81     }
82
83     @Override
84     protected int packetLength() {
85         return 188;
86     }
87
88     @Override
89     protected void parsePacket(ByteBuffer bytes) {
90         reserved0 = FIELD_RESERVED_0.value(bytes);
91         reserved1 = FIELD_RESERVED_1.value(bytes);
92         FIELD_INSTANCE_ID.value(bytes);
93         Integer effectType = FIELD_TYPE.value(bytes);
94         Long speed = FIELD_SPEED.value(bytes);
95         Long duration = FIELD_DURATION.value(bytes);
96         reserved19to22 = FIELD_RESERVED_19_TO_26.value(bytes);
97         reserved23to26 = FIELD_RESERVED_19_TO_26.value(bytes);
98         Long[] parameters = new Long[8];
99         for (int i = 0; i < parameters.length; i++) {
100             parameters[i] = FIELD_PARAMETER_27_TO_58.value(bytes);
101         }
102         Integer paletteCount = FIELD_PALETTE_COUNT.value(bytes);
103         HSBK[] palette = new HSBK[paletteCount];
104         for (int i = 0; i < palette.length; i++) {
105             palette[i] = FIELD_PALETTE_60_TO_187.value(bytes);
106         }
107         try {
108             effect = new Effect(effectType, speed, duration, palette);
109         } catch (IllegalArgumentException e) {
110             logger.debug("Wrong effect type received: {}", effectType);
111             effect = null;
112         }
113     }
114
115     @Override
116     protected ByteBuffer packetBytes() {
117         ByteBuffer buffer = ByteBuffer.allocate(packetLength());
118         buffer.put(FIELD_RESERVED_0.bytes(reserved0));
119         buffer.put(FIELD_RESERVED_1.bytes(reserved1));
120         buffer.put(FIELD_INSTANCE_ID.bytes(0L));
121         buffer.put(FIELD_TYPE.bytes(effect.getType().intValue()));
122         buffer.put(FIELD_SPEED.bytes(effect.getSpeed()));
123         buffer.put(FIELD_DURATION.bytes(effect.getDuration()));
124         buffer.put(FIELD_RESERVED_19_TO_26.bytes(reserved19to22));
125         buffer.put(FIELD_RESERVED_19_TO_26.bytes(reserved23to26));
126         for (int i = 0; i < 8; i++) {
127             buffer.put(FIELD_PARAMETER_27_TO_58.bytes(0L));
128         }
129         buffer.put(FIELD_PALETTE_COUNT.bytes(effect.getPalette().length));
130         HSBK[] palette = effect.getPalette();
131         for (int i = 0; i < palette.length; i++) {
132             buffer.put(FIELD_PALETTE_60_TO_187.bytes(palette[i]));
133         }
134         HSBK hsbkZero = new HSBK(0, 0, 0, 0);
135         for (int i = 0; i < 16 - palette.length; i++) {
136             buffer.put(FIELD_PALETTE_60_TO_187.bytes(hsbkZero));
137         }
138         if (logger.isDebugEnabled()) {
139             logger.debug("SetTileEffectRequest: type={}, speed={}, duration={}, palette_count={}", effect.getType(),
140                     effect.getSpeed(), effect.getDuration(), palette.length);
141             for (int i = 0; i < palette.length; i++) {
142                 logger.debug("SetTileEffectRequest palette[{}] = {}", i, palette[i]);
143             }
144         }
145         return buffer;
146     }
147
148     @Override
149     public int[] expectedResponses() {
150         return new int[] {};
151     }
152 }