]> git.basschouten.com Git - openhab-addons.git/blob
02110f1364dcbffb24f1abd133403e07d98da96a
[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.lcn.internal.connection;
14
15 import java.io.IOException;
16 import java.io.OutputStream;
17 import java.nio.BufferOverflowException;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.lcn.internal.common.LcnAddr;
21 import org.openhab.binding.lcn.internal.common.LcnDefs;
22 import org.openhab.binding.lcn.internal.common.PckGenerator;
23
24 /**
25  * A PCK command to be send to LCN-PCHK.
26  * It is already encoded as bytes to allow different text-encodings (ANSI, UTF-8).
27  *
28  * @author Tobias Jüttner - Initial Contribution
29  * @author Fabian Wolter - Migration to OH2
30  */
31 @NonNullByDefault
32 class SendDataPck extends SendData {
33     /** The target LCN address. */
34     private final LcnAddr addr;
35
36     /** true to acknowledge the command on receipt. */
37     private final boolean wantsAck;
38
39     /** PCK command (without address header) encoded as bytes. */
40     private final byte[] data;
41
42     /**
43      * Constructor.
44      *
45      * @param addr the target LCN address
46      * @param wantsAck true to claim receipt
47      * @param data the PCK command encoded as bytes
48      */
49     SendDataPck(LcnAddr addr, boolean wantsAck, byte[] data) {
50         this.addr = addr;
51         this.wantsAck = wantsAck;
52         this.data = data;
53     }
54
55     /**
56      * Gets the PCK command.
57      *
58      * @return the PCK command encoded as bytes
59      */
60     byte[] getData() {
61         return this.data;
62     }
63
64     @Override
65     boolean write(OutputStream buffer, int localSegId) throws BufferOverflowException, IOException {
66         buffer.write(PckGenerator.generateAddressHeader(this.addr, localSegId == -1 ? 0 : localSegId, this.wantsAck)
67                 .getBytes(LcnDefs.LCN_ENCODING));
68         buffer.write(this.data);
69         buffer.write(PckGenerator.TERMINATION.getBytes(LcnDefs.LCN_ENCODING));
70         return true;
71     }
72
73     @Override
74     public String toString() {
75         return "Addr: " + addr + ": " + new String(data, 0, data.length, LcnDefs.LCN_ENCODING);
76     }
77 }