Refactoring and added colors mananing.

This commit is contained in:
Mathieu 2020-10-10 10:28:59 +02:00
parent eb666e5ab1
commit 9562050d23
2 changed files with 58 additions and 7 deletions

View File

@ -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());
}

View File

@ -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;
}
}