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