2 * Copyright (c) 2010-2021 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 result = modbus40WriteSuccess(data);
42 public boolean isSuccessfull() {
47 public byte[] decodeMessage() {
48 return createModbusWriteResponsePdu(result);
51 private byte[] createModbusWriteResponsePdu(boolean result) {
52 byte[] data = new byte[7];
54 data[0] = NibeHeatPumpProtocol.FRAME_START_CHAR_FROM_NIBE;
56 data[2] = NibeHeatPumpProtocol.ADR_MODBUS40;
57 data[3] = NibeHeatPumpProtocol.CMD_MODBUS_WRITE_RESP;
58 data[4] = (byte) 0x01; // data len
59 data[5] = result ? (byte) 0x01 : (byte) 0x00;
60 data[6] = NibeHeatPumpProtocol.calculateChecksum(data, 2, 6);
66 public String toString() {
67 String str = super.toString();
68 str += ", Result = " + result;
73 private boolean modbus40WriteSuccess(byte[] data) throws NibeHeatPumpException {
74 if (NibeHeatPumpProtocol.isModbus40WriteResponsePdu(data)) {
75 super.encodeMessage(data);
76 return data[NibeHeatPumpProtocol.OFFSET_DATA] == 1;
78 throw new NibeHeatPumpException("Not Write Response message");
81 public static class MessageBuilder {
82 private boolean result;
84 public MessageBuilder result(boolean result) {
89 public ModbusWriteResponseMessage build() {
90 return new ModbusWriteResponseMessage(this);