2 * Copyright (c) 2010-2022 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.nibeheatpump.internal.message;
15 import org.openhab.binding.nibeheatpump.internal.NibeHeatPumpException;
16 import org.openhab.binding.nibeheatpump.internal.protocol.NibeHeatPumpProtocol;
19 * The {@link ModbusWriteResponseMessage} implements Nibe write response message.
22 * @author Pauli Anttila - Initial contribution
24 public class ModbusWriteResponseMessage extends NibeHeatPumpBaseMessage {
26 private boolean result;
28 private ModbusWriteResponseMessage(MessageBuilder builder) {
29 super.msgType = MessageType.MODBUS_WRITE_RESPONSE_MSG;
30 this.result = builder.result;
33 public ModbusWriteResponseMessage(byte[] data) throws NibeHeatPumpException {
38 public void encodeMessage(byte[] data) throws NibeHeatPumpException {
39 if (NibeHeatPumpProtocol.isModbus40WriteResponsePdu(data)) {
40 super.encodeMessage(data);
41 result = rawMessage[NibeHeatPumpProtocol.RES_OFFS_DATA] == 1;
43 throw new NibeHeatPumpException("Not Write Response message");
47 public boolean isSuccessfull() {
52 public byte[] decodeMessage() {
53 byte[] data = new byte[7];
55 data[0] = NibeHeatPumpProtocol.FRAME_START_CHAR_RES;
57 data[2] = NibeHeatPumpProtocol.ADR_MODBUS40;
58 data[3] = NibeHeatPumpProtocol.CMD_MODBUS_WRITE_RESP;
59 data[4] = (byte) 0x01; // data len
60 data[5] = result ? (byte) 0x01 : (byte) 0x00;
61 data[6] = NibeHeatPumpProtocol.calculateChecksum(data, 2, 6);
67 public String toString() {
68 String str = super.toString();
69 str += ", Result = " + result;
74 public static class MessageBuilder {
75 private boolean result;
77 public MessageBuilder result(boolean result) {
82 public ModbusWriteResponseMessage build() {
83 return new ModbusWriteResponseMessage(this);