2 * Copyright (c) 2010-2020 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;
61 public NibeHeatPumpBaseMessage() {
64 public NibeHeatPumpBaseMessage(byte[] data) throws NibeHeatPumpException {
69 public void encodeMessage(byte[] data) throws NibeHeatPumpException {
70 data = NibeHeatPumpProtocol.checkMessageChecksumAndRemoveDoubles(data);
74 byte messageTypeByte = NibeHeatPumpProtocol.getMessageType(data);
75 msgType = NibeHeatPumpBaseMessage.getMessageType(messageTypeByte);
79 public String toString() {
80 return "Message type = " + msgType;
84 public String toHexString() {
85 if (rawMessage == null) {
88 return HexUtils.bytesToHex(rawMessage);