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