]> git.basschouten.com Git - openhab-addons.git/blob
b40f21d5381a09bd20305231f713d6ffc37ca87f
[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 org.eclipse.jdt.annotation.NonNullByDefault;
16
17 /**
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
20  * Input port
21  *
22  * @author Timo Wendt - Initial contribution
23  * @author Christian Niessner - Ported to OpenHAB2
24  */
25 @NonNullByDefault
26 public final class DigitalMessage extends Message {
27
28     public DigitalMessage(byte[] raw) {
29         super(raw);
30     }
31
32     /**
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.
36      */
37     public DigitalMessage(byte canNode, byte podNr) {
38         super(canNode, podNr);
39     }
40
41     /**
42      * Get the state of the specified port number.
43      *
44      * @param portNumber
45      * @return
46      */
47     public boolean getPortState(int portNumber) {
48         return getBit(getValue(0), (portNumber - 1) % 16);
49     }
50
51     /**
52      * Set the state of the specified port number.
53      *
54      * @param portNumber
55      * @param value
56      * @return
57      */
58     public boolean setPortState(int portNumber, boolean value) {
59         short val = getValue(0);
60         int bit = (1 << portNumber);
61         if (value) {
62             val |= bit;
63         } else {
64             val &= ~bit;
65         }
66         return setValue(0, val, 0);
67     }
68
69     /**
70      * Read the specified bit from the short value holding the states of all 16
71      * ports.
72      *
73      * @param portBits
74      * @param portBit
75      * @return
76      */
77     private boolean getBit(int portBits, int portBit) {
78         int result = (portBits >> portBit) & 0x1;
79         return result == 1 ? true : false;
80     }
81
82     /**
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
85      *
86      * @param portNumber - the portNumber in Range 1-32
87      * @return
88      */
89     @SuppressWarnings("PMD.SimplifyBooleanReturns")
90     @Override
91     public boolean hasPortnumber(int portNumber) {
92         if (podNumber == 0 && portNumber <= 16) {
93             return true;
94         }
95         if (podNumber == 9 && portNumber >= 17) {
96             return true;
97         }
98         return false;
99     }
100
101     @Override
102     public MessageType getType() {
103         return MessageType.DIGITAL;
104     }
105 }