]> git.basschouten.com Git - openhab-addons.git/blob
effbae452fcf036667874a3f59d5bf157ab8f199
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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         return String.format("%02X", b & 0xFF);
30     }
31
32     public static String getHexString(byte[] b) {
33         return getHexString(b, b.length);
34     }
35
36     public static String getHexString(byte[] b, int len) {
37         String result = "";
38         for (int i = 0; i < b.length && i < len; i++) {
39             result += String.format("%02X ", b[i] & 0xFF);
40         }
41         return result;
42     }
43
44     public static int strToInt(String s) throws NumberFormatException {
45         int ret = -1;
46         if (s.startsWith("0x")) {
47             ret = Integer.parseInt(s.substring(2), 16);
48         } else {
49             ret = Integer.parseInt(s);
50         }
51         return (ret);
52     }
53
54     public static int fromHexString(String string) {
55         return Integer.parseInt(string, 16);
56     }
57
58     public static int from0xHexString(String string) {
59         String hex = string.substring(2);
60         return fromHexString(hex);
61     }
62
63     public static String getHexByte(byte b) {
64         return String.format("0x%02X", b & 0xFF);
65     }
66
67     public static String getHexByte(int b) {
68         return String.format("0x%02X", b);
69     }
70
71     public static class DataTypeParser {
72         public static Object parseDataType(DataType type, String val) {
73             switch (type) {
74                 case BYTE:
75                     return parseByte(val);
76                 case INT:
77                     return parseInt(val);
78                 case FLOAT:
79                     return parseFloat(val);
80                 case ADDRESS:
81                     return parseAddress(val);
82                 default:
83                     throw new IllegalArgumentException("Data Type not implemented in Field Value Parser!");
84             }
85         }
86
87         public static byte parseByte(@Nullable String val) {
88             if (val != null && !"".equals(val.trim())) {
89                 return (byte) Utils.from0xHexString(val.trim());
90             } else {
91                 return 0x00;
92             }
93         }
94
95         public static int parseInt(@Nullable String val) {
96             if (val != null && !"".equals(val.trim())) {
97                 return Integer.parseInt(val);
98             } else {
99                 return 0x00;
100             }
101         }
102
103         public static float parseFloat(@Nullable String val) {
104             if (val != null && !"".equals(val.trim())) {
105                 return Float.parseFloat(val.trim());
106             } else {
107                 return 0;
108             }
109         }
110
111         public static InsteonAddress parseAddress(@Nullable String val) {
112             if (val != null && !"".equals(val.trim())) {
113                 return InsteonAddress.parseAddress(val.trim());
114             } else {
115                 return new InsteonAddress();
116             }
117         }
118     }
119
120     /**
121      * Exception to indicate various xml parsing errors.
122      */
123     public static class ParsingException extends Exception {
124         private static final long serialVersionUID = 3997461423241843949L;
125
126         public ParsingException(String msg) {
127             super(msg);
128         }
129
130         public ParsingException(String msg, Throwable cause) {
131             super(msg, cause);
132         }
133     }
134
135     public static String redactPassword(String port) {
136         return !port.startsWith("/hub2/") ? port : port.replaceAll(":\\w+@", ":******@");
137     }
138 }