]> git.basschouten.com Git - openhab-addons.git/blob
1a2e641e7245c0bfc24c200abcaf6b31bffa6393
[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.ipcamera.internal;
14
15 import java.net.InetAddress;
16 import java.net.NetworkInterface;
17 import java.net.SocketException;
18 import java.net.URLEncoder;
19 import java.nio.charset.StandardCharsets;
20 import java.util.Enumeration;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23
24 /**
25  * The {@link Helper} class has static functions that help the IpCamera binding not need as many external libs.
26  *
27  *
28  * @author Matthew Skinner - Initial contribution
29  */
30 @NonNullByDefault
31 public class Helper {
32
33     /**
34      * The {@link searchString} Used to grab values out of JSON or other quote encapsulated structures without needing
35      * an external lib. String may be terminated by ," or }.
36      *
37      * @author Matthew Skinner - Initial contribution
38      */
39     public static String searchString(String rawString, String searchedString) {
40         String result = "";
41         int index = 0;
42         index = rawString.indexOf(searchedString);
43         if (index != -1) // -1 means "not found"
44         {
45             result = rawString.substring(index + searchedString.length(), rawString.length());
46             index = result.indexOf(',');
47             if (index == -1) {
48                 index = result.indexOf('"');
49                 if (index == -1) {
50                     index = result.indexOf('}');
51                     if (index == -1) {
52                         return result;
53                     } else {
54                         return result.substring(0, index);
55                     }
56                 } else {
57                     return result.substring(0, index);
58                 }
59             } else {
60                 result = result.substring(0, index);
61                 index = result.indexOf('"');
62                 if (index == -1) {
63                     return result;
64                 } else {
65                     return result.substring(0, index);
66                 }
67             }
68         }
69         return "";
70     }
71
72     public static String fetchXML(String message, String sectionHeading, String key) {
73         String result = "";
74         int sectionHeaderBeginning = 0;
75         if (!sectionHeading.isEmpty()) {// looking for a sectionHeading
76             sectionHeaderBeginning = message.indexOf(sectionHeading);
77         }
78         if (sectionHeaderBeginning == -1) {
79             return "";
80         }
81         int startIndex = message.indexOf(key, sectionHeaderBeginning + sectionHeading.length());
82         if (startIndex == -1) {
83             return "";
84         }
85         int endIndex = message.indexOf("<", startIndex + key.length());
86         if (endIndex > startIndex) {
87             result = message.substring(startIndex + key.length(), endIndex);
88         }
89         // remove any quotes and anything after the quote.
90         sectionHeaderBeginning = result.indexOf("\"");
91         if (sectionHeaderBeginning > 0) {
92             result = result.substring(0, sectionHeaderBeginning);
93         }
94         // remove any ">" and anything after it.
95         sectionHeaderBeginning = result.indexOf(">");
96         if (sectionHeaderBeginning > 0) {
97             result = result.substring(0, sectionHeaderBeginning);
98         }
99         if (!key.endsWith(">")) {
100             startIndex = result.indexOf(">");
101             if (startIndex != -1) {
102                 return result.substring(startIndex + 1);
103             }
104         }
105         return result;
106     }
107
108     /**
109      * The {@link encodeSpecialChars} Is used to replace spaces with %20 in Strings meant for URL queries.
110      *
111      * @author Matthew Skinner - Initial contribution
112      */
113     public static String encodeSpecialChars(String text) {
114         return URLEncoder.encode(text, StandardCharsets.UTF_8).replace("+", "%20");
115     }
116
117     public static String getLocalIpAddress() {
118         String ipAddress = "";
119         try {
120             for (Enumeration<NetworkInterface> enumNetworks = NetworkInterface.getNetworkInterfaces(); enumNetworks
121                     .hasMoreElements();) {
122                 NetworkInterface networkInterface = enumNetworks.nextElement();
123                 for (Enumeration<InetAddress> enumIpAddr = networkInterface.getInetAddresses(); enumIpAddr
124                         .hasMoreElements();) {
125                     InetAddress inetAddress = enumIpAddr.nextElement();
126                     if (!inetAddress.isLoopbackAddress() && inetAddress.getHostAddress().toString().length() < 18
127                             && inetAddress.isSiteLocalAddress()) {
128                         ipAddress = inetAddress.getHostAddress().toString();
129                     }
130                 }
131             }
132         } catch (SocketException ex) {
133         }
134         return ipAddress;
135     }
136 }