]> git.basschouten.com Git - openhab-addons.git/blob
5a1ae5fd82c88036f54352edcb6aebb50f127a36
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.velbus.internal;
14
15 import java.util.ArrayList;
16 import java.util.List;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.core.thing.ChannelUID;
20
21 /**
22  * The {@link VelbusModuleAddress} represents the address and possible subaddresses of a Velbus module.
23  *
24  * @author Cedric Boon - Initial contribution
25  */
26 @NonNullByDefault
27 public class VelbusModuleAddress {
28     private byte address;
29     private byte[] subAddresses;
30
31     public VelbusModuleAddress(byte address, int numberOfSubAddresses) {
32         this(address, getInitialSubAddresses(numberOfSubAddresses));
33     }
34
35     public VelbusModuleAddress(byte address, byte[] subAddresses) {
36         this.address = address;
37         this.subAddresses = subAddresses;
38     }
39
40     public byte getAddress() {
41         return address;
42     }
43
44     public void setSubAddresses(byte[] subAddresses) {
45         this.subAddresses = subAddresses;
46     }
47
48     public byte[] getSubAddresses() {
49         return subAddresses;
50     }
51
52     public byte[] getActiveAddresses() {
53         List<Byte> activeAddresses = new ArrayList<>();
54         activeAddresses.add(address);
55
56         for (int i = 0; i < subAddresses.length; i++) {
57             if (subAddresses[i] != (byte) 0xFF) {
58                 activeAddresses.add(subAddresses[i]);
59             }
60         }
61
62         byte[] result = new byte[activeAddresses.size()];
63
64         for (int i = 0; i < activeAddresses.size(); i++) {
65             result[i] = activeAddresses.get(i);
66         }
67
68         return result;
69     }
70
71     public VelbusChannelIdentifier getChannelIdentifier(ChannelUID channelUID) {
72         int channelIndex = getChannelIndex(channelUID);
73
74         return getChannelIdentifier(channelIndex);
75     }
76
77     public int getChannelNumber(ChannelUID channelUID) {
78         return Integer.parseInt(channelUID.getIdWithoutGroup().substring(2));
79     }
80
81     public int getChannelIndex(ChannelUID channelUID) {
82         return getChannelNumber(channelUID) - 1;
83     }
84
85     public String getChannelId(VelbusChannelIdentifier velbusChannelIdentifier) {
86         return "CH" + getChannelNumber(velbusChannelIdentifier);
87     }
88
89     public int getChannelIndex(VelbusChannelIdentifier velbusChannelIdentifier) {
90         return this.getChannelNumber(velbusChannelIdentifier) - 1;
91     }
92
93     public int getChannelNumber(VelbusChannelIdentifier velbusChannelIdentifier) {
94         byte[] activeAddresses = getActiveAddresses();
95
96         for (int i = 0; i < activeAddresses.length; i++) {
97             if (velbusChannelIdentifier.getAddress() == activeAddresses[i]) {
98                 return (i * 8) + velbusChannelIdentifier.getChannelNumberFromBitNumber();
99             }
100         }
101
102         throw new IllegalArgumentException("The byte '" + velbusChannelIdentifier.getChannelByte()
103                 + "' does not represent a valid channel on the address '" + velbusChannelIdentifier.getAddress()
104                 + "'.");
105     }
106
107     public VelbusChannelIdentifier getChannelIdentifier(int channelIndex) {
108         int addressIndex = channelIndex / 8;
109         int addressChannelIndex = channelIndex % 8;
110
111         byte address = addressIndex == 0 ? this.address : subAddresses[addressIndex - 1];
112         byte channel = (byte) Math.pow(2, addressChannelIndex);
113
114         return new VelbusChannelIdentifier(address, channel);
115     }
116
117     private static byte[] getInitialSubAddresses(int numberOfSubAddresses) {
118         byte[] subAddresses = new byte[numberOfSubAddresses];
119
120         for (int i = 0; i < numberOfSubAddresses; i++) {
121             subAddresses[i] = (byte) 0xFF;
122         }
123
124         return subAddresses;
125     }
126 }