]> git.basschouten.com Git - openhab-addons.git/blob
d8367bcda6a6187c39bd7060dea93c95c82291ef
[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 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 = (rawMessage[4] & 0xFF) << 8 | (rawMessage[3] & 0xFF);
45         } else {
46             throw new NibeHeatPumpException("Not Read Request message");
47         }
48     }
49
50     @Override
51     public byte[] decodeMessage() {
52         byte[] data = new byte[6];
53         data[0] = NibeHeatPumpProtocol.FRAME_START_CHAR_REQ;
54         data[1] = NibeHeatPumpProtocol.CMD_MODBUS_READ_REQ;
55         data[2] = (byte) 0x02; // data len
56         data[3] = (byte) (coilAddress & 0xFF);
57         data[4] = (byte) ((coilAddress >> 8) & 0xFF);
58         data[5] = NibeHeatPumpProtocol.calculateChecksum(data, 0, 5);
59
60         return data;
61     }
62
63     @Override
64     public String toString() {
65         String str = super.toString();
66         str += ", Coil address = " + coilAddress;
67
68         return str;
69     }
70
71     public static class MessageBuilder {
72         private int coilAddress;
73
74         public MessageBuilder coilAddress(int coilAddress) {
75             this.coilAddress = coilAddress;
76             return this;
77         }
78
79         public ModbusReadRequestMessage build() {
80             return new ModbusReadRequestMessage(this);
81         }
82     }
83
84     @Override
85     public String toHexString() {
86         if (rawMessage == null) {
87             rawMessage = decodeMessage();
88         }
89
90         return super.toHexString();
91     }
92 }