]> git.basschouten.com Git - openhab-addons.git/blob
541fbc694fefd42c06d8e61a2cdda8d1a3e3256b
[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.tacmi.internal.message;
14
15 import java.nio.ByteBuffer;
16 import java.nio.ByteOrder;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 /**
23  * Base message class handling generic functions.
24  *
25  * @author Timo Wendt - Initial contribution
26  * @author Christian Niessner - Ported to OpenHAB2
27  */
28 @NonNullByDefault
29 public abstract class Message {
30
31     protected final Logger logger = LoggerFactory.getLogger(Message.class);
32
33     /**
34      * ByteBuffer that stores the content of the message.
35      */
36     private ByteBuffer buffer;
37
38     /**
39      * CAN Node number used in the message
40      */
41     public byte canNode;
42
43     /**
44      * POD number used in the message
45      */
46     public byte podNumber;
47
48     /**
49      * Initialize from the bytes of a received message
50      *
51      * @param raw
52      */
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);
58     }
59
60     /**
61      * Used to create a new message with the specified CAN node and POD number
62      *
63      * @param canNode
64      * @param podNumber
65      */
66     public Message(int canNode, int podNumber) {
67         this.buffer = ByteBuffer.allocate(14);
68         this.buffer.order(ByteOrder.LITTLE_ENDIAN);
69         setCanNode(canNode);
70         setPodNumber(podNumber);
71     }
72
73     public abstract MessageType getType();
74
75     public abstract boolean hasPortnumber(int portNumber);
76
77     /**
78      * Get the byte array. This can be sent to the CMI.
79      *
80      * @return raw
81      */
82     public byte[] getRaw() {
83         return buffer.array();
84     }
85
86     /**
87      * Set the CAN node number for this message
88      *
89      * @param canNode
90      */
91     public void setCanNode(int canNode) {
92         buffer.put(0, (byte) (canNode & 0xff));
93     }
94
95     /**
96      * Set the POD number for this message
97      *
98      * @param podNumber
99      */
100     public void setPodNumber(int podNumber) {
101         buffer.put(1, (byte) (podNumber & 0xf));
102     }
103
104     /**
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.
108      *
109      * @param idx
110      * @param value
111      * @param measureType
112      * @return true when value was modified
113      */
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);
119             modified = true;
120         }
121         byte mtv = (byte) (measureType & 0xf);
122         if (buffer.get(idx + 10) != mtv) {
123             buffer.put(idx + 10, mtv);
124             modified = true;
125         }
126         return modified;
127     }
128
129     /**
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.
132      *
133      * @param idx
134      * @return
135      */
136     public short getValue(int idx) {
137         return (buffer.getShort(idx * 2 + 2));
138     }
139
140     /**
141      * Get the measure type for the specified index within the message.
142      *
143      * @param idx
144      * @return
145      */
146     public int getMeasureType(int idx) {
147         return (buffer.get(idx + 10)) & 0xffff;
148     }
149
150     @Override
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));
156     }
157 }