2 * Copyright (c) 2010-2024 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.nikobus.internal.utils;
15 import java.util.concurrent.Future;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
21 * The {@link Utils} class defines commonly used utility functions.
23 * @author Boris Krivonog - Initial contribution
27 public static void cancel(@Nullable Future<?> future) {
34 * Convert bus address to push button's address as seen in Nikobus
37 * @param addressString
38 * String representing a bus Push Button's address.
39 * @return Push button's address as seen in Nikobus PC software.
41 public static String convertToHumanReadableNikobusAddress(String addressString) {
43 int address = Integer.parseInt(addressString, 16);
44 int nikobusAddress = 0;
46 for (int i = 0; i < 21; ++i) {
47 nikobusAddress = (nikobusAddress << 1) | ((address >> i) & 1);
50 nikobusAddress = (nikobusAddress << 1);
51 int button = (address >> 21) & 0x07;
53 return leftPadWithZeros(Integer.toHexString(nikobusAddress), 6) + ":" + mapButton(button);
55 } catch (NumberFormatException e) {
56 return "[" + addressString + "]";
60 private static String mapButton(int buttonIndex) {
61 switch (buttonIndex) {
83 public static String leftPadWithZeros(String text, int size) {
84 StringBuilder builder = new StringBuilder(text);
85 while (builder.length() < size) {
86 builder.insert(0, '0');
88 return builder.toString();