2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.lcn.internal.connection;
15 import java.io.IOException;
16 import java.io.OutputStream;
17 import java.nio.BufferOverflowException;
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;
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).
28 * @author Tobias Jüttner - Initial Contribution
29 * @author Fabian Wolter - Migration to OH2
32 class SendDataPck extends SendData {
33 /** The target LCN address. */
34 private final LcnAddr addr;
36 /** true to acknowledge the command on receipt. */
37 private final boolean wantsAck;
39 /** PCK command (without address header) encoded as bytes. */
40 private final byte[] data;
45 * @param addr the target LCN address
46 * @param wantsAck true to claim receipt
47 * @param data the PCK command encoded as bytes
49 SendDataPck(LcnAddr addr, boolean wantsAck, byte[] data) {
51 this.wantsAck = wantsAck;
56 * Gets the PCK command.
58 * @return the PCK command encoded as bytes
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));
74 public String toString() {
75 return "Addr: " + addr + ": " + new String(data, 0, data.length, LcnDefs.LCN_ENCODING);