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.velbus.internal;
15 import java.util.ArrayList;
16 import java.util.List;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.core.thing.ChannelUID;
22 * The {@link VelbusModuleAddress} represents the address and possible subaddresses of a Velbus module.
24 * @author Cedric Boon - Initial contribution
27 public class VelbusModuleAddress {
29 private byte[] subAddresses;
31 public VelbusModuleAddress(byte address, int numberOfSubAddresses) {
32 this(address, getInitialSubAddresses(numberOfSubAddresses));
35 public VelbusModuleAddress(byte address, byte[] subAddresses) {
36 this.address = address;
37 this.subAddresses = subAddresses;
40 public byte getAddress() {
44 public void setSubAddresses(byte[] subAddresses) {
45 this.subAddresses = subAddresses;
48 public byte[] getSubAddresses() {
52 public byte[] getActiveAddresses() {
53 List<Byte> activeAddresses = new ArrayList<>();
54 activeAddresses.add(address);
56 for (int i = 0; i < subAddresses.length; i++) {
57 if (subAddresses[i] != (byte) 0xFF) {
58 activeAddresses.add(subAddresses[i]);
62 byte[] result = new byte[activeAddresses.size()];
64 for (int i = 0; i < activeAddresses.size(); i++) {
65 result[i] = activeAddresses.get(i);
71 public VelbusChannelIdentifier getChannelIdentifier(ChannelUID channelUID) {
72 int channelIndex = getChannelIndex(channelUID);
74 return getChannelIdentifier(channelIndex);
77 public int getChannelNumber(ChannelUID channelUID) {
78 return Integer.parseInt(channelUID.getIdWithoutGroup().substring(2));
81 public int getChannelIndex(ChannelUID channelUID) {
82 return getChannelNumber(channelUID) - 1;
85 public String getChannelId(VelbusChannelIdentifier velbusChannelIdentifier) {
86 return "CH" + getChannelNumber(velbusChannelIdentifier);
89 public int getChannelIndex(VelbusChannelIdentifier velbusChannelIdentifier) {
90 return this.getChannelNumber(velbusChannelIdentifier) - 1;
93 public int getChannelNumber(VelbusChannelIdentifier velbusChannelIdentifier) {
94 byte[] activeAddresses = getActiveAddresses();
96 for (int i = 0; i < activeAddresses.length; i++) {
97 if (velbusChannelIdentifier.getAddress() == activeAddresses[i]) {
98 return (i * 8) + velbusChannelIdentifier.getChannelNumberFromBitNumber();
102 throw new IllegalArgumentException("The byte '" + velbusChannelIdentifier.getChannelByte()
103 + "' does not represent a valid channel on the address '" + velbusChannelIdentifier.getAddress()
107 public VelbusChannelIdentifier getChannelIdentifier(int channelIndex) {
108 int addressIndex = channelIndex / 8;
109 int addressChannelIndex = channelIndex % 8;
111 byte address = addressIndex == 0 ? this.address : subAddresses[addressIndex - 1];
112 byte channel = (byte) Math.pow(2, addressChannelIndex);
114 return new VelbusChannelIdentifier(address, channel);
117 private static byte[] getInitialSubAddresses(int numberOfSubAddresses) {
118 byte[] subAddresses = new byte[numberOfSubAddresses];
120 for (int i = 0; i < numberOfSubAddresses; i++) {
121 subAddresses[i] = (byte) 0xFF;