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.insteon.internal.utils;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.insteon.internal.device.InsteonAddress;
18 import org.openhab.binding.insteon.internal.message.DataType;
21 * Various utility functions for e.g. hex string parsing
23 * @author Daniel Pfrommer - Initial contribution
24 * @author Rob Nielsen - Port to openHAB 2 insteon binding
28 public static String getHexString(int b) {
29 return String.format("%02X", b & 0xFF);
32 public static String getHexString(byte[] b) {
33 return getHexString(b, b.length);
36 public static String getHexString(byte[] b, int len) {
38 for (int i = 0; i < b.length && i < len; i++) {
39 result += String.format("%02X ", b[i] & 0xFF);
44 public static int strToInt(String s) throws NumberFormatException {
46 if (s.startsWith("0x")) {
47 ret = Integer.parseInt(s.substring(2), 16);
49 ret = Integer.parseInt(s);
54 public static int fromHexString(String string) {
55 return Integer.parseInt(string, 16);
58 public static int from0xHexString(String string) {
59 String hex = string.substring(2);
60 return fromHexString(hex);
63 public static String getHexByte(byte b) {
64 return String.format("0x%02X", b & 0xFF);
67 public static String getHexByte(int b) {
68 return String.format("0x%02X", b);
71 public static class DataTypeParser {
72 public static Object parseDataType(DataType type, String val) {
75 return parseByte(val);
79 return parseFloat(val);
81 return parseAddress(val);
83 throw new IllegalArgumentException("Data Type not implemented in Field Value Parser!");
87 public static byte parseByte(@Nullable String val) {
88 if (val != null && !"".equals(val.trim())) {
89 return (byte) Utils.from0xHexString(val.trim());
95 public static int parseInt(@Nullable String val) {
96 if (val != null && !"".equals(val.trim())) {
97 return Integer.parseInt(val);
103 public static float parseFloat(@Nullable String val) {
104 if (val != null && !"".equals(val.trim())) {
105 return Float.parseFloat(val.trim());
111 public static InsteonAddress parseAddress(@Nullable String val) {
112 if (val != null && !"".equals(val.trim())) {
113 return InsteonAddress.parseAddress(val.trim());
115 return new InsteonAddress();
121 * Exception to indicate various xml parsing errors.
123 public static class ParsingException extends Exception {
124 private static final long serialVersionUID = 3997461423241843949L;
126 public ParsingException(String msg) {
130 public ParsingException(String msg, Throwable cause) {
135 public static String redactPassword(String port) {
136 return !port.startsWith("/hub2/") ? port : port.replaceAll(":\\w+@", ":******@");