Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[lgwebos] Refactor usages of deprecated methods #18082

Merged
merged 1 commit into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
*/
package org.openhab.binding.lgwebos.internal;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
Expand All @@ -23,6 +25,7 @@
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.eclipse.jdt.annotation.NonNullByDefault;
Expand Down Expand Up @@ -170,7 +173,16 @@ private static byte[] getWOLPackage(String macStr) throws IllegalArgumentExcepti

private static boolean checkIfLinuxCommandExists(String cmd) {
try {
return 0 == Runtime.getRuntime().exec(String.format("which %s", cmd)).waitFor();
Process process = new ProcessBuilder("which", cmd).redirectErrorStream(true).start();

if (LOGGER.isDebugEnabled()) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
String output = reader.lines().collect(Collectors.joining("\n"));
LOGGER.debug("Command 'which {}' returned {}", cmd, output);
}
}

return process.waitFor() == 0;
} catch (InterruptedException | IOException e) {
LOGGER.debug("Error trying to check if command {} exists: {}", cmd, e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Collections;
Expand Down Expand Up @@ -100,11 +101,11 @@ public void showToast(
public void showToast(
@ActionInput(name = "icon", label = "@text/actionShowToastInputIconLabel", description = "@text/actionShowToastInputIconDesc") String icon,
@ActionInput(name = "text", label = "@text/actionShowToastInputTextLabel", description = "@text/actionShowToastInputTextDesc") String text)
throws IOException {
BufferedImage bi = ImageIO.read(new URL(icon));
throws IOException, URISyntaxException {
BufferedImage bi = ImageIO.read(new URI(icon).toURL());
try (ByteArrayOutputStream os = new ByteArrayOutputStream(); OutputStream b64 = Base64.getEncoder().wrap(os)) {
ImageIO.write(bi, "png", b64);
String string = os.toString(StandardCharsets.UTF_8.name());
String string = os.toString(StandardCharsets.UTF_8);
getConnectedSocket().ifPresent(control -> control.showToast(text, string, "png", createResponseListener()));
}
}
Expand Down Expand Up @@ -261,7 +262,8 @@ public static void showToast(ThingActions actions, String text) throws IOExcepti
((LGWebOSActions) actions).showToast(text);
}

public static void showToast(ThingActions actions, String icon, String text) throws IOException {
public static void showToast(ThingActions actions, String icon, String text)
throws IOException, URISyntaxException {
((LGWebOSActions) actions).showToast(icon, text);
}

Expand Down