]> git.basschouten.com Git - openhab-addons.git/blob
797705056e4776795104692c282b38c5534277c0
[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 ModbusReadRequestMessage} implements Nibe read request message.
20  *
21  * @author Pauli Anttila - Initial contribution
22  */
23 public class ModbusReadRequestMessage extends NibeHeatPumpBaseMessage {
24
25     private int coilAddress;
26
27     private ModbusReadRequestMessage(MessageBuilder builder) {
28         super.msgType = MessageType.MODBUS_READ_REQUEST_MSG;
29         this.coilAddress = builder.coilAddress;
30     }
31
32     ModbusReadRequestMessage(byte[] data) throws NibeHeatPumpException {
33         encodeMessage(data);
34     }
35
36     public int getCoilAddress() {
37         return coilAddress;
38     }
39
40     @Override
41     public void encodeMessage(byte[] data) throws NibeHeatPumpException {
42         if (NibeHeatPumpProtocol.isModbus40ReadRequestPdu(data)) {
43             super.encodeMessage(data);
44             coilAddress = (data[4] & 0xFF) << 8 | (data[3] & 0xFF);
45         } else {
46             throw new NibeHeatPumpException("Not Read Request message");
47         }
48     }
49
50     @Override
51     public byte[] decodeMessage() {
52         return createModbus40ReadPdu(coilAddress);
53     }
54
55     private byte[] createModbus40ReadPdu(int coilAddress) {
56         byte[] data = new byte[6];
57         data[0] = NibeHeatPumpProtocol.FRAME_START_CHAR_TO_NIBE;
58         data[1] = NibeHeatPumpProtocol.CMD_MODBUS_READ_REQ;
59         data[2] = (byte) 0x02; // data len
60         data[3] = (byte) (coilAddress & 0xFF);
61         data[4] = (byte) ((coilAddress >> 8) & 0xFF);
62         data[5] = NibeHeatPumpProtocol.calculateChecksum(data, 0, 5);
63
64         return data;
65     }
66
67     @Override
68     public String toString() {
69         String str = super.toString();
70         str += ", Coil address = " + coilAddress;
71
72         return str;
73     }
74
75     public static class MessageBuilder {
76         private int coilAddress;
77
78         public MessageBuilder coilAddress(int coilAddress) {
79             this.coilAddress = coilAddress;
80             return this;
81         }
82
83         public ModbusReadRequestMessage build() {
84             return new ModbusReadRequestMessage(this);
85         }
86     }
87
88     @Override
89     public String toHexString() {
90         if (rawMessage == null) {
91             rawMessage = decodeMessage();
92         }
93
94         return super.toHexString();
95     }
96 }