]> git.basschouten.com Git - openhab-addons.git/blob
c0c76842e420fd2736422a2e5c78ae40e42968da
[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.nibeheatpump.internal.message;
14
15 import org.openhab.binding.nibeheatpump.internal.NibeHeatPumpException;
16 import org.openhab.binding.nibeheatpump.internal.protocol.NibeHeatPumpProtocol;
17
18 /**
19  * The {@link ModbusWriteResponseMessage} implements Nibe write response message.
20  *
21  *
22  * @author Pauli Anttila - Initial contribution
23  */
24 public class ModbusWriteResponseMessage extends NibeHeatPumpBaseMessage {
25
26     private boolean result;
27
28     private ModbusWriteResponseMessage(MessageBuilder builder) {
29         super.msgType = MessageType.MODBUS_WRITE_RESPONSE_MSG;
30         this.result = builder.result;
31     }
32
33     public ModbusWriteResponseMessage(byte[] data) throws NibeHeatPumpException {
34         encodeMessage(data);
35     }
36
37     @Override
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;
42         } else {
43             throw new NibeHeatPumpException("Not Write Response message");
44         }
45     }
46
47     public boolean isSuccessfull() {
48         return result;
49     }
50
51     @Override
52     public byte[] decodeMessage() {
53         byte[] data = new byte[7];
54
55         data[0] = NibeHeatPumpProtocol.FRAME_START_CHAR_RES;
56         data[1] = 0x00;
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);
62
63         return data;
64     }
65
66     @Override
67     public String toString() {
68         String str = super.toString();
69         str += ", Result = " + result;
70
71         return str;
72     }
73
74     public static class MessageBuilder {
75         private boolean result;
76
77         public MessageBuilder result(boolean result) {
78             this.result = result;
79             return this;
80         }
81
82         public ModbusWriteResponseMessage build() {
83             return new ModbusWriteResponseMessage(this);
84         }
85     }
86 }