2 * Copyright (c) 2010-2020 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
27 @SuppressWarnings("null")
29 public static String getHexString(int b) {
30 String result = String.format("%02X", b & 0xFF);
34 public static String getHexString(byte @Nullable [] b) {
35 return getHexString(b, b.length);
38 public static String getHexString(byte[] b, int len) {
40 for (int i = 0; i < b.length && i < len; i++) {
41 result += String.format("%02X ", b[i] & 0xFF);
46 public static int strToInt(String s) throws NumberFormatException {
48 if (s.startsWith("0x")) {
49 ret = Integer.parseInt(s.substring(2), 16);
51 ret = Integer.parseInt(s);
56 public static int fromHexString(String string) {
57 return Integer.parseInt(string, 16);
60 public static int from0xHexString(String string) {
61 String hex = string.substring(2);
62 return fromHexString(hex);
65 public static String getHexByte(byte b) {
66 return String.format("0x%02X", b & 0xFF);
69 public static String getHexByte(int b) {
70 return String.format("0x%02X", b);
74 public static class DataTypeParser {
75 public static Object parseDataType(@Nullable DataType type, String val) {
78 return parseByte(val);
82 return parseFloat(val);
84 return parseAddress(val);
86 throw new IllegalArgumentException("Data Type not implemented in Field Value Parser!");
90 public static byte parseByte(@Nullable String val) {
91 if (val != null && !val.trim().equals("")) {
92 return (byte) Utils.from0xHexString(val.trim());
98 public static int parseInt(@Nullable String val) {
99 if (val != null && !val.trim().equals("")) {
100 return Integer.parseInt(val);
106 public static float parseFloat(@Nullable String val) {
107 if (val != null && !val.trim().equals("")) {
108 return Float.parseFloat(val.trim());
114 public static InsteonAddress parseAddress(@Nullable String val) {
115 if (val != null && !val.trim().equals("")) {
116 return InsteonAddress.parseAddress(val.trim());
118 return new InsteonAddress();
124 * Exception to indicate various xml parsing errors.
127 public static class ParsingException extends Exception {
128 private static final long serialVersionUID = 3997461423241843949L;
130 public ParsingException(String msg) {
134 public ParsingException(String msg, Throwable cause) {
139 public static String redactPassword(String port) {
140 return !port.startsWith("/hub2/") ? port : port.replaceAll(":\\w+@", ":******@");