2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.ipcamera.internal;
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;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
25 * The {@link Helper} class has static functions that help the IpCamera binding not need as many external libs.
28 * @author Matthew Skinner - Initial contribution
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 }.
37 * @author Matthew Skinner - Initial contribution
39 public static String searchString(String rawString, String searchedString) {
42 index = rawString.indexOf(searchedString);
43 if (index != -1) // -1 means "not found"
45 result = rawString.substring(index + searchedString.length(), rawString.length());
46 index = result.indexOf(',');
48 index = result.indexOf('"');
50 index = result.indexOf('}');
54 return result.substring(0, index);
57 return result.substring(0, index);
60 result = result.substring(0, index);
61 index = result.indexOf('"');
65 return result.substring(0, index);
72 public static String fetchXML(String message, String sectionHeading, String key) {
74 int sectionHeaderBeginning = 0;
75 if (!sectionHeading.isEmpty()) {// looking for a sectionHeading
76 sectionHeaderBeginning = message.indexOf(sectionHeading);
78 if (sectionHeaderBeginning == -1) {
81 int startIndex = message.indexOf(key, sectionHeaderBeginning + sectionHeading.length());
82 if (startIndex == -1) {
85 int endIndex = message.indexOf("<", startIndex + key.length());
86 if (endIndex > startIndex) {
87 result = message.substring(startIndex + key.length(), endIndex);
89 // remove any quotes and anything after the quote.
90 sectionHeaderBeginning = result.indexOf("\"");
91 if (sectionHeaderBeginning > 0) {
92 result = result.substring(0, sectionHeaderBeginning);
94 // remove any ">" and anything after it.
95 sectionHeaderBeginning = result.indexOf(">");
96 if (sectionHeaderBeginning > 0) {
97 result = result.substring(0, sectionHeaderBeginning);
99 if (!key.endsWith(">")) {
100 startIndex = result.indexOf(">");
101 if (startIndex != -1) {
102 return result.substring(startIndex + 1);
109 * The {@link encodeSpecialChars} Is used to replace spaces with %20 in Strings meant for URL queries.
111 * @author Matthew Skinner - Initial contribution
113 public static String encodeSpecialChars(String text) {
114 return URLEncoder.encode(text, StandardCharsets.UTF_8).replace("+", "%20");
117 public static String getLocalIpAddress() {
118 String ipAddress = "";
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();
132 } catch (SocketException ex) {