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.tacmi.internal.message;
15 import java.nio.ByteBuffer;
16 import java.nio.ByteOrder;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
23 * Base message class handling generic functions.
25 * @author Timo Wendt - Initial contribution
26 * @author Christian Niessner - Ported to OpenHAB2
29 public abstract class Message {
31 protected final Logger logger = LoggerFactory.getLogger(Message.class);
34 * ByteBuffer that stores the content of the message.
36 private ByteBuffer buffer;
39 * CAN Node number used in the message
44 * POD number used in the message
46 public byte podNumber;
49 * Initialize from the bytes of a received message
53 public Message(byte[] raw) {
54 this.buffer = ByteBuffer.wrap(raw);
55 this.buffer.order(ByteOrder.LITTLE_ENDIAN);
56 this.canNode = buffer.get(0);
57 this.podNumber = buffer.get(1);
61 * Used to create a new message with the specified CAN node and POD number
66 public Message(int canNode, int podNumber) {
67 this.buffer = ByteBuffer.allocate(14);
68 this.buffer.order(ByteOrder.LITTLE_ENDIAN);
70 setPodNumber(podNumber);
73 public abstract MessageType getType();
75 public abstract boolean hasPortnumber(int portNumber);
78 * Get the byte array. This can be sent to the CMI.
82 public byte[] getRaw() {
83 return buffer.array();
87 * Set the CAN node number for this message
91 public void setCanNode(int canNode) {
92 buffer.put(0, (byte) (canNode & 0xff));
96 * Set the POD number for this message
100 public void setPodNumber(int podNumber) {
101 buffer.put(1, (byte) (podNumber & 0xf));
105 * Set the value at th specified index within the message and the defined
106 * measure type. The measure type is only used in analog messages. Digital
107 * messages always use 0 for the measure types.
112 * @return true when value was modified
114 public boolean setValue(int idx, short value, int measureType) {
115 boolean modified = false;
116 int idxValue = idx * 2 + 2;
117 if (buffer.getShort(idxValue) != value) {
118 buffer.putShort(idxValue, value);
121 byte mtv = (byte) (measureType & 0xf);
122 if (buffer.get(idx + 10) != mtv) {
123 buffer.put(idx + 10, mtv);
130 * Get the value at the specified index within the message. The value will
131 * be converted from thr signed short to an unsigned int.
136 public short getValue(int idx) {
137 return (buffer.getShort(idx * 2 + 2));
141 * Get the measure type for the specified index within the message.
146 public int getMeasureType(int idx) {
147 return (buffer.get(idx + 10)) & 0xffff;
151 public String toString() {
152 return ("CAN: " + this.canNode + " POD: " + this.podNumber + " Value1: " + getValue(0) + " Value2: "
153 + getValue(1) + " Value3: " + getValue(2) + " Value4: " + getValue(3) + " MeasureType1 "
154 + getMeasureType(0) + " MeasureType2 " + getMeasureType(1) + " MeasureType3 " + getMeasureType(2)
155 + " MeasureType4 " + getMeasureType(3));