2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.nibeheatpump.internal.message;
15 import org.openhab.binding.nibeheatpump.internal.NibeHeatPumpException;
16 import org.openhab.binding.nibeheatpump.internal.protocol.NibeHeatPumpProtocol;
17 import org.openhab.core.util.HexUtils;
20 * The {@link NibeHeatPumpBaseMessage} define abstract class for Nibe messages. All message implementations should
24 * @author Pauli Anttila - Initial contribution
26 public abstract class NibeHeatPumpBaseMessage implements NibeHeatPumpMessage {
28 public static MessageType getMessageType(byte messageType) {
29 for (MessageType p : MessageType.values()) {
30 if (p.toByte() == messageType) {
34 return MessageType.UNKNOWN;
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),
46 private final int msgType;
48 MessageType(int msgType) {
49 this.msgType = msgType;
52 public byte toByte() {
53 return (byte) msgType;
57 public byte[] rawMessage;
58 public MessageType msgType = MessageType.UNKNOWN;
60 public NibeHeatPumpBaseMessage() {
63 public NibeHeatPumpBaseMessage(byte[] data) throws NibeHeatPumpException {
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();
73 byte messageTypeByte = NibeHeatPumpProtocol.getMessageType(d);
74 msgType = NibeHeatPumpBaseMessage.getMessageType(messageTypeByte);
76 throw new NibeHeatPumpException("Too short message");
81 public String toString() {
82 return "Message type = " + msgType;
86 public String toHexString() {
87 if (rawMessage == null) {
90 return HexUtils.bytesToHex(rawMessage);