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 org.eclipse.jdt.annotation.NonNullByDefault;
18 * This class can be used to decode the digital values received in a messag and
19 * also to create a new DigitalMessage used to send ON/OFF to a digital CAN
22 * @author Timo Wendt - Initial contribution
23 * @author Christian Niessner - Ported to OpenHAB2
26 public final class DigitalMessage extends Message {
28 public DigitalMessage(byte[] raw) {
33 * Create a new message to be sent to the CMI. It is only supported to use the
34 * first port for each CAN node. This is due to the fact that all digital port
35 * for the specific CAN node are send within a single message.
37 public DigitalMessage(byte canNode, byte podNr) {
38 super(canNode, podNr);
42 * Get the state of the specified port number.
47 public boolean getPortState(int portNumber) {
48 return getBit(getValue(0), (portNumber - 1) % 16);
52 * Set the state of the specified port number.
58 public boolean setPortState(int portNumber, boolean value) {
59 short val = getValue(0);
60 int bit = (1 << portNumber);
66 return setValue(0, val, 0);
70 * Read the specified bit from the short value holding the states of all 16
77 private boolean getBit(int portBits, int portBit) {
78 int result = (portBits >> portBit) & 0x1;
79 return result == 1 ? true : false;
83 * Check if message contains a value for the specified port number. portNumber
84 * Digital messages are in POD 0 for 1-16 and POD 9 for 17-32
86 * @param portNumber - the portNumber in Range 1-32
90 public boolean hasPortnumber(int portNumber) {
91 if (podNumber == 0 && portNumber <= 16) {
94 if (podNumber == 9 && portNumber >= 17) {
101 public MessageType getType() {
102 return MessageType.DIGITAL;