]> git.basschouten.com Git - openhab-addons.git/blob
ff4988b08b645ed603a6fecc94b65defe54fe1b1
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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
20 /**
21  * @author Tim Buckley - Initial contribution
22  * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification
23  */
24 public class SetPowerRequest extends Packet {
25
26     public static final int TYPE = 0x15;
27
28     public static final Field<Integer> FIELD_STATE = new UInt16Field();
29
30     private PowerState state;
31
32     public PowerState getState() {
33         return state;
34     }
35
36     public SetPowerRequest() {
37         state = PowerState.OFF;
38         setTagged(false);
39         setAddressable(true);
40         setResponseRequired(true);
41     }
42
43     public SetPowerRequest(PowerState state) {
44         this.state = state;
45         setTagged(false);
46         setAddressable(true);
47         setResponseRequired(true);
48     }
49
50     @Override
51     public int packetType() {
52         return TYPE;
53     }
54
55     @Override
56     protected int packetLength() {
57         return 2;
58     }
59
60     @Override
61     protected void parsePacket(ByteBuffer bytes) {
62         state = PowerState.fromValue(FIELD_STATE.value(bytes));
63     }
64
65     @Override
66     protected ByteBuffer packetBytes() {
67         return ByteBuffer.allocate(2).put(FIELD_STATE.bytes(state.getValue()));
68     }
69
70     @Override
71     public int[] expectedResponses() {
72         return new int[] { StatePowerResponse.TYPE };
73     }
74 }