Skip to content

Commit

Permalink
Adding effects: blink and rainbow.
Browse files Browse the repository at this point in the history
  • Loading branch information
robvanderleek committed Dec 22, 2013
1 parent 05f161b commit dd6b54a
Show file tree
Hide file tree
Showing 10 changed files with 369 additions and 321 deletions.
2 changes: 1 addition & 1 deletion install.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/sh
TARGET_DIR="$HOME/Desktop/"
mvn compile assembly:single
mvn clean compile assembly:single
cp target/*with-dependencies.jar $TARGET_DIR/jlifx.jar
./jar2sh "$TARGET_DIR/jlifx" $TARGET_DIR/jlifx.jar
echo "Launchable jar (jlifx.jar) copied to $TARGET_DIR"
Expand Down
85 changes: 48 additions & 37 deletions src/main/java/jlifx/bulb/Bulb.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,52 @@
import jlifx.packet.PacketService;

public class Bulb {
private final byte[] macAddress;
private final GatewayBulb gatewayBulb;

public Bulb(byte[] macAddress, GatewayBulb gatewayBulb) {
this.macAddress = macAddress;
this.gatewayBulb = gatewayBulb;
}

Bulb(byte[] macAddress) {
this.macAddress = macAddress;
this.gatewayBulb = null;
}

public byte[] getMacAddress() {
return macAddress;
}

public GatewayBulb getGatewayBulb() {
return gatewayBulb;
}

public String getMacAddressAsString() {
return String.format("%02x:%02x:%02x:%02x:%02x:%02x", macAddress[0], macAddress[1], macAddress[2],
macAddress[3], macAddress[4], macAddress[5]);
}

public void switchOn() throws IOException {
PacketService.sendPowerManagementPacket(this, true);
}

public void switchOff() throws IOException {
PacketService.sendPowerManagementPacket(this, false);
}

public void colorize(Color color) throws IOException {
PacketService.sendColorManagementPacket(this, color);
}
private final byte[] macAddress;
private final GatewayBulb gatewayBulb;
private String name;

public Bulb(byte[] macAddress, GatewayBulb gatewayBulb) {
this.macAddress = macAddress;
this.gatewayBulb = gatewayBulb;
}

Bulb(byte[] macAddress) {
this.macAddress = macAddress;
this.gatewayBulb = null;
}

public byte[] getMacAddress() {
return macAddress;
}

public GatewayBulb getGatewayBulb() {
return gatewayBulb;
}

public String getMacAddressAsString() {
return String.format("%02x:%02x:%02x:%02x:%02x:%02x", macAddress[0],
macAddress[1], macAddress[2], macAddress[3], macAddress[4],
macAddress[5]);
}

public void switchOn() throws IOException {
PacketService.sendPowerManagementPacket(this, true);
}

public void switchOff() throws IOException {
PacketService.sendPowerManagementPacket(this, false);
}

public void colorize(Color color) throws IOException {
PacketService.sendColorManagementPacket(this, color);
}

public String getName() {
return name;
}

public void setName(String bulbName) {
name = bulbName;
}

}
172 changes: 94 additions & 78 deletions src/main/java/jlifx/bulb/DiscoveryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,93 +14,109 @@

import jlifx.packet.Packet;
import jlifx.packet.PacketService;
import jlifx.packet.StatusResponsePacket;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class DiscoveryService {
public static final int PORT = 56700;
private static final Log LOG = LogFactory.getLog(DiscoveryService.class);
public static final int PORT = 56700;
private static final Log LOG = LogFactory.getLog(DiscoveryService.class);

public static GatewayBulb discoverGatewayBulb() {
List<InetAddress> networkBroadcastAddresses = getNetworkBroadcastAddresses();
for (InetAddress broadcastAddress : networkBroadcastAddresses) {
GatewayBulb result = discoverGatewayBulbOnNetwork(broadcastAddress);
if (result != null) {
return result;
}
}
return null;
}
public static GatewayBulb discoverGatewayBulb() {
List<InetAddress> networkBroadcastAddresses = getNetworkBroadcastAddresses();
for (InetAddress broadcastAddress : networkBroadcastAddresses) {
GatewayBulb result = discoverGatewayBulbOnNetwork(broadcastAddress);
if (result != null) {
return result;
}
}
return null;
}

private static GatewayBulb discoverGatewayBulbOnNetwork(InetAddress broadcastAddress) {
GatewayBulb result = null;
Packet packet = new Packet();
byte[] byteArray = packet.toByteArray();
try {
DatagramSocket socket = new DatagramSocket(PORT);
socket.setReuseAddress(true);
DatagramPacket datagramPacket = new DatagramPacket(byteArray, 0, byteArray.length, broadcastAddress, PORT);
socket.setSoTimeout(1000);
boolean gatewayResponse = false;
int retries = 3;
socket.send(datagramPacket);
while (!gatewayResponse && retries > 0) {
DatagramPacket answer = new DatagramPacket(byteArray, byteArray.length);
try {
socket.receive(answer);
} catch (SocketTimeoutException e) {
retries--;
continue;
}
InetAddress inetAddress = answer.getAddress();
if (inetAddress.isAnyLocalAddress() || inetAddress.isLoopbackAddress()
|| NetworkInterface.getByInetAddress(inetAddress) != null) {
retries--;
continue;
} else {
gatewayResponse = true;
Packet answerLifxPacket = Packet.fromDatagramPacket(answer);
result = new GatewayBulb(answer.getAddress(), answerLifxPacket.getGatewayMac());
}
}
socket.close();
} catch (SocketException e) {} catch (IOException e) {}
private static GatewayBulb discoverGatewayBulbOnNetwork(
InetAddress broadcastAddress) {
GatewayBulb result = null;
Packet packet = new Packet();
byte[] byteArray = packet.toByteArray();
try {
DatagramSocket socket = new DatagramSocket(PORT);
socket.setReuseAddress(true);
DatagramPacket datagramPacket = new DatagramPacket(byteArray, 0,
byteArray.length, broadcastAddress, PORT);
socket.setSoTimeout(1000);
boolean gatewayResponse = false;
int retries = 3;
socket.send(datagramPacket);
while (!gatewayResponse && retries > 0) {
DatagramPacket answer = new DatagramPacket(byteArray,
byteArray.length);
try {
socket.receive(answer);
} catch (SocketTimeoutException e) {
retries--;
continue;
}
InetAddress inetAddress = answer.getAddress();
if (inetAddress.isAnyLocalAddress()
|| inetAddress.isLoopbackAddress()
|| NetworkInterface.getByInetAddress(inetAddress) != null) {
retries--;
continue;
} else {
gatewayResponse = true;
Packet answerLifxPacket = Packet.fromDatagramPacket(answer);
result = new GatewayBulb(answer.getAddress(),
answerLifxPacket.getGatewayMac());
}
}
socket.close();
} catch (SocketException e) {
} catch (IOException e) {
}

return result;
}
return result;
}

public static List<Bulb> discoverAllBulbs(GatewayBulb gatewayBulb) throws IOException {
List<Bulb> result = new ArrayList<Bulb>();
List<Packet> sendStatusRequestPacket = PacketService.sendStatusRequestPacket(gatewayBulb);
for (Packet packet : sendStatusRequestPacket) {
result.add(new Bulb(packet.getTargetMac(), gatewayBulb));
}
return result;
}
public static List<Bulb> discoverAllBulbs(GatewayBulb gatewayBulb)
throws IOException {
List<Bulb> result = new ArrayList<Bulb>();
List<Packet> packets = PacketService
.sendStatusRequestPacket(gatewayBulb);
for (Packet packet : packets) {
StatusResponsePacket responsePacket = new StatusResponsePacket(
packet);
Bulb bulb = new Bulb(responsePacket.getTargetMac(), gatewayBulb);
bulb.setName(responsePacket.getBulbName());
result.add(bulb);
}
return result;
}

public static Bulb lookupBulb(byte[] macAddress) {
return new Bulb(macAddress, null);
}
public static Bulb lookupBulb(byte[] macAddress) {
return new Bulb(macAddress, null);
}

static List<InetAddress> getNetworkBroadcastAddresses() {
List<InetAddress> result = new ArrayList<InetAddress>();
try {
List<NetworkInterface> networkInterfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface networkInterface : networkInterfaces) {
if (!networkInterface.isLoopback() && networkInterface.isUp()) {
List<InterfaceAddress> interfaceAddresses = networkInterface.getInterfaceAddresses();
for (InterfaceAddress interfaceAddress : interfaceAddresses) {
InetAddress broadcast = interfaceAddress.getBroadcast();
if (broadcast != null) {
result.add(broadcast);
}
}
}
}
} catch (SocketException e) {
LOG.error("Could not retreive broadcast addresses from available network interfaces");
}
return result;
}
static List<InetAddress> getNetworkBroadcastAddresses() {
List<InetAddress> result = new ArrayList<InetAddress>();
try {
List<NetworkInterface> networkInterfaces = Collections
.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface networkInterface : networkInterfaces) {
if (!networkInterface.isLoopback() && networkInterface.isUp()) {
List<InterfaceAddress> interfaceAddresses = networkInterface
.getInterfaceAddresses();
for (InterfaceAddress interfaceAddress : interfaceAddresses) {
InetAddress broadcast = interfaceAddress.getBroadcast();
if (broadcast != null) {
result.add(broadcast);
}
}
}
}
} catch (SocketException e) {
LOG.error("Could not retreive broadcast addresses from available network interfaces");
}
return result;
}
}
37 changes: 24 additions & 13 deletions src/main/java/jlifx/commandline/AbstractBulbCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,32 @@
import jlifx.bulb.DiscoveryService;
import jlifx.bulb.GatewayBulb;

import org.apache.commons.lang3.ArrayUtils;

public abstract class AbstractBulbCommand implements CommandLineCommand {

public boolean execute(String[] args, PrintStream out) throws Exception {
if (args.length < 2) {
return false;
}
if (args[1].equalsIgnoreCase("all")) {
GatewayBulb gatewayBulb = DiscoveryService.discoverGatewayBulb();
return execute(DiscoveryService.discoverAllBulbs(gatewayBulb), args, out);
} else {
Bulb bulb = DiscoveryService.lookupBulb(Utils.parseMacAddress(args[1]));
return execute(Collections.singletonList(bulb), args, out);
}
}
public boolean execute(String[] args, PrintStream out) throws Exception {
if (args.length < 2) {
return false;
}
String[] commandArgs;
if (args.length < 3) {
commandArgs = new String[] {};
} else {
commandArgs = ArrayUtils.subarray(args, 2, args.length);
}
if (args[1].equalsIgnoreCase("all")) {
GatewayBulb gatewayBulb = DiscoveryService.discoverGatewayBulb();
return execute(DiscoveryService.discoverAllBulbs(gatewayBulb),
commandArgs, out);
} else {
Bulb bulb = DiscoveryService.lookupBulb(Utils
.parseMacAddress(args[1]));
return execute(Collections.singletonList(bulb), commandArgs, out);
}
}

public abstract boolean execute(List<Bulb> bulbs, String[] args, PrintStream out) throws Exception;
public abstract boolean execute(List<Bulb> bulbs, String[] commandArgs,
PrintStream out) throws Exception;

}
43 changes: 21 additions & 22 deletions src/main/java/jlifx/commandline/ColorCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,27 @@
import jlifx.bulb.Bulb;

public class ColorCommand extends AbstractBulbCommand {
public String getCommandName() {
return "color";
}

@Override
public boolean execute(List<Bulb> bulbs, String[] args, PrintStream out) throws Exception {
if (args.length != 3) {
return false;
} else {
Color color = Utils.stringToColor(args[2]);
if (color == null) {
return false;
} else {
colorizeBulbs(bulbs, color);
}
}
return true;
}
@Override
public boolean execute(List<Bulb> bulbs, String[] commandArgs,
PrintStream out) throws Exception {
if (commandArgs.length != 1) {
return false;
} else {
Color color = Utils.stringToColor(commandArgs[0]);
if (color == null) {
return false;
} else {
colorizeBulbs(bulbs, color);
}
}
return true;
}

private void colorizeBulbs(List<Bulb> bulbs, Color color) throws IOException {
for (Bulb bulb : bulbs) {
bulb.colorize(color);
}
}
private void colorizeBulbs(List<Bulb> bulbs, Color color)
throws IOException {
for (Bulb bulb : bulbs) {
bulb.colorize(color);
}
}
}
4 changes: 1 addition & 3 deletions src/main/java/jlifx/commandline/CommandLineCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,5 @@
import java.io.PrintStream;

public interface CommandLineCommand {
String getCommandName();

boolean execute(String[] args, PrintStream out) throws Exception;
boolean execute(String[] args, PrintStream out) throws Exception;
}
Loading

0 comments on commit dd6b54a

Please sign in to comment.