diff --git a/src/main/java/ch/mathieubroillet/leds/Led.java b/src/main/java/ch/mathieubroillet/leds/Led.java index cf5c3a4..9d025bf 100644 --- a/src/main/java/ch/mathieubroillet/leds/Led.java +++ b/src/main/java/ch/mathieubroillet/leds/Led.java @@ -1,6 +1,9 @@ package ch.mathieubroillet.leds; import ch.mathieubroillet.leds.enums.EnumLedsStatus; +import ch.mathieubroillet.leds.utils.Logger; + +import java.awt.*; public class Led { String ip; @@ -21,7 +24,7 @@ public class Led { return targetMachineIP; } - public boolean isTargetMachineIsOn() { + public boolean isTargetMachineOn() { return Utils.isDeviceConnectedToNetwork(getTargetMachineIP()); } @@ -29,7 +32,7 @@ public class Led { return name; } - public EnumLedsStatus getLedsStatus() { + public EnumLedsStatus getStatus() { return LedsUtils.getStatus(getIp()); } @@ -45,6 +48,18 @@ public class Led { LedsUtils.setStatus(getIp(), EnumLedsStatus.OFF); } + public Color getColor() { + return LedsUtils.getColor(getIp()); + } + + public void setColor(Color color) { + LedsUtils.setColor(getIp(), color); + } + + public void setColor(int r, int g, int b) { + LedsUtils.setColor(getIp(), new Color(r, g, b)); + } + public boolean isConnectedToNetwork() { return Utils.isDeviceConnectedToNetwork(getIp()); } diff --git a/src/main/java/ch/mathieubroillet/leds/LedsUtils.java b/src/main/java/ch/mathieubroillet/leds/LedsUtils.java index ed16817..2cb3732 100644 --- a/src/main/java/ch/mathieubroillet/leds/LedsUtils.java +++ b/src/main/java/ch/mathieubroillet/leds/LedsUtils.java @@ -4,20 +4,23 @@ import ch.mathieubroillet.leds.enums.EnumLedsStatus; import ch.mathieubroillet.leds.utils.Logger; import ch.mathieubroillet.leds.utils.OSValidator; +import java.awt.*; + public class LedsUtils { + private static final String COMMAND_BASE = (OSValidator.isWindows() ? "py" : "python3") + " -m flux_led "; public static EnumLedsStatus getStatus(String ip) { if (Utils.isDeviceConnectedToNetwork(ip)) { - String output = Utils.execCommandWithOutput(OSValidator.isWindows() ? "py" : "python" + " -m flux_led " + ip + " --info"); + String output = Utils.execCommandWithOutput(COMMAND_BASE + ip + " --info"); - if (output.split(" ")[3].equals("ON")) { + String[] infos = output.split(" "); + if (infos[3].equals("ON")) { return EnumLedsStatus.ON; - } else if (output.split(" ")[3].equals("OFF")){ + } else if (infos[3].equals("OFF")) { return EnumLedsStatus.OFF; } else { Logger.error(output); } - } return EnumLedsStatus.OFF; @@ -28,7 +31,40 @@ public class LedsUtils { } public static void setStatus(String ip, EnumLedsStatus enumLeds) { - Utils.execCommandWithOutput(OSValidator.isWindows() ? "py" : "python" + " -m flux_led " + ip + " --" + enumLeds.toString().toLowerCase()); + Utils.execCommandWithOutput(COMMAND_BASE + ip + " --" + enumLeds.toString().toLowerCase()); + } + public static void setColor(String ip, int r, int g, int b) { + setColor(ip, new Color(r, g, b)); + } + + public static void setColor(String ip, Color color) { + int r = color.getRed(); + int g = color.getGreen(); + int b = color.getBlue(); + Utils.execCommandWithOutput(COMMAND_BASE + ip + " -c" + r + "," + g + "," + b); + } + + public static Color getColor(String ip) { + if (Utils.isDeviceConnectedToNetwork(ip)) { + String output = Utils.execCommandWithOutput(COMMAND_BASE + ip + " --info"); + String[] infos = output.split(" "); + int r, g, b; + if (infos[5].equals("Color")) { + String red = infos[6].replaceFirst("\\(", "").replaceFirst(",", ""); + r = Utils.getIntegerFromString(red); + + String blue = infos[7].replaceFirst(",", ""); + b = Utils.getIntegerFromString(blue); + + String green = infos[8].replaceFirst("\\)", ""); + g = Utils.getIntegerFromString(green); + + return new Color(r, g, b); + } + + } + + return Color.WHITE; } }