]> git.basschouten.com Git - openhab-addons.git/blob
d54cc0b0cc88af2da48835ddd429b8a8f8e63289
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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         result = modbus40WriteSuccess(data);
40     }
41
42     public boolean isSuccessfull() {
43         return result;
44     }
45
46     @Override
47     public byte[] decodeMessage() {
48         return createModbusWriteResponsePdu(result);
49     }
50
51     private byte[] createModbusWriteResponsePdu(boolean result) {
52         byte[] data = new byte[7];
53
54         data[0] = NibeHeatPumpProtocol.FRAME_START_CHAR_FROM_NIBE;
55         data[1] = 0x00;
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);
61
62         return data;
63     }
64
65     @Override
66     public String toString() {
67         String str = super.toString();
68         str += ", Result = " + result;
69
70         return str;
71     }
72
73     private boolean modbus40WriteSuccess(byte[] data) throws NibeHeatPumpException {
74         if (NibeHeatPumpProtocol.isModbus40WriteResponsePdu(data)) {
75             super.encodeMessage(data);
76             return data[NibeHeatPumpProtocol.OFFSET_DATA] == 1;
77         }
78         throw new NibeHeatPumpException("Not Write Response message");
79     }
80
81     public static class MessageBuilder {
82         private boolean result;
83
84         public MessageBuilder result(boolean result) {
85             this.result = result;
86             return this;
87         }
88
89         public ModbusWriteResponseMessage build() {
90             return new ModbusWriteResponseMessage(this);
91         }
92     }
93 }