]> git.basschouten.com Git - openhab-addons.git/blob
12723dad540e2957dd14b8dc0c502bd53a66b4cd
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.UInt16Field;
19 import org.openhab.binding.lifx.internal.fields.UInt32Field;
20
21 /**
22  * @author Tim Buckley - Initial contribution
23  * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification
24  */
25 public class SetLightPowerRequest extends Packet {
26
27     public static final int TYPE = 0x75;
28
29     public static final Field<Integer> FIELD_STATE = new UInt16Field();
30     public static final Field<Long> FIELD_DURATION = new UInt32Field().little();
31
32     private PowerState state;
33     private long duration;
34
35     public PowerState getState() {
36         return state;
37     }
38
39     public long getDuration() {
40         return duration;
41     }
42
43     public void setDuration(long duration) {
44         this.duration = duration;
45     }
46
47     public SetLightPowerRequest() {
48         state = PowerState.OFF;
49         this.duration = 0;
50         setTagged(false);
51         setAddressable(true);
52         setResponseRequired(true);
53     }
54
55     public SetLightPowerRequest(PowerState state) {
56         this.state = state;
57         this.duration = 0;
58         setTagged(false);
59         setAddressable(true);
60         setResponseRequired(true);
61     }
62
63     @Override
64     public int packetType() {
65         return TYPE;
66     }
67
68     @Override
69     protected int packetLength() {
70         return 6;
71     }
72
73     @Override
74     protected void parsePacket(ByteBuffer bytes) {
75         state = PowerState.fromValue(FIELD_STATE.value(bytes));
76         duration = FIELD_DURATION.value(bytes);
77     }
78
79     @Override
80     protected ByteBuffer packetBytes() {
81         return ByteBuffer.allocate(packetLength()).put(FIELD_STATE.bytes(state.getValue()))
82                 .put(FIELD_DURATION.bytes(duration));
83     }
84
85     @Override
86     public int[] expectedResponses() {
87         return new int[] { StateLightPowerResponse.TYPE };
88     }
89 }