]> git.basschouten.com Git - openhab-addons.git/blob
ca28046868c792a51f318891ee1b8d370f700285
[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.bluetooth.bluegiga.internal.command.attributeclient;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.binding.bluetooth.bluegiga.internal.BlueGigaDeviceCommand;
17
18 /**
19  * Class to implement the BlueGiga command <b>attributeWrite</b>.
20  * <p>
21  * This command can be used to write an attributes value on a remote device. In order to write the
22  * value of an attribute a connection must exists and you need to know the handle of the attribute
23  * you want to write
24  * <p>
25  * This class provides methods for processing BlueGiga API commands.
26  * <p>
27  * Note that this code is autogenerated. Manual changes may be overwritten.
28  *
29  * @author Chris Jackson - Initial contribution of Java code generator
30  * @author Pauli Anttila - Added message builder
31  */
32 @NonNullByDefault
33 public class BlueGigaAttributeWriteCommand extends BlueGigaDeviceCommand {
34     public static final int COMMAND_CLASS = 0x04;
35     public static final int COMMAND_METHOD = 0x05;
36
37     private BlueGigaAttributeWriteCommand(CommandBuilder builder) {
38         super.setConnection(builder.connection);
39         this.attHandle = builder.attHandle;
40         this.data = builder.data;
41     }
42
43     /**
44      * Attribute handle to write to
45      * <p>
46      * BlueGiga API type is <i>uint16</i> - Java type is {@link int}
47      */
48     private int attHandle;
49
50     /**
51      * Attribute value
52      * <p>
53      * BlueGiga API type is <i>uint8array</i> - Java type is {@link int[]}
54      */
55     private int[] data;
56
57     @Override
58     public int[] serialize() {
59         // Serialize the header
60         serializeHeader(COMMAND_CLASS, COMMAND_METHOD);
61
62         // Serialize the fields
63         serializeUInt8(connection);
64         serializeUInt16(attHandle);
65         serializeUInt8Array(data);
66
67         return getPayload();
68     }
69
70     @Override
71     public String toString() {
72         final StringBuilder builder = new StringBuilder();
73         builder.append("BlueGigaAttributeWriteCommand [connection=");
74         builder.append(connection);
75         builder.append(", attHandle=");
76         builder.append(attHandle);
77         builder.append(", data=");
78         for (int c = 0; c < data.length; c++) {
79             if (c > 0) {
80                 builder.append(' ');
81             }
82             builder.append(String.format("%02X", data[c] & 0xFF));
83         }
84         builder.append(']');
85         return builder.toString();
86     }
87
88     public static class CommandBuilder {
89         private int connection;
90         private int attHandle;
91         private int[] data = new int[0];
92
93         /**
94          * Set connection handle.
95          *
96          * @param connection the connection to set as {@link int}
97          */
98         public CommandBuilder withConnection(int connection) {
99             this.connection = connection;
100             return this;
101         }
102
103         /**
104          * Attribute handle to write to
105          *
106          * @param attHandle the attHandle to set as {@link int}
107          */
108         public CommandBuilder withAttHandle(int attHandle) {
109             this.attHandle = attHandle;
110             return this;
111         }
112
113         /**
114          * Attribute value
115          *
116          * @param data the data to set as {@link int[]}
117          */
118         public CommandBuilder withData(int[] data) {
119             this.data = data;
120             return this;
121         }
122
123         public BlueGigaAttributeWriteCommand build() {
124             return new BlueGigaAttributeWriteCommand(this);
125         }
126     }
127 }