]> git.basschouten.com Git - openhab-addons.git/blob
352e82b2c3af259e0e79e1b37bda5cf6307395ed
[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 import org.openhab.core.util.HexUtils;
18
19 /**
20  * The {@link NibeHeatPumpBaseMessage} define abstract class for Nibe messages. All message implementations should
21  * extend this class.
22  *
23  *
24  * @author Pauli Anttila - Initial contribution
25  */
26 public abstract class NibeHeatPumpBaseMessage implements NibeHeatPumpMessage {
27
28     public static MessageType getMessageType(byte messageType) {
29         for (MessageType p : MessageType.values()) {
30             if (p.toByte() == messageType) {
31                 return p;
32             }
33         }
34         return MessageType.UNKNOWN;
35     }
36
37     public enum MessageType {
38         MODBUS_DATA_READ_OUT_MSG(NibeHeatPumpProtocol.CMD_MODBUS_DATA_MSG),
39         MODBUS_READ_REQUEST_MSG(NibeHeatPumpProtocol.CMD_MODBUS_READ_REQ),
40         MODBUS_READ_RESPONSE_MSG(NibeHeatPumpProtocol.CMD_MODBUS_READ_RESP),
41         MODBUS_WRITE_REQUEST_MSG(NibeHeatPumpProtocol.CMD_MODBUS_WRITE_REQ),
42         MODBUS_WRITE_RESPONSE_MSG(NibeHeatPumpProtocol.CMD_MODBUS_WRITE_RESP),
43
44         UNKNOWN(-1);
45
46         private final int msgType;
47
48         MessageType(int msgType) {
49             this.msgType = msgType;
50         }
51
52         public byte toByte() {
53             return (byte) msgType;
54         }
55     }
56
57     public byte[] rawMessage;
58     public MessageType msgType = MessageType.UNKNOWN;
59
60     public NibeHeatPumpBaseMessage() {
61     }
62
63     public NibeHeatPumpBaseMessage(byte[] data) throws NibeHeatPumpException {
64         encodeMessage(data);
65     }
66
67     @Override
68     public void encodeMessage(byte[] data) throws NibeHeatPumpException {
69         if (data.length >= NibeHeatPumpProtocol.PDU_MIN_LEN) {
70             byte[] d = NibeHeatPumpProtocol.checkMessageChecksumAndRemoveDoubles(data);
71             rawMessage = d.clone();
72
73             byte messageTypeByte = NibeHeatPumpProtocol.getMessageType(d);
74             msgType = NibeHeatPumpBaseMessage.getMessageType(messageTypeByte);
75         } else {
76             throw new NibeHeatPumpException("Too short message");
77         }
78     }
79
80     @Override
81     public String toString() {
82         return "Message type = " + msgType;
83     }
84
85     @Override
86     public String toHexString() {
87         if (rawMessage == null) {
88             return null;
89         } else {
90             return HexUtils.bytesToHex(rawMessage);
91         }
92     }
93 }