]> git.basschouten.com Git - openhab-addons.git/blob
8193a45efb7aed13aa8991375cd1945a7386132c
[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 ModbusWriteRequestMessage} implements Nibe write request message.
20  *
21  * @author Pauli Anttila - Initial contribution
22  */
23 public class ModbusWriteRequestMessage extends NibeHeatPumpBaseMessage {
24
25     private int coilAddress;
26     private int value;
27
28     private ModbusWriteRequestMessage(MessageBuilder builder) {
29         super.msgType = MessageType.MODBUS_WRITE_REQUEST_MSG;
30         this.coilAddress = builder.coilAddress;
31         this.value = builder.value;
32     }
33
34     public ModbusWriteRequestMessage(byte[] data) throws NibeHeatPumpException {
35         encodeMessage(data);
36     }
37
38     public int getCoilAddress() {
39         return coilAddress;
40     }
41
42     public void setCoildAddress(int coildAddress) {
43         this.coilAddress = coildAddress;
44     }
45
46     public int getValue() {
47         return value;
48     }
49
50     public void setValue(int value) {
51         this.value = value;
52     }
53
54     @Override
55     public void encodeMessage(byte[] data) throws NibeHeatPumpException {
56         if (NibeHeatPumpProtocol.isModbus40WriteRequestPdu(data)) {
57             super.encodeMessage(data);
58             coilAddress = (rawMessage[4] & 0xFF) << 8 | (rawMessage[3] & 0xFF);
59             value = (rawMessage[8] & 0xFF) << 24 | (rawMessage[7] & 0xFF) << 16 | (rawMessage[6] & 0xFF) << 8
60                     | (rawMessage[5] & 0xFF);
61         } else {
62             throw new NibeHeatPumpException("Not Write Request message");
63         }
64     }
65
66     @Override
67     public byte[] decodeMessage() {
68         byte[] data = new byte[10];
69
70         data[0] = NibeHeatPumpProtocol.FRAME_START_CHAR_REQ;
71         data[1] = NibeHeatPumpProtocol.CMD_MODBUS_WRITE_REQ;
72         data[2] = (byte) 0x06; // data len
73         data[3] = (byte) (coilAddress & 0xFF);
74         data[4] = (byte) ((coilAddress >> 8) & 0xFF);
75         data[5] = (byte) (value & 0xFF);
76         data[6] = (byte) ((value >> 8) & 0xFF);
77         data[7] = (byte) ((value >> 16) & 0xFF);
78         data[8] = (byte) ((value >> 24) & 0xFF);
79         data[9] = NibeHeatPumpProtocol.calculateChecksum(data, 0, 9);
80
81         return data;
82     }
83
84     @Override
85     public String toString() {
86         String str = super.toString();
87         str += ", Coil address = " + coilAddress;
88         str += ", Value = " + value;
89
90         return str;
91     }
92
93     public static class MessageBuilder {
94         private int coilAddress;
95         private int value;
96
97         public MessageBuilder coilAddress(int coilAddress) {
98             this.coilAddress = coilAddress;
99             return this;
100         }
101
102         public MessageBuilder value(int value) {
103             this.value = value;
104             return this;
105         }
106
107         public ModbusWriteRequestMessage build() {
108             return new ModbusWriteRequestMessage(this);
109         }
110     }
111
112     @Override
113     public String toHexString() {
114         if (rawMessage == null) {
115             rawMessage = decodeMessage();
116         }
117
118         return super.toHexString();
119     }
120 }