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.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 String result = String.format("%02X", b & 0xFF);
33 public static String getHexString(byte[] b) {
34 return getHexString(b, b.length);
37 public static String getHexString(byte[] b, int len) {
39 for (int i = 0; i < b.length && i < len; i++) {
40 result += String.format("%02X ", b[i] & 0xFF);
45 public static int strToInt(String s) throws NumberFormatException {
47 if (s.startsWith("0x")) {
48 ret = Integer.parseInt(s.substring(2), 16);
50 ret = Integer.parseInt(s);
55 public static int fromHexString(String string) {
56 return Integer.parseInt(string, 16);
59 public static int from0xHexString(String string) {
60 String hex = string.substring(2);
61 return fromHexString(hex);
64 public static String getHexByte(byte b) {
65 return String.format("0x%02X", b & 0xFF);
68 public static String getHexByte(int b) {
69 return String.format("0x%02X", b);
72 public static class DataTypeParser {
73 public static Object parseDataType(DataType type, String val) {
76 return parseByte(val);
80 return parseFloat(val);
82 return parseAddress(val);
84 throw new IllegalArgumentException("Data Type not implemented in Field Value Parser!");
88 public static byte parseByte(@Nullable String val) {
89 if (val != null && !val.trim().equals("")) {
90 return (byte) Utils.from0xHexString(val.trim());
96 public static int parseInt(@Nullable String val) {
97 if (val != null && !val.trim().equals("")) {
98 return Integer.parseInt(val);
104 public static float parseFloat(@Nullable String val) {
105 if (val != null && !val.trim().equals("")) {
106 return Float.parseFloat(val.trim());
112 public static InsteonAddress parseAddress(@Nullable String val) {
113 if (val != null && !val.trim().equals("")) {
114 return InsteonAddress.parseAddress(val.trim());
116 return new InsteonAddress();
122 * Exception to indicate various xml parsing errors.
124 public static class ParsingException extends Exception {
125 private static final long serialVersionUID = 3997461423241843949L;
127 public ParsingException(String msg) {
131 public ParsingException(String msg, Throwable cause) {
136 public static String redactPassword(String port) {
137 return !port.startsWith("/hub2/") ? port : port.replaceAll(":\\w+@", ":******@");