import com.github.besherman.lifx.LFXLight; //導入方法依賴的package包/類
@Override
public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int intensity,
Integer targetBri, Integer targetBriInc, ColorData colorData, DeviceDescriptor device, String body) {
String theReturn = null;
float aBriValue;
float theValue;
log.debug("executing HUE api request to send message to LifxDevice: " + anItem.getItem().toString());
if(!validLifx) {
log.warn("Should not get here, no LifxDevice clients configured");
theReturn = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId
+ "\",\"description\": \"Should not get here, no LifxDevices configured\", \"parameter\": \"/lights/"
+ lightId + "state\"}}]";
} else {
LifxEntry lifxCommand = null;
if(anItem.getItem().isJsonObject())
lifxCommand = aGsonHandler.fromJson(anItem.getItem(), LifxEntry.class);
else
lifxCommand = aGsonHandler.fromJson(anItem.getItem().getAsString(), LifxEntry.class);
LifxDevice theDevice = getLifxDevice(lifxCommand.getName());
if (theDevice == null) {
log.warn("Should not get here, no LifxDevices available");
theReturn = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId
+ "\",\"description\": \"Should not get here, no Lifx clients available\", \"parameter\": \"/lights/"
+ lightId + "state\"}}]";
} else {
log.debug("calling LifxDevice: " + lifxCommand.getName());
if(theDevice.getType().equals(LifxDevice.LIGHT_TYPE)) {
LFXLight theLight = (LFXLight)theDevice.getLifxObject();
if(body.contains("true"))
theLight.setPower(true);
if(body.contains("false"))
theLight.setPower(false);
if(targetBri != null || targetBriInc != null) {
aBriValue = (float)BrightnessDecode.calculateIntensity(intensity, targetBri, targetBriInc);
theValue = aBriValue/DIM_DIVISOR;
if(theValue > (float)1.0)
theValue = (float)0.99;
theLight.setBrightness(theValue);
}
} else if (theDevice.getType().equals(LifxDevice.GROUP_TYPE)) {
LFXGroup theGroup = (LFXGroup)theDevice.getLifxObject();
if(body.contains("true"))
theGroup.setPower(true);
if(body.contains("false"))
theGroup.setPower(false);
}
}
}
return theReturn;
}