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