]> git.basschouten.com Git - openhab-addons.git/blob
86b8a23e9cdd03f1bb7ad3c5e161e658535aa188
[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 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     public byte msgId;
60
61     public NibeHeatPumpBaseMessage() {
62     }
63
64     public NibeHeatPumpBaseMessage(byte[] data) throws NibeHeatPumpException {
65         encodeMessage(data);
66     }
67
68     @Override
69     public void encodeMessage(byte[] data) throws NibeHeatPumpException {
70         data = NibeHeatPumpProtocol.checkMessageChecksumAndRemoveDoubles(data);
71         rawMessage = data;
72         msgId = data[1];
73
74         byte messageTypeByte = NibeHeatPumpProtocol.getMessageType(data);
75         msgType = NibeHeatPumpBaseMessage.getMessageType(messageTypeByte);
76     }
77
78     @Override
79     public String toString() {
80         return "Message type = " + msgType;
81     }
82
83     @Override
84     public String toHexString() {
85         if (rawMessage == null) {
86             return null;
87         } else {
88             return HexUtils.bytesToHex(rawMessage);
89         }
90     }
91 }