From: rtrimana Date: Mon, 12 Dec 2016 16:43:33 +0000 (-0800) Subject: Adding LifxLightBulb files and placeholders for benchmarks; preparing for porting X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=9192c68e9a6aa5a8845b20d9f74c0a8517e4341f;hp=7a27eab091d560ca1222d3a2652da56c97456980;p=iot2.git Adding LifxLightBulb files and placeholders for benchmarks; preparing for porting --- diff --git a/benchmarks/Makefile b/benchmarks/Makefile new file mode 100644 index 0000000..dac207c --- /dev/null +++ b/benchmarks/Makefile @@ -0,0 +1,39 @@ +BASE := .. +BOOFDIR := ./libs/boofcv_libs +BOOFJARS := $(BOOFDIR)/BoofCV-feature-0.21.jar:$(BOOFDIR)/BoofCV-io-0.21.jar:$(BOOFDIR)/BoofCV-visualize-0.21.jar:$(BOOFDIR)/BoofCV-ip-0.21.jar + +include $(BASE)/common.mk + +all: interfaces annotation drivers Lifxtest SmartLights Irrigation Speaker + +nocheck: interfaces annotation nocheckdrivers nocheckLifxtest nocheckSmartLights nocheckIrrigation nocheckSpeaker nocheckBrillotest + +PHONY += interfaces +interfaces: + $(MAKE) -C interfaces + +PHONY += annotation +annotation: + $(JAVAC) -d $(BIN_DIR) -classpath $(BIN_DIR):. annotation/*java + +PHONY += drivers +drivers: + $(MAKE) -C drivers + +PHONY += Lifxtest +Lifxtest: + $(MAKE) -C Lifxtest + +PHONY += SmartLights +SmartLights: + $(MAKE) -C SmartLightsController + +PHONY += Irrigation +Irrigation: + $(MAKE) -C IrrigationController + +PHONY += Speaker +Speaker: + $(MAKE) -C SpeakerController + +.PHONY: $(PHONY) diff --git a/benchmarks/drivers/LifxLightBulb/BulbColor.java b/benchmarks/drivers/LifxLightBulb/BulbColor.java new file mode 100644 index 0000000..e069e74 --- /dev/null +++ b/benchmarks/drivers/LifxLightBulb/BulbColor.java @@ -0,0 +1,67 @@ +package iotcode.LifxLightBulb; + +import java.security.InvalidParameterException; + +public class BulbColor { + + private int hue; + private int saturation; + private int brightness; + private int kelvin; + + public BulbColor(int _hue, int _saturation, int _brightness, int _kelvin) { + + if ((hue > 65535) || (hue < 0)) { + throw new InvalidParameterException("BulbColor: Invalid parameter value for _hue (0-65535)"); + } + + if ((saturation > 65535) || (saturation < 0)) { + throw new InvalidParameterException("BulbColor: Invalid parameter value for _saturation (0-65535)"); + } + + if ((brightness > 65535) || (brightness < 0)) { + throw new InvalidParameterException("BulbColor: Invalid parameter value for _brightness (0-65535)"); + } + + if ((kelvin > 65535) || (kelvin < 0)) { + throw new InvalidParameterException("BulbColor: Invalid parameter value for _kelvin (0-65535)"); + } + + hue = _hue; + saturation = _saturation; + brightness = _brightness; + kelvin = _kelvin; + } + + public BulbColor(byte[] data) { + hue = ((data[1] & 0xFF) << 8); + hue |= (data[0] & 0xFF); + + saturation = ((data[3] & 0xFF) << 8); + saturation |= (data[2] & 0xFF); + + brightness = ((data[5] & 0xFF) << 8); + brightness |= (data[4] & 0xFF); + + kelvin = ((data[7] & 0xFF) << 8); + kelvin |= (data[6] & 0xFF); + } + + public int getHue() { + return hue; + } + + public int getSaturation() { + return saturation; + } + + public int getBrightness() { + return brightness; + } + + public int getKelvin() { + return kelvin; + } +} + + diff --git a/benchmarks/drivers/LifxLightBulb/DeviceStateGroup.java b/benchmarks/drivers/LifxLightBulb/DeviceStateGroup.java new file mode 100644 index 0000000..9ed6c91 --- /dev/null +++ b/benchmarks/drivers/LifxLightBulb/DeviceStateGroup.java @@ -0,0 +1,25 @@ +package iotcode.LifxLightBulb; + +public class DeviceStateGroup { + byte[] group = new byte[16]; + final String label; + final long updatedAt; + + public DeviceStateGroup(byte[] _location, String _label, long _updatedAt) { + group = _location; + label = _label; + updatedAt = _updatedAt; + } + + public byte[] getGroup() { + return group; + } + + public String getLabel() { + return label; + } + + public long getUpdatedAt() { + return updatedAt; + } +} diff --git a/benchmarks/drivers/LifxLightBulb/DeviceStateHostFirmware.java b/benchmarks/drivers/LifxLightBulb/DeviceStateHostFirmware.java new file mode 100644 index 0000000..4447ca0 --- /dev/null +++ b/benchmarks/drivers/LifxLightBulb/DeviceStateHostFirmware.java @@ -0,0 +1,21 @@ +package iotcode.LifxLightBulb; + +public class DeviceStateHostFirmware { + // time of build in nanosecond accuracy + // after some tests + final long build; + final long version; // firmware version + + public DeviceStateHostFirmware(long _build, long _version) { + build = _build; + version = _version; + } + + public long getBuild() { + return build; + } + + public long getVersion() { + return version; + } +} diff --git a/benchmarks/drivers/LifxLightBulb/DeviceStateHostInfo.java b/benchmarks/drivers/LifxLightBulb/DeviceStateHostInfo.java new file mode 100644 index 0000000..a10213d --- /dev/null +++ b/benchmarks/drivers/LifxLightBulb/DeviceStateHostInfo.java @@ -0,0 +1,25 @@ +package iotcode.LifxLightBulb; + +public class DeviceStateHostInfo { + final long signal; + final long tx; + final long rx; + + public DeviceStateHostInfo(long _signal, long _tx, long _rx) { + signal = _signal; + tx = _tx; + rx = _rx; + } + + public long getSignal() { + return signal; + } + + public long getTx() { + return tx; + } + + public long getRx() { + return rx; + } +} diff --git a/benchmarks/drivers/LifxLightBulb/DeviceStateInfo.java b/benchmarks/drivers/LifxLightBulb/DeviceStateInfo.java new file mode 100644 index 0000000..6f8e397 --- /dev/null +++ b/benchmarks/drivers/LifxLightBulb/DeviceStateInfo.java @@ -0,0 +1,26 @@ +package iotcode.LifxLightBulb; + +public class DeviceStateInfo { + // all values are in nanoseconds + private final long time; + private final long upTime; + private final long downTime; + + public DeviceStateInfo(long _time, long _upTime, long _downTime) { + time = _time; + upTime = _upTime; + downTime = _downTime; + } + + public long getTime() { + return time; + } + + public long getUpTime() { + return upTime; + } + + public long getDownTime() { + return downTime; + } +} diff --git a/benchmarks/drivers/LifxLightBulb/DeviceStateLocation.java b/benchmarks/drivers/LifxLightBulb/DeviceStateLocation.java new file mode 100644 index 0000000..e5d3e08 --- /dev/null +++ b/benchmarks/drivers/LifxLightBulb/DeviceStateLocation.java @@ -0,0 +1,25 @@ +package iotcode.LifxLightBulb; + +public class DeviceStateLocation { + byte[] location = new byte[16]; + final String label; + final long updatedAt; + + public DeviceStateLocation(byte[] _location, String _label, long _updatedAt) { + location = _location; + label = _label; + updatedAt = _updatedAt; + } + + public byte[] getLocation() { + return location; + } + + public String getLabel() { + return label; + } + + public long getUpdatedAt() { + return updatedAt; + } +} diff --git a/benchmarks/drivers/LifxLightBulb/DeviceStateService.java b/benchmarks/drivers/LifxLightBulb/DeviceStateService.java new file mode 100644 index 0000000..22f34ac --- /dev/null +++ b/benchmarks/drivers/LifxLightBulb/DeviceStateService.java @@ -0,0 +1,19 @@ +package iotcode.LifxLightBulb; + +public class DeviceStateService { + private final int service; + private final long port; + + public DeviceStateService(int _service, long _port) { + service = _service; + port = _port; + } + + public int getService() { + return service; + } + + public long getPort() { + return port; + } +} diff --git a/benchmarks/drivers/LifxLightBulb/DeviceStateVersion.java b/benchmarks/drivers/LifxLightBulb/DeviceStateVersion.java new file mode 100644 index 0000000..f2f0200 --- /dev/null +++ b/benchmarks/drivers/LifxLightBulb/DeviceStateVersion.java @@ -0,0 +1,25 @@ +package iotcode.LifxLightBulb; + +public class DeviceStateVersion { + final long vender; + final long product; + final long version; + + public DeviceStateVersion(long _vender, long _product, long _version) { + vender = _vender; + product = _product; + version = _version; + } + + public long getVender() { + return vender; + } + + public long getProduct() { + return product; + } + + public long getVersion() { + return version; + } +} diff --git a/benchmarks/drivers/LifxLightBulb/DeviceStateWifiFirmware.java b/benchmarks/drivers/LifxLightBulb/DeviceStateWifiFirmware.java new file mode 100644 index 0000000..50bafe9 --- /dev/null +++ b/benchmarks/drivers/LifxLightBulb/DeviceStateWifiFirmware.java @@ -0,0 +1,21 @@ +package iotcode.LifxLightBulb; + +public class DeviceStateWifiFirmware { + // time of build in nanosecond accuracy + // after some tests + final long build; + final long version; // firmware version + + public DeviceStateWifiFirmware(long _build, long _version) { + build = _build; + version = _version; + } + + public long getBuild() { + return build; + } + + public long getVersion() { + return version; + } +} diff --git a/benchmarks/drivers/LifxLightBulb/DeviceStateWifiInfo.java b/benchmarks/drivers/LifxLightBulb/DeviceStateWifiInfo.java new file mode 100644 index 0000000..b2d8e02 --- /dev/null +++ b/benchmarks/drivers/LifxLightBulb/DeviceStateWifiInfo.java @@ -0,0 +1,25 @@ +package iotcode.LifxLightBulb; + +public class DeviceStateWifiInfo { + final long signal; + final long tx; + final long rx; + + public DeviceStateWifiInfo(long _signal, long _tx, long _rx) { + signal = _signal; + tx = _tx; + rx = _rx; + } + + public long getSignal() { + return signal; + } + + public long getTx() { + return tx; + } + + public long getRx() { + return rx; + } +} diff --git a/benchmarks/drivers/LifxLightBulb/LifxHeader.java b/benchmarks/drivers/LifxLightBulb/LifxHeader.java new file mode 100644 index 0000000..70d314f --- /dev/null +++ b/benchmarks/drivers/LifxLightBulb/LifxHeader.java @@ -0,0 +1,263 @@ +package iotcode.LifxLightBulb; + +import java.security.InvalidParameterException; + +public class LifxHeader { + // Frame Variables + private int size; + private int origin; + private boolean tagged; + private boolean addressable; + private int protocol; + private long source; + + //Frame Adress Variables + private byte[] macAddress = new byte[8]; + private boolean ack_required; + private boolean res_required; + private int sequence; + + //Protocol Header + private int type; + + public LifxHeader() { + // needed values as per spec + origin = 0; + addressable = true; + protocol = 1024; + } + + public void setSize(int _size) { + if (_size < 0) { + throw new InvalidParameterException("Header: size cannot be less than 0"); + } else if (_size > 65535) { + throw new InvalidParameterException("Header: size to large"); + } + size = _size; + } + + public void setOrigin(int _origin) { + if (_origin < 0) { + throw new InvalidParameterException("Header: origin cannot be less than 0"); + } else if (_origin > 3) { + throw new InvalidParameterException("Header: origin to large"); + } + + origin = _origin; + } + + public void setTagged(boolean _tagged) { + tagged = _tagged; + } + + public void setAddressable(boolean _addressable) { + addressable = _addressable; + } + + public void setProtocol(int _protocol) { + if (_protocol < 0) { + throw new InvalidParameterException("Header: protocol cannot be less than 0"); + } else if (_protocol > 4095) { + throw new InvalidParameterException("Header: protocol to large"); + } + + protocol = _protocol; + } + + public void setSource(long _source) { + if (_source < 0) { + throw new InvalidParameterException("Header: source cannot be less than 0"); + } else if (_source > (long)4294967295l) { + throw new InvalidParameterException("Header: source to large"); + } + source = _source; + } + + public void setSequence(int _sequence) { + if (_sequence < 0) { + throw new InvalidParameterException("Header: sequence cannot be less than 0"); + } else if (_sequence > 255) { + throw new InvalidParameterException("Header: sequence to large"); + } + sequence = _sequence; + } + + public void setType(int _type) { + if (_type < 0) { + throw new InvalidParameterException("Header: type cannot be less than 0"); + } else if (_type > 65535) { + throw new InvalidParameterException("Header: type to large"); + } + type = _type; + } + + public void setAck_required(boolean _ack_required) { + ack_required = _ack_required; + } + + public void setRes_required(boolean _res_required) { + res_required = _res_required; + } + + public void setMacAddress(byte[] _macAddress) { + macAddress = _macAddress; + } + + public int getSize() { + return size; + } + + public int getOrigin() { + return origin; + } + + public boolean getTagged() { + return tagged; + } + + public boolean getAddressable() { + return addressable; + } + + public int getProtocol() { + return protocol; + } + + public long getSource() { + return source; + } + + public int getSequence() { + return sequence; + } + + public int getType() { + return type; + } + + public byte[] getMacAddress() { + return macAddress; + } + + public boolean getAck_required() { + return ack_required; + } + + public boolean getRes_required() { + return res_required; + } + + public byte[] getHeaderBytes() { + byte[] headerBytes = new byte[36]; + headerBytes[0] = (byte)(size & 0xFF); + headerBytes[1] = (byte)((size >> 8) & 0xFF); + + + headerBytes[2] = (byte)(protocol & 0xFF); + headerBytes[3] = (byte)((protocol >> 8) & 0x0F); + + headerBytes[3] |= (byte)((origin & 0x03) << 6); + + if (tagged) { + headerBytes[3] |= (1 << 5); + } + + if (addressable) { + headerBytes[3] |= (1 << 4); + } + + headerBytes[4] = (byte)((source >> 0) & 0xFF); + headerBytes[5] = (byte)((source >> 8) & 0xFF); + headerBytes[6] = (byte)((source >> 16) & 0xFF); + headerBytes[7] = (byte)((source >> 24) & 0xFF); + + + // fix in a bit + headerBytes[8] = macAddress[0]; + headerBytes[9] = macAddress[1]; + headerBytes[10] = macAddress[2]; + headerBytes[11] = macAddress[3]; + headerBytes[12] = macAddress[4]; + headerBytes[13] = macAddress[5]; + headerBytes[14] = macAddress[6]; + headerBytes[15] = macAddress[7]; + + // Reserved and set to 0 + // headerBytes[16] = 0; + // headerBytes[17] = 0; + // headerBytes[18] = 0; + // headerBytes[19] = 0; + // headerBytes[20] = 0; + // headerBytes[21] = 0; + + if (ack_required) { + headerBytes[22] = (1 << 1); + } + + if (res_required) { + headerBytes[22] |= (1); + } + + headerBytes[23] = (byte)(sequence & 0xFF); + + // Reserved and set to 0 + //headerBytes[24] = 0; + //headerBytes[25] = 0; + //headerBytes[26] = 0; + //headerBytes[27] = 0; + //headerBytes[28] = 0; + //headerBytes[29] = 0; + //headerBytes[30] = 0; + //headerBytes[31] = 0; + + headerBytes[32] = (byte)((type >> 0) & 0xFF); + headerBytes[33] = (byte)((type >> 8) & 0xFF); + + // Reserved and set to 0 + //headerBytes[34] = 0; + //headerBytes[35] = 0; + + return headerBytes; + } + + public void setFromBytes(byte[] dataBytes) { + if (dataBytes.length != 36) { + throw new InvalidParameterException("Header: invalid number of bytes"); + } + + size = dataBytes[0] & 0xFF; + size |= ((dataBytes[1] & 0xFF) << 8); + size &= 0xFFFF; + + origin = (dataBytes[3] >> 6) & 0x03; + tagged = ((dataBytes[3] >> 5) & 0x01) == 1; + addressable = ((dataBytes[3] >> 4) & 0x01) == 1; + + + protocol = (dataBytes[3] & 0x0F) << 8; + protocol |= dataBytes[2]; + protocol &= 0x0FFF; + + source = (dataBytes[7] & 0xFFl) << 24; + source |= ((dataBytes[6] & 0xFFl) << 16); + source |= ((dataBytes[5] & 0xFFl) << 8); + source |= ((dataBytes[4] & 0xFFl)); + + macAddress[0] = dataBytes[8]; + macAddress[1] = dataBytes[9]; + macAddress[2] = dataBytes[10]; + macAddress[3] = dataBytes[11]; + macAddress[4] = dataBytes[12]; + macAddress[5] = dataBytes[13]; + macAddress[6] = dataBytes[14]; + macAddress[7] = dataBytes[15]; + + ack_required = (dataBytes[22] & 0x02) == 0x02; + res_required = (dataBytes[22] & 0x01) == 0x01; + + sequence = (dataBytes[23] & 0xFF); + + type = ((dataBytes[33] & 0xFF) << 8); + type |= (dataBytes[32] & 0xFF); + } +} diff --git a/benchmarks/drivers/LifxLightBulb/LifxLightBulb.config b/benchmarks/drivers/LifxLightBulb/LifxLightBulb.config new file mode 100644 index 0000000..2af7725 --- /dev/null +++ b/benchmarks/drivers/LifxLightBulb/LifxLightBulb.config @@ -0,0 +1,2 @@ +INTERFACE_CLASS=LightBulb + diff --git a/benchmarks/drivers/LifxLightBulb/LifxLightBulb.java b/benchmarks/drivers/LifxLightBulb/LifxLightBulb.java new file mode 100644 index 0000000..021e749 --- /dev/null +++ b/benchmarks/drivers/LifxLightBulb/LifxLightBulb.java @@ -0,0 +1,1214 @@ +package iotcode.LifxLightBulb; + +// Standard Java Packages +import java.io.*; +import java.net.*; +import java.util.concurrent.Semaphore; +import java.security.InvalidParameterException; +import java.util.Date; +import java.util.Iterator; +import java.util.concurrent.atomic.AtomicBoolean; + + +// IoT Packages +//import iotcode.annotation.*; +import iotcode.interfaces.LightBulb; +import iotruntime.IoTUDP; +import iotruntime.slave.IoTDeviceAddress; +import iotruntime.slave.IoTSet; + +// String to byte conversion +import javax.xml.bind.DatatypeConverter; + +import iotchecker.qual.*; + +public class LifxLightBulb implements LightBulb { + + /******************************************************************************************************************************************* + ** + ** Constants + ** + *******************************************************************************************************************************************/ + public static final long GET_BULB_VERSION_RESEND_WAIT_SECONDS = 10; + + + + /******************************************************************************************************************************************* + ** + ** Variables + ** + *******************************************************************************************************************************************/ + private IoTUDP communicationSockect; + private byte[] bulbMacAddress = new byte[8]; + static Semaphore socketMutex = new Semaphore(1); + static boolean sendSocketFlag = false; + private long lastSentGetBulbVersionRequest = 0; // time last request sent + + // Current Bulb Values + private int currentHue = 0; + private int currentSaturation = 0; + private int currentBrightness = 65535; + private int currentTemperature = 9000; + private boolean bulbIsOn = false; + + + + private AtomicBoolean didAlreadyInit = new AtomicBoolean(false); + + private AtomicBoolean didGetBulbVersion = new AtomicBoolean(false); + static Semaphore settingBulbColorMutex = new Semaphore(1); + static Semaphore settingBulbTempuraturerMutex = new Semaphore(1); + static Semaphore bulbStateMutex = new Semaphore(1); + + // color and temperature ranges for the bulbs + private int hueLowerBound = 0; + private int hueUpperBound = 0; + private int saturationLowerBound = 0; + private int saturationUpperBound = 0; + private int brightnessLowerBound = 0; + private int brightnessUpperBound = 0; + private int temperatureLowerBound = 2500; + private int temperatureUpperBound = 9000; + + + + // Check if a state change was requested, used to poll the bulb for if the bulb did + // preform the requested state change + private boolean stateDidChange = false; + + /******************************************************************************************************************************************* + ** + ** Threads + ** + *******************************************************************************************************************************************/ + + // Main worker thread will do the receive loop + Thread workerThread = null; + + /******************************************************************************************************************************************* + ** + ** IoT Sets and Relations + ** + *******************************************************************************************************************************************/ + + // IoTSet of Device Addresses. + // Will be filled with only 1 address. + @config private IoTSet lb_addresses; + + /** + * Used for testing only + */ + public LifxLightBulb(IoTUDP udp, byte[] macAddress) { + communicationSockect = udp; + bulbMacAddress = macAddress; + } + + public LifxLightBulb(String macAddress) { + communicationSockect = null; + + // Set the Mac Address to a default value + // Probably not needed for anything + /*bulbMacAdd[0] = (byte)0x00; + bulbMacAdd[1] = (byte)0x00; + bulbMacAdd[2] = (byte)0x00; + bulbMacAdd[3] = (byte)0x00; + bulbMacAdd[4] = (byte)0x00; + bulbMacAdd[5] = (byte)0x00; + bulbMacAdd[6] = (byte)0x00; + bulbMacAdd[7] = (byte)0x00;*/ + + bulbMacAddress = DatatypeConverter.parseHexBinary(macAddress); + } + + + + /******************************************************************************************************************************************* + ** Sending + ** Device Messages + ** + *******************************************************************************************************************************************/ + private void sendGetServicePacket() { + LifxHeader header = new LifxHeader(); + header.setSize(36); + header.setTagged(true); + header.setMacAddress(bulbMacAddress); + header.setSource(0); // randomly picked + header.setAck_required(false); + header.setRes_required(false); + header.setSequence(0); + header.setType(2); + + byte[] dataBytes = header.getHeaderBytes(); + sendPacket(dataBytes); + } + + private void sendGetHostInfoPacket() { + LifxHeader header = new LifxHeader(); + header.setSize(36); + header.setTagged(false); + header.setMacAddress(bulbMacAddress); + header.setSource(10); // randomly picked + header.setAck_required(false); + header.setRes_required(false); + header.setSequence(0); + header.setType(12); + + byte[] dataBytes = header.getHeaderBytes(); + sendPacket(dataBytes); + } + + private void sendGetHostFirmwarePacket() { + LifxHeader header = new LifxHeader(); + header.setSize(36); + header.setTagged(false); + header.setMacAddress(bulbMacAddress); + header.setSource(10); // randomly picked + header.setAck_required(false); + header.setRes_required(false); + header.setSequence(0); + header.setType(14); + + byte[] dataBytes = header.getHeaderBytes(); + sendPacket(dataBytes); + } + + private void sendGetWifiInfoPacket() { + LifxHeader header = new LifxHeader(); + header.setSize(36); + header.setTagged(false); + header.setMacAddress(bulbMacAddress); + header.setSource(10); // randomly picked + header.setAck_required(false); + header.setRes_required(false); + header.setSequence(0); + header.setType(16); + + byte[] dataBytes = header.getHeaderBytes(); + sendPacket(dataBytes); + } + + private void sendGetWifiFirmwarePacket() { + LifxHeader header = new LifxHeader(); + header.setSize(36); + header.setTagged(false); + header.setMacAddress(bulbMacAddress); + header.setSource(10); // randomly picked + header.setAck_required(false); + header.setRes_required(false); + header.setSequence(0); + header.setType(18); + + byte[] dataBytes = header.getHeaderBytes(); + sendPacket(dataBytes); + } + + private void sendGetPowerPacket() { + LifxHeader header = new LifxHeader(); + header.setSize(36); + header.setTagged(false); + header.setMacAddress(bulbMacAddress); + header.setSource(10); // randomly picked + header.setAck_required(false); + header.setRes_required(false); + header.setSequence(0); + header.setType(20); + + byte[] dataBytes = header.getHeaderBytes(); + sendPacket(dataBytes); + } + + private void sendSetPowerPacket(int level) { + // Currently only 0 and 65535 are supported + // This is a fix for now + if ((level != 65535) && (level != 0)) { + throw new InvalidParameterException("Invalid parameter values"); + } + + if ((level > 65535) || (level < 0)) { + throw new InvalidParameterException("Invalid parameter values"); + } + + byte[] packetBytes = new byte[38]; + + LifxHeader header = new LifxHeader(); + header.setSize(38); + header.setTagged(false); + header.setMacAddress(bulbMacAddress); + header.setSource(10); // randomly picked + header.setAck_required(false); + header.setRes_required(false); + header.setSequence(0); + header.setType(21); + byte[] headerBytes = header.getHeaderBytes(); + + for (int i = 0; i < 36; i++) { + packetBytes[i] = headerBytes[i]; + } + + packetBytes[36] = (byte)(level & 0xFF); + packetBytes[37] = (byte)((level >> 8) & 0xFF); + + sendPacket(packetBytes); + } + + private void sendGetLabelPacket() { + LifxHeader header = new LifxHeader(); + header.setSize(36); + header.setTagged(false); + header.setMacAddress(bulbMacAddress); + header.setSource(10); // randomly picked + header.setAck_required(false); + header.setRes_required(false); + header.setSequence(0); + header.setType(23); + + byte[] dataBytes = header.getHeaderBytes(); + sendPacket(dataBytes); + } + + private void sendSetLabelPacket(String label) { + // Currently only 0 and 65535 are supported + // This is a fix for now + if (label.length() != 32) { + throw new InvalidParameterException("Invalid parameter values, label must be 32 bytes long"); + } + + byte[] packetBytes = new byte[68]; + + LifxHeader header = new LifxHeader(); + header.setSize(68); + header.setTagged(false); + header.setMacAddress(bulbMacAddress); + header.setSource(10); // randomly picked + header.setAck_required(false); + header.setRes_required(false); + header.setSequence(0); + header.setType(24); + byte[] headerBytes = header.getHeaderBytes(); + + for (int i = 0; i < 36; i++) { + packetBytes[i] = headerBytes[i]; + } + + for (int i = 0; i < 32; i++) { + packetBytes[i + 36] = label.getBytes()[i]; + } + + sendPacket(packetBytes); + } + + private void sendGetVersionPacket() { + LifxHeader header = new LifxHeader(); + header.setSize(36); + header.setTagged(false); + header.setMacAddress(bulbMacAddress); + header.setSource(10); // randomly picked + header.setAck_required(false); + header.setRes_required(false); + header.setSequence(0); + header.setType(32); + + byte[] dataBytes = header.getHeaderBytes(); + sendPacket(dataBytes); + } + + private void sendGetInfoPacket() { + LifxHeader header = new LifxHeader(); + header.setSize(36); + header.setTagged(false); + header.setMacAddress(bulbMacAddress); + header.setSource(10); // randomly picked + header.setAck_required(false); + header.setRes_required(false); + header.setSequence(0); + header.setType(34); + + byte[] dataBytes = header.getHeaderBytes(); + sendPacket(dataBytes); + } + + private void sendGetLocationPacket() { + LifxHeader header = new LifxHeader(); + header.setSize(36); + header.setTagged(false); + header.setMacAddress(bulbMacAddress); + header.setSource(10); // randomly picked + header.setAck_required(false); + header.setRes_required(false); + header.setSequence(0); + header.setType(34); + + byte[] dataBytes = header.getHeaderBytes(); + sendPacket(dataBytes); + } + + private void sendGetGroupPacket() { + LifxHeader header = new LifxHeader(); + header.setSize(36); + header.setTagged(false); + header.setMacAddress(bulbMacAddress); + header.setSource(10); // randomly picked + header.setAck_required(false); + header.setRes_required(false); + header.setSequence(0); + header.setType(51); + + byte[] dataBytes = header.getHeaderBytes(); + sendPacket(dataBytes); + } + + + /******************************************************************************************************************************************* + ** Sending + ** Light Messages + ** + *******************************************************************************************************************************************/ + private void sendGetLightStatePacket() { + LifxHeader header = new LifxHeader(); + header.setSize(36); + header.setTagged(false); + header.setMacAddress(bulbMacAddress); + header.setSource(10); // randomly picked + header.setAck_required(false); + header.setRes_required(false); + header.setSequence(0); + header.setType(101); + + byte[] dataBytes = header.getHeaderBytes(); + sendPacket(dataBytes); + } + + private void sendSetLightColorPacket(BulbColor bulbColor, long duration) { + + if ((duration > 4294967295l) || (duration < 0)) { + throw new InvalidParameterException("Invalid parameter value, duration out of range (0 - 4294967295)"); + } + + byte[] packetBytes = new byte[49]; + + LifxHeader header = new LifxHeader(); + header.setSize(49); + header.setTagged(false); + header.setMacAddress(bulbMacAddress); + header.setSource(10); // randomly picked + header.setAck_required(false); + header.setRes_required(false); + header.setSequence(0); + header.setType(102); + byte[] headerBytes = header.getHeaderBytes(); + + for (int i = 0; i < 36; i++) { + packetBytes[i] = headerBytes[i]; + } + + // 1 reserved packet + packetBytes[37] = (byte)(bulbColor.getHue() & 0xFF); + packetBytes[38] = (byte)((bulbColor.getHue() >> 8) & 0xFF); + + packetBytes[39] = (byte)(bulbColor.getSaturation() & 0xFF); + packetBytes[40] = (byte)((bulbColor.getSaturation() >> 8) & 0xFF); + + packetBytes[41] = (byte)(bulbColor.getBrightness() & 0xFF); + packetBytes[42] = (byte)((bulbColor.getBrightness() >> 8) & 0xFF); + + packetBytes[43] = (byte)(bulbColor.getKelvin() & 0xFF); + packetBytes[44] = (byte)((bulbColor.getKelvin() >> 8) & 0xFF); + + packetBytes[45] = (byte)((duration >> 0) & 0xFF); + packetBytes[46] = (byte)((duration >> 8) & 0xFF); + packetBytes[47] = (byte)((duration >> 16) & 0xFF); + packetBytes[48] = (byte)((duration >> 24) & 0xFF); + + sendPacket(packetBytes); + } + + private void sendGetLightPowerPacket() { + LifxHeader header = new LifxHeader(); + header.setSize(36); + header.setTagged(false); + header.setMacAddress(bulbMacAddress); + header.setSource(10); // randomly picked + header.setAck_required(false); + header.setRes_required(false); + header.setSequence(0); + header.setType(116); + + byte[] dataBytes = header.getHeaderBytes(); + sendPacket(dataBytes); + } + + private void sendSetLightPowerPacket(int level, long duration) { + + if ((level > 65535) || (duration > 4294967295l) + || (level < 0) || (duration < 0)) { + throw new InvalidParameterException("Invalid parameter values"); + } + + byte[] packetBytes = new byte[42]; + + + LifxHeader header = new LifxHeader(); + header.setSize(42); + header.setTagged(false); + header.setMacAddress(bulbMacAddress); + header.setSource(10); // randomly picked + header.setAck_required(false); + header.setRes_required(false); + header.setSequence(0); + header.setType(117); + byte[] headerBytes = header.getHeaderBytes(); + + for (int i = 0; i < 36; i++) { + packetBytes[i] = headerBytes[i]; + } + + packetBytes[36] = (byte)(level & 0xFF); + packetBytes[37] = (byte)((level >> 8) & 0xFF); + + packetBytes[38] = (byte)((duration >> 0) & 0xFF); + packetBytes[39] = (byte)((duration >> 8) & 0xFF); + packetBytes[40] = (byte)((duration >> 16) & 0xFF); + packetBytes[41] = (byte)((duration >> 24) & 0xFF); + + sendPacket(packetBytes); + } + + private void sendEchoRequestPacket(byte[] data) { + // Currently only 0 and 65535 are supported + // This is a fix for now + if (data.length != 64) { + throw new InvalidParameterException("Invalid parameter values, must have 64 bytes"); + } + + byte[] packetBytes = new byte[100]; + + LifxHeader header = new LifxHeader(); + header.setSize(100); + header.setTagged(false); + header.setMacAddress(bulbMacAddress); + header.setSource(10); // randomly picked + header.setAck_required(false); + header.setRes_required(false); + header.setSequence(0); + header.setType(58); + byte[] headerBytes = header.getHeaderBytes(); + + for (int i = 0; i < 36; i++) { + packetBytes[i] = headerBytes[i]; + } + + for (int i = 0; i < 64; i++) { + packetBytes[i + 36] = data[i]; + } + + sendPacket(packetBytes); + } + + + /******************************************************************************************************************************************* + ** Receiving + ** Device Messages + ** + *******************************************************************************************************************************************/ + private DeviceStateService parseDeviceStateServiceMessage(LifxHeader header, byte[] payloadData) { + int service = payloadData[0]; + long port = ((payloadData[3] & 0xFF) << 24); + port |= ((payloadData[2] & 0xFF) << 16); + port |= ((payloadData[1] & 0xFF) << 8); + port |= (payloadData[0] & 0xFF); + + return new DeviceStateService(service, port); + } + + private DeviceStateHostInfo parseDeviceStateHostInfoMessage(LifxHeader header, byte[] payloadData) { + long signal = ((payloadData[3] & 0xFF) << 24); + signal |= ((payloadData[2] & 0xFF) << 16); + signal |= ((payloadData[1] & 0xFF) << 8); + signal |= (payloadData[0] & 0xFF); + + long tx = ((payloadData[7] & 0xFF) << 24); + tx |= ((payloadData[6] & 0xFF) << 16); + tx |= ((payloadData[5] & 0xFF) << 8); + tx |= (payloadData[4] & 0xFF); + + long rx = ((payloadData[11] & 0xFF) << 24); + rx |= ((payloadData[10] & 0xFF) << 16); + rx |= ((payloadData[9] & 0xFF) << 8); + rx |= (payloadData[8] & 0xFF); + + return new DeviceStateHostInfo(signal, tx, rx); + } + + private DeviceStateHostFirmware parseDeviceStateHostFirmwareMessage(LifxHeader header, byte[] payloadData) { + long build = 0; + for (int i = 0; i < 8; i++) { + build += ((long) payloadData[i] & 0xffL) << (8 * i); + } + + // 8 reserved bytes + + long version = ((payloadData[19] & 0xFF) << 24); + version |= ((payloadData[18] & 0xFF) << 16); + version |= ((payloadData[17] & 0xFF) << 8); + version |= (payloadData[16] & 0xFF); + + return new DeviceStateHostFirmware(build, version); + } + + private DeviceStateWifiInfo parseDeviceStateWifiInfoMessage(LifxHeader header, byte[] payloadData) { + long signal = ((payloadData[3] & 0xFF) << 24); + signal |= ((payloadData[2] & 0xFF) << 16); + signal |= ((payloadData[1] & 0xFF) << 8); + signal |= (payloadData[0] & 0xFF); + + long tx = ((payloadData[7] & 0xFF) << 24); + tx |= ((payloadData[6] & 0xFF) << 16); + tx |= ((payloadData[5] & 0xFF) << 8); + tx |= (payloadData[4] & 0xFF); + + long rx = ((payloadData[11] & 0xFF) << 24); + rx |= ((payloadData[10] & 0xFF) << 16); + rx |= ((payloadData[9] & 0xFF) << 8); + rx |= (payloadData[8] & 0xFF); + + return new DeviceStateWifiInfo(signal, tx, rx); + } + + private DeviceStateWifiFirmware parseDeviceStateWifiFirmwareMessage(LifxHeader header, byte[] payloadData) { + long build = 0; + for (int i = 0; i < 8; i++) { + build += ((long) payloadData[i] & 0xffL) << (8 * i); + } + + // 8 reserved bytes + + long version = ((payloadData[19] & 0xFF) << 24); + version |= ((payloadData[18] & 0xFF) << 16); + version |= ((payloadData[17] & 0xFF) << 8); + version |= (payloadData[16] & 0xFF); + + return new DeviceStateWifiFirmware(build, version); + } + + private int parseStatePowerMessage(LifxHeader header, byte[] payloadData) { + int level = ((payloadData[1] & 0xFF) << 8); + level |= (payloadData[0] & 0xFF); + return level; + } + + private String parseStateLabelMessage(LifxHeader header, byte[] payloadData) { + return new String(payloadData); + } + + + private DeviceStateVersion parseDeviceStateVersionMessage(LifxHeader header, byte[] payloadData) { + long vender = ((payloadData[3] & 0xFF) << 24); + vender |= ((payloadData[2] & 0xFF) << 16); + vender |= ((payloadData[1] & 0xFF) << 8); + vender |= (payloadData[0] & 0xFF); + + long product = ((payloadData[7] & 0xFF) << 24); + product |= ((payloadData[6] & 0xFF) << 16); + product |= ((payloadData[5] & 0xFF) << 8); + product |= (payloadData[4] & 0xFF); + + long version = ((payloadData[11] & 0xFF) << 24); + version |= ((payloadData[10] & 0xFF) << 16); + version |= ((payloadData[9] & 0xFF) << 8); + version |= (payloadData[8] & 0xFF); + + return new DeviceStateVersion(vender, product, version); + } + + private DeviceStateInfo parseDeviceStateInfoMessage(LifxHeader header, byte[] payloadData) { + long time = 0; + long upTime = 0; + long downTime = 0; + for (int i = 0; i < 8; i++) { + time += ((long) payloadData[i] & 0xffL) << (8 * i); + upTime += ((long) payloadData[i + 8] & 0xffL) << (8 * i); + downTime += ((long) payloadData[i + 16] & 0xffL) << (8 * i); + } + + return new DeviceStateInfo(time, upTime, downTime); + } + + private DeviceStateLocation parseDeviceStateLocationMessage(LifxHeader header, byte[] payloadData) { + byte[] location = new byte[16]; + for (int i = 0; i < 16; i++) { + location[i] = payloadData[i]; + } + + byte[] labelBytes = new byte[32]; + for (int i = 0; i < 32; i++) { + labelBytes[i] = payloadData[i + 16]; + } + + long updatedAt = 0; + for (int i = 0; i < 8; i++) { + updatedAt += ((long) payloadData[48] & 0xffL) << (8 * i); + } + + return new DeviceStateLocation(location, new String(labelBytes), updatedAt); + } + + private DeviceStateGroup parseDeviceStateGroupMessage(LifxHeader header, byte[] payloadData) { + byte[] group = new byte[16]; + for (int i = 0; i < 16; i++) { + group[i] = payloadData[i]; + } + + byte[] labelBytes = new byte[32]; + for (int i = 0; i < 32; i++) { + labelBytes[i] = payloadData[i + 16]; + } + + long updatedAt = 0; + for (int i = 0; i < 8; i++) { + updatedAt += ((long) payloadData[48] & 0xffL) << (8 * i); + } + + return new DeviceStateGroup(group, new String(labelBytes), updatedAt); + } + + private byte[] parseDeviceEchoResponseMessage(LifxHeader header, byte[] payloadData) { + return payloadData; + } + + /******************************************************************************************************************************************* + ** Receiving + ** Light Messages + ** + *******************************************************************************************************************************************/ + private LightState parseLightStateMessage(LifxHeader header, byte[] payloadData) { + + byte[] colorData = new byte[8]; + for (int i = 0; i < 8; i++) { + colorData[i] = payloadData[i]; + } + BulbColor color = new BulbColor(colorData); + + int power = ((payloadData[11] & 0xFF) << 8); + power |= (payloadData[10] & 0xFF); + + String label = new String(payloadData); + + byte[] labelArray = new byte[32]; + for (int i = 0; i < 32; i++) { + labelArray[i] = payloadData[12 + i]; + } + + return new LightState(color, power, label); + } + + private int parseLightStatePowerMessage(LifxHeader header, byte[] payloadData) { + int level = ((payloadData[1] & 0xFF) << 8); + level |= (payloadData[0] & 0xFF); + return level; + } + + + /******************************************************************************************************************************************* + ** + ** Private Handlers + ** + *******************************************************************************************************************************************/ + private void handleStateVersionMessageRecieved(LifxHeader header, byte[] payloadData) { + + DeviceStateVersion deviceState = parseDeviceStateVersionMessage(header, payloadData); + int productNumber = (int)deviceState.getProduct(); + + boolean isColor = false; + + if (productNumber == 1) { // Original 1000 + isColor = true; + } else if (productNumber == 3) { //Color 650 + isColor = true; + } else if (productNumber == 10) { // White 800 (Low Voltage) + isColor = false; + } else if (productNumber == 11) { // White 800 (High Voltage) + isColor = false; + } else if (productNumber == 18) { // White 900 BR30 (Low Voltage) + isColor = false; + } else if (productNumber == 20) { // Color 1000 BR30 + isColor = true; + } else if (productNumber == 22) { // Color 1000 + isColor = true; + } + + if (isColor) { + hueLowerBound = 0; + hueUpperBound = 65535; + saturationLowerBound = 0; + saturationUpperBound = 65535; + brightnessLowerBound = 0; + brightnessUpperBound = 65535; + temperatureLowerBound = 2500; + temperatureUpperBound = 9000; + } else { + hueLowerBound = 0; + hueUpperBound = 0; + saturationLowerBound = 0; + saturationUpperBound = 0; + brightnessLowerBound = 0; + brightnessUpperBound = 65535; // still can dim bulb + temperatureLowerBound = 2500; + temperatureUpperBound = 9000; + } + + didGetBulbVersion.set(true); + + } + + private void handleLightStateMessageRecieved(LifxHeader header, byte[] payloadData) { + LightState lightState = parseLightStateMessage(header, payloadData); + + BulbColor color = lightState.getColor(); + int power = lightState.getPower(); + + boolean bulbWrongColor = false; + bulbWrongColor = bulbWrongColor || (color.getHue() != currentHue); + bulbWrongColor = bulbWrongColor || (color.getSaturation() != currentSaturation); + bulbWrongColor = bulbWrongColor || (color.getBrightness() != currentBrightness); + bulbWrongColor = bulbWrongColor || (color.getKelvin() != currentTemperature); + + + // gets set to true if any of the below if statements are taken + stateDidChange = false; + + if (bulbWrongColor) { + BulbColor newColor = new BulbColor(currentHue, currentSaturation, currentBrightness, currentTemperature); + sendSetLightColorPacket(newColor, 250); + // System.out.println("Failed Check 1"); + } + + try { + bulbStateMutex.acquire(); + } catch (Exception e) { + e.printStackTrace(); + } + boolean bulbIsOnTmp = bulbIsOn; + bulbStateMutex.release(); + + if ((!bulbIsOnTmp) && (power != 0)) { + turnOff(); + // System.out.println("Failed Check 2: " + Integer.toString(power)); + + } + + if (bulbIsOnTmp && (power < 65530)) { + turnOn(); + // System.out.println("Failed Check 3: " + Integer.toString(power)); + + } + } + + /******************************************************************************************************************************************* + ** + ** Light Bulb Interface Methods + ** + *******************************************************************************************************************************************/ + public double getHue() { + double tmp = 0; + try { + settingBulbColorMutex.acquire(); + tmp = ((double)currentHue / 65535.0) * 360.0; + } catch (Exception e) { + e.printStackTrace(); + } + settingBulbColorMutex.release(); + + + return tmp; + } + + public double getSaturation() { + double tmp = 0; + try { + settingBulbColorMutex.acquire(); + tmp = ((double)currentSaturation / 65535.0) * 360.0; + } catch (Exception e) { + e.printStackTrace(); + } + settingBulbColorMutex.release(); + + + return tmp; + } + + public double getBrightness() { + double tmp = 0; + try { + settingBulbColorMutex.acquire(); + tmp = ((double)currentBrightness / 65535.0) * 360.0; + } catch (Exception e) { + e.printStackTrace(); + } + settingBulbColorMutex.release(); + + return tmp; + } + + public int getTemperature() { + + int tmp = 0; + try { + settingBulbTempuraturerMutex.acquire(); + tmp = currentTemperature; + } catch (Exception e) { + e.printStackTrace(); + } + settingBulbTempuraturerMutex.release(); + + return tmp; + } + + public double getHueRangeLowerBound() { + if (!didGetBulbVersion.get()) { + return -1; + } + return ((double)hueLowerBound / 65535.0) * 360.0; + } + + public double getHueRangeUpperBound() { + if (!didGetBulbVersion.get()) { + return -1; + } + return ((double)hueUpperBound / 65535.0) * 360.0; + } + + public double getSaturationRangeLowerBound() { + if (!didGetBulbVersion.get()) { + return -1; + } + return ((double)saturationLowerBound / 65535.0) * 100.0; + } + + public double getSaturationRangeUpperBound() { + if (!didGetBulbVersion.get()) { + return -1; + } + return ((double)saturationUpperBound / 65535.0) * 100.0; + } + + public double getBrightnessRangeLowerBound() { + if (!didGetBulbVersion.get()) { + return -1; + } + return ((double)brightnessLowerBound / 65535.0) * 100.0; + } + + public double getBrightnessRangeUpperBound() { + if (!didGetBulbVersion.get()) { + return -1; + } + return ((double)brightnessUpperBound / 65535.0) * 100.0; + } + + public int getTemperatureRangeLowerBound() { + if (!didGetBulbVersion.get()) { + return -1; + } + return temperatureLowerBound; + } + + public int getTemperatureRangeUpperBound() { + if (!didGetBulbVersion.get()) { + return -1; + } + return temperatureUpperBound; + } + + public void setTemperature(int _temperature) { + + try { + settingBulbTempuraturerMutex.acquire(); + } catch (Exception e) { + e.printStackTrace(); + } + + BulbColor newColor = new BulbColor(currentHue, currentSaturation, currentBrightness, _temperature); + sendSetLightColorPacket(newColor, 250); + + currentTemperature = _temperature; + stateDidChange = true; + + settingBulbTempuraturerMutex.release(); + } + + public void setColor(double _hue, double _saturation, double _brightness) { + + try { + settingBulbColorMutex.acquire(); + } catch (Exception e) { + e.printStackTrace(); + } + + + _hue /= 360.0; + _saturation /= 100.0; + _brightness /= 100.0; + + + int newHue = (int)(_hue * 65535.0); + int newSaturation = (int)(_saturation * 65535.0); + int newBrightness = (int)(_brightness * 65535.0); + + BulbColor newColor = new BulbColor(newHue, newSaturation, newBrightness, currentTemperature); + sendSetLightColorPacket(newColor, 250); + + currentHue = newHue; + currentSaturation = newSaturation; + currentBrightness = newBrightness; + stateDidChange = true; + + settingBulbColorMutex.release(); + } + + + public void turnOff() { + + try { + bulbStateMutex.acquire(); + bulbIsOn = false; + sendSetLightPowerPacket(0, 0); + stateDidChange = true; + } catch (Exception e) { + e.printStackTrace(); + } + + bulbStateMutex.release(); + } + + public void turnOn() { + try { + bulbStateMutex.acquire(); + bulbIsOn = true; + sendSetLightPowerPacket(65535, 0); + stateDidChange = true; + + } catch (Exception e) { + e.printStackTrace(); + } + + + bulbStateMutex.release(); + } + + public boolean getState() { + + boolean tmp = false; + try { + bulbStateMutex.acquire(); + tmp = bulbIsOn; + } catch (Exception e) { + e.printStackTrace(); + } + + bulbStateMutex.release(); + + return tmp; + } + + + /******************************************************************************************************************************************* + ** + ** Communication Helpers + ** + *******************************************************************************************************************************************/ + private void recievedPacket(byte[] packetData) { + + byte[] headerBytes = new byte[36]; + for (int i = 0; i < 36; i++) { + headerBytes[i] = packetData[i]; + } + + LifxHeader recHeader = new LifxHeader(); + recHeader.setFromBytes(headerBytes); + + // load the payload bytes (strip away the header) + byte[] payloadBytes = new byte[recHeader.getSize()]; + for (int i = 36; i < recHeader.getSize(); i++) { + payloadBytes[i - 36] = packetData[i]; + } + + System.out.println("Received: " + Integer.toString(recHeader.getType())); + + switch (recHeader.getType()) { + case 3: + DeviceStateService dat = parseDeviceStateServiceMessage(recHeader, payloadBytes); + // System.out.println("Service: " + Integer.toString(dat.getService())); + // System.out.println("Port : " + Long.toString(dat.getPort())); + break; + + + case 33: + handleStateVersionMessageRecieved(recHeader, payloadBytes); + break; + + case 35: + parseDeviceStateInfoMessage(recHeader, payloadBytes); + break; + + + case 107: + handleLightStateMessageRecieved(recHeader, payloadBytes); + break; + + default: + // System.out.println("unknown packet Type"); + } + + } + + private void sendPacket(byte[] packetData) { + // System.out.println("About to send"); + sendSocketFlag = true; + + try { + socketMutex.acquire(); + } catch (InterruptedException e) { + System.out.println("mutex Error"); + } + + try { + communicationSockect.sendData(packetData); + + } catch (IOException e) { + System.out.println("Socket Send Error"); + } + + sendSocketFlag = false; + socketMutex.release(); + } + + + /** + * Worker function which runs the while loop for receiving data from the bulb. + * Is blocking + */ + private void workerFunction() { + LifxHeader h = new LifxHeader(); + + try { + // Need timeout on receives since we are not sure if a packet will be available + // for processing so don't block waiting + communicationSockect.setSoTimeout(50); + } catch (IOException e) { + } + + // Start the bulb in the off state + turnOff(); + + + while (true) { + + // Check if we got the bulb version yet + // could have requested it but message could have gotten lost (UDP) + if (!didGetBulbVersion.get()) { + long currentTime = (new Date().getTime()) / 1000; + if ((currentTime - lastSentGetBulbVersionRequest) > GET_BULB_VERSION_RESEND_WAIT_SECONDS) { + // Get the bulb version so we know what type of bulb this is. + sendGetVersionPacket(); + lastSentGetBulbVersionRequest = currentTime; + } + } + + // Communication resource is busy so try again later + if (sendSocketFlag) { + continue; + } + + try { + socketMutex.acquire(); + } catch (InterruptedException e) { + } + + byte[] dat = null; + try { + dat = communicationSockect.recieveData(1024); + } catch (java.net.SocketTimeoutException e) { + // Timeout occurred + + } catch (IOException e) { + // Problem but might be able to recover?? + e.printStackTrace(); + + } + + // Never forget to release! + socketMutex.release(); + + // A packed arrived + if (dat != null) { + recievedPacket(dat); + } + + // If a state change occurred then request the bulb state to ensure that the + // bulb did indeed change its state to the correct state + if (stateDidChange) { + sendGetLightStatePacket(); + } + + // Wait a bit as to not tie up system resources + try { + Thread.sleep(100); + } catch (Exception e) { + + } + + + } + } + + + public void init() { + + if (didAlreadyInit.compareAndSet(false, true) == false) { + return; // already init + } + + try { + // Get the bulb address from the IoTSet + Iterator itr = lb_addresses.iterator(); + IoTDeviceAddress deviceAddress = (IoTDeviceAddress)itr.next(); + + System.out.println("Address: " + deviceAddress.getCompleteAddress()); + + // Create the communication channel + communicationSockect = new IoTUDP(deviceAddress); + + } catch (IOException e) { + e.printStackTrace(); + } + + // Launch the worker function in a separate thread. + workerThread = new Thread(new Runnable() { + public void run() { + workerFunction(); + } + }); + workerThread.start(); + + } +} + + + + + + + + + + + + + + + + + + + + diff --git a/benchmarks/drivers/LifxLightBulb/LightState.java b/benchmarks/drivers/LifxLightBulb/LightState.java new file mode 100644 index 0000000..6d8a1f7 --- /dev/null +++ b/benchmarks/drivers/LifxLightBulb/LightState.java @@ -0,0 +1,25 @@ +package iotcode.LifxLightBulb; + +public class LightState { + private final BulbColor color; + private final int power; + private final String label; + + public LightState(BulbColor _color, int _power, String _label) { + color = _color; + power = _power; + label = _label; + } + + public BulbColor getColor() { + return color; + } + + public int getPower() { + return power; + } + + public String getLabel() { + return label; + } +} diff --git a/benchmarks/libs/boofcv_libs/BoofCV-WebcamCapture-0.21-sources.jar b/benchmarks/libs/boofcv_libs/BoofCV-WebcamCapture-0.21-sources.jar new file mode 100644 index 0000000..3ef2914 Binary files /dev/null and b/benchmarks/libs/boofcv_libs/BoofCV-WebcamCapture-0.21-sources.jar differ diff --git a/benchmarks/libs/boofcv_libs/BoofCV-WebcamCapture-0.21.jar b/benchmarks/libs/boofcv_libs/BoofCV-WebcamCapture-0.21.jar new file mode 100644 index 0000000..4373992 Binary files /dev/null and b/benchmarks/libs/boofcv_libs/BoofCV-WebcamCapture-0.21.jar differ diff --git a/benchmarks/libs/boofcv_libs/BoofCV-android-0.21-sources.jar b/benchmarks/libs/boofcv_libs/BoofCV-android-0.21-sources.jar new file mode 100644 index 0000000..1d77656 Binary files /dev/null and b/benchmarks/libs/boofcv_libs/BoofCV-android-0.21-sources.jar differ diff --git a/benchmarks/libs/boofcv_libs/BoofCV-android-0.21.jar b/benchmarks/libs/boofcv_libs/BoofCV-android-0.21.jar new file mode 100644 index 0000000..982711c Binary files /dev/null and b/benchmarks/libs/boofcv_libs/BoofCV-android-0.21.jar differ diff --git a/benchmarks/libs/boofcv_libs/BoofCV-calibration-0.21-sources.jar b/benchmarks/libs/boofcv_libs/BoofCV-calibration-0.21-sources.jar new file mode 100644 index 0000000..452e01a Binary files /dev/null and b/benchmarks/libs/boofcv_libs/BoofCV-calibration-0.21-sources.jar differ diff --git a/benchmarks/libs/boofcv_libs/BoofCV-calibration-0.21.jar b/benchmarks/libs/boofcv_libs/BoofCV-calibration-0.21.jar new file mode 100644 index 0000000..7fa5c9f Binary files /dev/null and b/benchmarks/libs/boofcv_libs/BoofCV-calibration-0.21.jar differ diff --git a/benchmarks/libs/boofcv_libs/BoofCV-feature-0.21-sources.jar b/benchmarks/libs/boofcv_libs/BoofCV-feature-0.21-sources.jar new file mode 100644 index 0000000..4c5f39b Binary files /dev/null and b/benchmarks/libs/boofcv_libs/BoofCV-feature-0.21-sources.jar differ diff --git a/benchmarks/libs/boofcv_libs/BoofCV-feature-0.21.jar b/benchmarks/libs/boofcv_libs/BoofCV-feature-0.21.jar new file mode 100644 index 0000000..c585033 Binary files /dev/null and b/benchmarks/libs/boofcv_libs/BoofCV-feature-0.21.jar differ diff --git a/benchmarks/libs/boofcv_libs/BoofCV-geo-0.21-sources.jar b/benchmarks/libs/boofcv_libs/BoofCV-geo-0.21-sources.jar new file mode 100644 index 0000000..38e9cb0 Binary files /dev/null and b/benchmarks/libs/boofcv_libs/BoofCV-geo-0.21-sources.jar differ diff --git a/benchmarks/libs/boofcv_libs/BoofCV-geo-0.21.jar b/benchmarks/libs/boofcv_libs/BoofCV-geo-0.21.jar new file mode 100644 index 0000000..a7b0cd3 Binary files /dev/null and b/benchmarks/libs/boofcv_libs/BoofCV-geo-0.21.jar differ diff --git a/benchmarks/libs/boofcv_libs/BoofCV-io-0.21-sources.jar b/benchmarks/libs/boofcv_libs/BoofCV-io-0.21-sources.jar new file mode 100644 index 0000000..ea0182e Binary files /dev/null and b/benchmarks/libs/boofcv_libs/BoofCV-io-0.21-sources.jar differ diff --git a/benchmarks/libs/boofcv_libs/BoofCV-io-0.21.jar b/benchmarks/libs/boofcv_libs/BoofCV-io-0.21.jar new file mode 100644 index 0000000..661dead Binary files /dev/null and b/benchmarks/libs/boofcv_libs/BoofCV-io-0.21.jar differ diff --git a/benchmarks/libs/boofcv_libs/BoofCV-ip-0.21-sources.jar b/benchmarks/libs/boofcv_libs/BoofCV-ip-0.21-sources.jar new file mode 100644 index 0000000..4001616 Binary files /dev/null and b/benchmarks/libs/boofcv_libs/BoofCV-ip-0.21-sources.jar differ diff --git a/benchmarks/libs/boofcv_libs/BoofCV-ip-0.21.jar b/benchmarks/libs/boofcv_libs/BoofCV-ip-0.21.jar new file mode 100644 index 0000000..37b76dd Binary files /dev/null and b/benchmarks/libs/boofcv_libs/BoofCV-ip-0.21.jar differ diff --git a/benchmarks/libs/boofcv_libs/BoofCV-jcodec-0.21-sources.jar b/benchmarks/libs/boofcv_libs/BoofCV-jcodec-0.21-sources.jar new file mode 100644 index 0000000..055c2a2 Binary files /dev/null and b/benchmarks/libs/boofcv_libs/BoofCV-jcodec-0.21-sources.jar differ diff --git a/benchmarks/libs/boofcv_libs/BoofCV-jcodec-0.21.jar b/benchmarks/libs/boofcv_libs/BoofCV-jcodec-0.21.jar new file mode 100644 index 0000000..31566e6 Binary files /dev/null and b/benchmarks/libs/boofcv_libs/BoofCV-jcodec-0.21.jar differ diff --git a/benchmarks/libs/boofcv_libs/BoofCV-learning-0.21-sources.jar b/benchmarks/libs/boofcv_libs/BoofCV-learning-0.21-sources.jar new file mode 100644 index 0000000..d6eecb1 Binary files /dev/null and b/benchmarks/libs/boofcv_libs/BoofCV-learning-0.21-sources.jar differ diff --git a/benchmarks/libs/boofcv_libs/BoofCV-learning-0.21.jar b/benchmarks/libs/boofcv_libs/BoofCV-learning-0.21.jar new file mode 100644 index 0000000..3363ca7 Binary files /dev/null and b/benchmarks/libs/boofcv_libs/BoofCV-learning-0.21.jar differ diff --git a/benchmarks/libs/boofcv_libs/BoofCV-openkinect-0.21-sources.jar b/benchmarks/libs/boofcv_libs/BoofCV-openkinect-0.21-sources.jar new file mode 100644 index 0000000..9dd3e2d Binary files /dev/null and b/benchmarks/libs/boofcv_libs/BoofCV-openkinect-0.21-sources.jar differ diff --git a/benchmarks/libs/boofcv_libs/BoofCV-openkinect-0.21.jar b/benchmarks/libs/boofcv_libs/BoofCV-openkinect-0.21.jar new file mode 100644 index 0000000..e0939ab Binary files /dev/null and b/benchmarks/libs/boofcv_libs/BoofCV-openkinect-0.21.jar differ diff --git a/benchmarks/libs/boofcv_libs/BoofCV-recognition-0.21-sources.jar b/benchmarks/libs/boofcv_libs/BoofCV-recognition-0.21-sources.jar new file mode 100644 index 0000000..cd989e5 Binary files /dev/null and b/benchmarks/libs/boofcv_libs/BoofCV-recognition-0.21-sources.jar differ diff --git a/benchmarks/libs/boofcv_libs/BoofCV-recognition-0.21.jar b/benchmarks/libs/boofcv_libs/BoofCV-recognition-0.21.jar new file mode 100644 index 0000000..7df834e Binary files /dev/null and b/benchmarks/libs/boofcv_libs/BoofCV-recognition-0.21.jar differ diff --git a/benchmarks/libs/boofcv_libs/BoofCV-sfm-0.21-sources.jar b/benchmarks/libs/boofcv_libs/BoofCV-sfm-0.21-sources.jar new file mode 100644 index 0000000..c6a1aeb Binary files /dev/null and b/benchmarks/libs/boofcv_libs/BoofCV-sfm-0.21-sources.jar differ diff --git a/benchmarks/libs/boofcv_libs/BoofCV-sfm-0.21.jar b/benchmarks/libs/boofcv_libs/BoofCV-sfm-0.21.jar new file mode 100644 index 0000000..73513bb Binary files /dev/null and b/benchmarks/libs/boofcv_libs/BoofCV-sfm-0.21.jar differ diff --git a/benchmarks/libs/boofcv_libs/BoofCV-visualize-0.21-sources.jar b/benchmarks/libs/boofcv_libs/BoofCV-visualize-0.21-sources.jar new file mode 100644 index 0000000..f787353 Binary files /dev/null and b/benchmarks/libs/boofcv_libs/BoofCV-visualize-0.21-sources.jar differ diff --git a/benchmarks/libs/boofcv_libs/BoofCV-visualize-0.21.jar b/benchmarks/libs/boofcv_libs/BoofCV-visualize-0.21.jar new file mode 100644 index 0000000..4be069b Binary files /dev/null and b/benchmarks/libs/boofcv_libs/BoofCV-visualize-0.21.jar differ diff --git a/benchmarks/libs/boofcv_libs/BoofCV-xuggler-0.21-sources.jar b/benchmarks/libs/boofcv_libs/BoofCV-xuggler-0.21-sources.jar new file mode 100644 index 0000000..5883a9f Binary files /dev/null and b/benchmarks/libs/boofcv_libs/BoofCV-xuggler-0.21-sources.jar differ diff --git a/benchmarks/libs/boofcv_libs/BoofCV-xuggler-0.21.jar b/benchmarks/libs/boofcv_libs/BoofCV-xuggler-0.21.jar new file mode 100644 index 0000000..d72f51b Binary files /dev/null and b/benchmarks/libs/boofcv_libs/BoofCV-xuggler-0.21.jar differ diff --git a/benchmarks/libs/boofcv_libs/core-0.29.jar b/benchmarks/libs/boofcv_libs/core-0.29.jar new file mode 100644 index 0000000..46e8fe9 Binary files /dev/null and b/benchmarks/libs/boofcv_libs/core-0.29.jar differ diff --git a/benchmarks/libs/boofcv_libs/ddogleg-0.9.jar b/benchmarks/libs/boofcv_libs/ddogleg-0.9.jar new file mode 100644 index 0000000..62bb1f1 Binary files /dev/null and b/benchmarks/libs/boofcv_libs/ddogleg-0.9.jar differ diff --git a/benchmarks/libs/boofcv_libs/dense64-0.29.jar b/benchmarks/libs/boofcv_libs/dense64-0.29.jar new file mode 100644 index 0000000..fc1c59c Binary files /dev/null and b/benchmarks/libs/boofcv_libs/dense64-0.29.jar differ diff --git a/benchmarks/libs/boofcv_libs/equation-0.29.jar b/benchmarks/libs/boofcv_libs/equation-0.29.jar new file mode 100644 index 0000000..5802fb8 Binary files /dev/null and b/benchmarks/libs/boofcv_libs/equation-0.29.jar differ diff --git a/benchmarks/libs/boofcv_libs/georegression-0.10.jar b/benchmarks/libs/boofcv_libs/georegression-0.10.jar new file mode 100644 index 0000000..df3a2aa Binary files /dev/null and b/benchmarks/libs/boofcv_libs/georegression-0.10.jar differ diff --git a/benchmarks/libs/boofcv_libs/simple-0.29.jar b/benchmarks/libs/boofcv_libs/simple-0.29.jar new file mode 100644 index 0000000..662a584 Binary files /dev/null and b/benchmarks/libs/boofcv_libs/simple-0.29.jar differ diff --git a/benchmarks/libs/boofcv_libs/xmlpull-1.1.3.1.jar b/benchmarks/libs/boofcv_libs/xmlpull-1.1.3.1.jar new file mode 100644 index 0000000..cbc149d Binary files /dev/null and b/benchmarks/libs/boofcv_libs/xmlpull-1.1.3.1.jar differ diff --git a/benchmarks/libs/boofcv_libs/xpp3_min-1.1.4c.jar b/benchmarks/libs/boofcv_libs/xpp3_min-1.1.4c.jar new file mode 100644 index 0000000..813a9a8 Binary files /dev/null and b/benchmarks/libs/boofcv_libs/xpp3_min-1.1.4c.jar differ diff --git a/benchmarks/libs/boofcv_libs/xstream-1.4.7.jar b/benchmarks/libs/boofcv_libs/xstream-1.4.7.jar new file mode 100644 index 0000000..ea4b6a2 Binary files /dev/null and b/benchmarks/libs/boofcv_libs/xstream-1.4.7.jar differ diff --git a/benchmarks/libs/georegression_libs/GeoRegression-experimental-0.9-sources.jar b/benchmarks/libs/georegression_libs/GeoRegression-experimental-0.9-sources.jar new file mode 100644 index 0000000..2050d2f Binary files /dev/null and b/benchmarks/libs/georegression_libs/GeoRegression-experimental-0.9-sources.jar differ diff --git a/benchmarks/libs/georegression_libs/GeoRegression-experimental-0.9.jar b/benchmarks/libs/georegression_libs/GeoRegression-experimental-0.9.jar new file mode 100644 index 0000000..b640d05 Binary files /dev/null and b/benchmarks/libs/georegression_libs/GeoRegression-experimental-0.9.jar differ diff --git a/benchmarks/libs/georegression_libs/GeoRegression-georegression-0.9-sources.jar b/benchmarks/libs/georegression_libs/GeoRegression-georegression-0.9-sources.jar new file mode 100644 index 0000000..5146894 Binary files /dev/null and b/benchmarks/libs/georegression_libs/GeoRegression-georegression-0.9-sources.jar differ diff --git a/benchmarks/libs/georegression_libs/GeoRegression-georegression-0.9.jar b/benchmarks/libs/georegression_libs/GeoRegression-georegression-0.9.jar new file mode 100644 index 0000000..6f31730 Binary files /dev/null and b/benchmarks/libs/georegression_libs/GeoRegression-georegression-0.9.jar differ diff --git a/benchmarks/libs/georegression_libs/core-0.28.jar b/benchmarks/libs/georegression_libs/core-0.28.jar new file mode 100644 index 0000000..cf79ac7 Binary files /dev/null and b/benchmarks/libs/georegression_libs/core-0.28.jar differ diff --git a/benchmarks/libs/georegression_libs/ddogleg-0.8-SNAPSHOT.jar b/benchmarks/libs/georegression_libs/ddogleg-0.8-SNAPSHOT.jar new file mode 100644 index 0000000..17474da Binary files /dev/null and b/benchmarks/libs/georegression_libs/ddogleg-0.8-SNAPSHOT.jar differ diff --git a/benchmarks/libs/georegression_libs/dense64-0.28.jar b/benchmarks/libs/georegression_libs/dense64-0.28.jar new file mode 100644 index 0000000..a2b829e Binary files /dev/null and b/benchmarks/libs/georegression_libs/dense64-0.28.jar differ diff --git a/benchmarks/libs/georegression_libs/equation-0.28.jar b/benchmarks/libs/georegression_libs/equation-0.28.jar new file mode 100644 index 0000000..fa2ba79 Binary files /dev/null and b/benchmarks/libs/georegression_libs/equation-0.28.jar differ diff --git a/benchmarks/libs/georegression_libs/simple-0.28.jar b/benchmarks/libs/georegression_libs/simple-0.28.jar new file mode 100644 index 0000000..c546b0f Binary files /dev/null and b/benchmarks/libs/georegression_libs/simple-0.28.jar differ diff --git a/benchmarks/libs/jlayer_libs/jl1.0.1.jar b/benchmarks/libs/jlayer_libs/jl1.0.1.jar new file mode 100644 index 0000000..bd5fb8b Binary files /dev/null and b/benchmarks/libs/jlayer_libs/jl1.0.1.jar differ