]> git.basschouten.com Git - openhab-addons.git/blob
8ec905ddb25bd52ddcb8fb0b9281d2d83f781874
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.insteon.internal.utils;
14
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;
19
20 /**
21  * Various utility functions for e.g. hex string parsing
22  *
23  * @author Daniel Pfrommer - Initial contribution
24  * @author Rob Nielsen - Port to openHAB 2 insteon binding
25  */
26 @NonNullByDefault
27 public class Utils {
28     public static String getHexString(int b) {
29         String result = String.format("%02X", b & 0xFF);
30         return result;
31     }
32
33     public static String getHexString(byte[] b) {
34         return getHexString(b, b.length);
35     }
36
37     public static String getHexString(byte[] b, int len) {
38         String result = "";
39         for (int i = 0; i < b.length && i < len; i++) {
40             result += String.format("%02X ", b[i] & 0xFF);
41         }
42         return result;
43     }
44
45     public static int strToInt(String s) throws NumberFormatException {
46         int ret = -1;
47         if (s.startsWith("0x")) {
48             ret = Integer.parseInt(s.substring(2), 16);
49         } else {
50             ret = Integer.parseInt(s);
51         }
52         return (ret);
53     }
54
55     public static int fromHexString(String string) {
56         return Integer.parseInt(string, 16);
57     }
58
59     public static int from0xHexString(String string) {
60         String hex = string.substring(2);
61         return fromHexString(hex);
62     }
63
64     public static String getHexByte(byte b) {
65         return String.format("0x%02X", b & 0xFF);
66     }
67
68     public static String getHexByte(int b) {
69         return String.format("0x%02X", b);
70     }
71
72     public static class DataTypeParser {
73         public static Object parseDataType(DataType type, String val) {
74             switch (type) {
75                 case BYTE:
76                     return parseByte(val);
77                 case INT:
78                     return parseInt(val);
79                 case FLOAT:
80                     return parseFloat(val);
81                 case ADDRESS:
82                     return parseAddress(val);
83                 default:
84                     throw new IllegalArgumentException("Data Type not implemented in Field Value Parser!");
85             }
86         }
87
88         public static byte parseByte(@Nullable String val) {
89             if (val != null && !val.trim().equals("")) {
90                 return (byte) Utils.from0xHexString(val.trim());
91             } else {
92                 return 0x00;
93             }
94         }
95
96         public static int parseInt(@Nullable String val) {
97             if (val != null && !val.trim().equals("")) {
98                 return Integer.parseInt(val);
99             } else {
100                 return 0x00;
101             }
102         }
103
104         public static float parseFloat(@Nullable String val) {
105             if (val != null && !val.trim().equals("")) {
106                 return Float.parseFloat(val.trim());
107             } else {
108                 return 0;
109             }
110         }
111
112         public static InsteonAddress parseAddress(@Nullable String val) {
113             if (val != null && !val.trim().equals("")) {
114                 return InsteonAddress.parseAddress(val.trim());
115             } else {
116                 return new InsteonAddress();
117             }
118         }
119     }
120
121     /**
122      * Exception to indicate various xml parsing errors.
123      */
124     public static class ParsingException extends Exception {
125         private static final long serialVersionUID = 3997461423241843949L;
126
127         public ParsingException(String msg) {
128             super(msg);
129         }
130
131         public ParsingException(String msg, Throwable cause) {
132             super(msg, cause);
133         }
134     }
135
136     public static String redactPassword(String port) {
137         return !port.startsWith("/hub2/") ? port : port.replaceAll(":\\w+@", ":******@");
138     }
139 }