use of com.bwssystems.HABridge.hue.ColorData in project ha-bridge by bwssytems.
the class HueMulator method changeState.
private String changeState(String userId, String lightId, String body, String ipAddress, boolean ignoreRequester) {
if (bridgeSettings.isUserooms() && Integer.parseInt(lightId) >= 10000) {
return changeGroupState(userId, String.valueOf(Integer.parseInt(lightId) - 10000), body, ipAddress, true);
}
String responseString = null;
String url = null;
StateChangeBody theStateChanges = null;
DeviceState state = null;
Integer targetBri = null;
Integer targetBriInc = null;
boolean isColorRequest = false;
boolean isDimRequest = false;
boolean isOnRequest = false;
ColorData colorData = null;
if (bridgeSettings.isTracestate())
log.info("Tracestate: hue state change requested: " + userId + " from " + ipAddress + " body: " + body);
log.debug("hue state change requested: " + userId + " from " + ipAddress + " body: " + body);
HueError[] theErrors = bridgeSettingMaster.getBridgeSecurity().validateWhitelistUser(userId, null, bridgeSettingMaster.getBridgeSecurity().isUseLinkButton());
if (theErrors != null) {
log.warn("Errors in security: <<<" + aGsonHandler.toJson(theErrors) + ">>>");
return aGsonHandler.toJson(theErrors);
}
try {
theStateChanges = aGsonHandler.fromJson(body, StateChangeBody.class);
} catch (Exception e) {
theStateChanges = null;
}
if (theStateChanges == null) {
log.warn("Could not parse state change body. Light state not changed.");
return aGsonHandler.toJson(HueErrorResponse.createResponse("2", "/lights/" + lightId, "Could not parse state change body.", null, null, null).getTheErrors(), HueError[].class);
}
DeviceDescriptor device = repository.findOne(lightId);
if (device == null) {
log.warn("Could not find device: " + lightId + " for hue state change request: " + userId + " from " + ipAddress + " body: " + body);
return aGsonHandler.toJson(HueErrorResponse.createResponse("3", "/lights/" + lightId, "Could not find device.", "/lights/" + lightId, null, null).getTheErrors(), HueError[].class);
}
state = device.getDeviceState();
if (state == null) {
state = DeviceState.createDeviceState(device.isColorDevice());
device.setDeviceState(state);
}
if (body.contains("\"bri_inc\"")) {
targetBriInc = Integer.valueOf(theStateChanges.getBri_inc());
isDimRequest = true;
} else if (body.contains("\"bri\"")) {
targetBri = Integer.valueOf(theStateChanges.getBri());
isDimRequest = true;
}
colorData = parseColorInfo(body, theStateChanges, state, targetBri, targetBriInc);
if (colorData != null)
isColorRequest = true;
if (body.contains("\"on\"")) {
isOnRequest = true;
}
if (device.isOnFirstDim()) {
if (isDimRequest && !device.getDeviceState().isOn()) {
isOnRequest = true;
theStateChanges.setOn(true);
// isDimRequest = false;
// isColorRequest = false;
} else if (isDimRequest && device.getDeviceState().isOn()) {
if (device.getDeviceState().getBri() == theStateChanges.getBri()) {
isOnRequest = true;
theStateChanges.setOn(true);
// isDimRequest = false;
// isColorRequest = false;
} else {
isOnRequest = false;
// isDimRequest = true;
// isColorRequest = false;
}
}
} else if (device.isOnWhenDimPresent()) {
if (isDimRequest) {
isOnRequest = true;
theStateChanges.setOn(true);
}
} else if (device.isDimNoOn()) {
if (isDimRequest && isOnRequest) {
isOnRequest = false;
}
}
if (isColorRequest && isDimRequest && !device.isDimOnColor()) {
isDimRequest = false;
}
if (isOnRequest) {
if (bridgeSettings.isTracestate())
log.info("Tracestate: Calling on-off as requested: " + theStateChanges.isOn());
log.debug("Calling on-off as requested.");
if (theStateChanges.isOn()) {
url = device.getOnUrl();
} else if (!theStateChanges.isOn()) {
url = device.getOffUrl();
}
// code for backwards compatibility
if (device.getMapType() != null && device.getMapType().equalsIgnoreCase(DeviceMapTypes.HUE_DEVICE[DeviceMapTypes.typeIndex])) {
if (url == null)
url = device.getOnUrl();
}
if (url != null && !url.equals("")) {
responseString = callUrl(url, device, userId, lightId, body, ipAddress, ignoreRequester, targetBri, targetBriInc, colorData);
if (responseString != null && responseString.contains("[{\"error\":")) {
log.warn("On/Off Request failed with: " + responseString);
}
} else {
log.info("On/off url not available for state change, lightId: " + lightId + ", userId: " + userId + ", from IP: " + ipAddress + ", body: " + body);
}
}
if (isDimRequest) {
if (bridgeSettings.isTracestate())
log.info("Tracestate: Calling dim as requested: " + targetBri + ", inc: " + targetBriInc);
log.debug("Calling dim as requested.");
url = device.getDimUrl();
// code for backwards compatibility
if (device.getMapType() != null && device.getMapType().equalsIgnoreCase(DeviceMapTypes.HUE_DEVICE[DeviceMapTypes.typeIndex])) {
if (url == null)
url = device.getOnUrl();
}
if (url != null && !url.equals("")) {
if (isOnRequest) {
try {
Thread.sleep(bridgeSettings.getButtonsleep());
} catch (InterruptedException e) {
// ignore
}
}
responseString = callUrl(url, device, userId, lightId, body, ipAddress, ignoreRequester, targetBri, targetBriInc, colorData);
if (responseString != null && responseString.contains("[{\"error\":")) {
log.warn("Dim Request failed with: " + responseString);
}
} else {
log.info("Dim url not available for state change, lightId: " + lightId + ", userId: " + userId + ", from IP: " + ipAddress + ", body: " + body);
}
}
if (isColorRequest) {
if (bridgeSettings.isTracestate())
log.info("Tracestate: Calling color as requested. With " + colorData);
log.debug("Calling color as requested.");
url = device.getColorUrl();
// code for backwards compatibility
if (device.getMapType() != null && device.getMapType().equalsIgnoreCase(DeviceMapTypes.HUE_DEVICE[DeviceMapTypes.typeIndex])) {
if (url == null)
url = device.getOnUrl();
}
if (url != null && !url.equals("")) {
if ((isOnRequest && !isDimRequest) || isDimRequest) {
try {
Thread.sleep(bridgeSettings.getButtonsleep());
} catch (InterruptedException e) {
// ignore
}
}
responseString = callUrl(url, device, userId, lightId, body, ipAddress, ignoreRequester, targetBri, targetBriInc, colorData);
if (responseString != null && responseString.contains("[{\"error\":")) {
log.warn("Color Request failed with: " + responseString);
}
} else {
log.info("Color url not available for state change, lightId: " + lightId + ", userId: " + userId + ", from IP: " + ipAddress + ", body: " + body);
}
}
if (responseString == null || !responseString.contains("[{\"error\":")) {
if (!device.isNoState()) {
responseString = this.formatSuccessHueResponse(theStateChanges, body, lightId, state, targetBri, targetBriInc, colorData, device.isOffState());
device.setDeviceState(state);
} else {
DeviceState dummyState = DeviceState.createDeviceState(device.isColorDevice());
responseString = this.formatSuccessHueResponse(theStateChanges, body, lightId, dummyState, targetBri, targetBriInc, colorData, device.isOffState());
}
}
return responseString;
}
use of com.bwssystems.HABridge.hue.ColorData in project ha-bridge by bwssytems.
the class HueMulator method parseColorInfo.
private ColorData parseColorInfo(String body, StateChangeBody theStateChanges, DeviceState state, Integer targetBri, Integer targetBriInc) {
ColorData colorData = null;
List<Double> xy = null;
List<Double> xyInc = null;
Integer ct = null;
Integer ctInc = null;
HueSatBri anHSL = null;
Integer hue = null;
Integer sat = null;
Integer hueInc = null;
Integer satInc = null;
if (body.contains("\"xy\"")) {
xy = theStateChanges.getXy();
}
if (body.contains("\"ct\"")) {
ct = theStateChanges.getCt();
}
if (body.contains("\"hue\"")) {
hue = theStateChanges.getHue();
}
if (body.contains("\"sat\"")) {
sat = theStateChanges.getSat();
}
if (body.contains("\"xy_inc\"")) {
xyInc = theStateChanges.getXy_inc();
}
if (body.contains("\"ct_inc\"")) {
ctInc = theStateChanges.getCt_inc();
}
if (body.contains("\"hue_inc\"")) {
hueInc = theStateChanges.getHue_inc();
}
if (body.contains("\"sat_inc\"")) {
satInc = theStateChanges.getSat_inc();
}
if (xy != null && xy.size() == 2) {
colorData = new ColorData(ColorData.ColorMode.XY, xy);
} else if (xyInc != null && xyInc.size() == 2) {
List<Double> current = state.getXy();
current.set(0, current.get(0) + xyInc.get(0));
current.set(1, current.get(1) + xyInc.get(1));
colorData = new ColorData(ColorData.ColorMode.XY, current);
} else if (ct != null) {
colorData = new ColorData(ColorData.ColorMode.CT, ct);
} else if (ctInc != null) {
colorData = new ColorData(ColorData.ColorMode.CT, state.getCt() + ctInc);
} else if (hue != null || sat != null) {
anHSL = new HueSatBri();
int bri = 0;
if (targetBriInc != null) {
bri = state.getBri() - targetBriInc;
if (bri < 1)
bri = 1;
} else if (targetBri != null) {
bri = targetBri;
} else {
bri = state.getBri();
}
anHSL.setBri(bri);
if (hue != null)
anHSL.setHue(hue);
else
anHSL.setHue(state.getHue());
if (sat != null)
anHSL.setSat(sat);
else
anHSL.setSat(state.getSat());
log.debug("hue/sat request - " + anHSL);
colorData = new ColorData(ColorData.ColorMode.HS, anHSL);
} else if (hueInc != null || satInc != null) {
anHSL = new HueSatBri();
int bri = 0;
if (targetBriInc != null) {
bri = state.getBri() - targetBriInc;
if (bri < 1)
bri = 1;
} else if (targetBri != null) {
bri = targetBri;
} else {
bri = state.getBri();
}
anHSL.setBri(bri);
if (hueInc != null)
anHSL.setHue(state.getHue() - hueInc);
else
anHSL.setHue(state.getHue());
if (satInc != null)
anHSL.setSat(state.getSat() - satInc);
else
anHSL.setSat(state.getSat());
log.info("hue/sat inc request - " + anHSL);
colorData = new ColorData(ColorData.ColorMode.HS, anHSL);
}
return colorData;
}
use of com.bwssystems.HABridge.hue.ColorData in project ha-bridge by bwssytems.
the class HueMulator method updateState.
private String updateState(String userId, String lightId, String body, String ipAddress) {
String responseString = null;
StateChangeBody theStateChanges = null;
DeviceState state = null;
Integer targetBri = null;
Integer targetBriInc = null;
ColorData colorData = null;
if (bridgeSettings.isTracestate())
log.info("Tracestate: Update state requested: " + userId + " from " + ipAddress + " body: " + body);
log.debug("Update state requested: " + userId + " from " + ipAddress + " body: " + body);
HueError[] theErrors = bridgeSettingMaster.getBridgeSecurity().validateWhitelistUser(userId, null, bridgeSettingMaster.getBridgeSecurity().isUseLinkButton());
if (theErrors != null)
return aGsonHandler.toJson(theErrors);
try {
theStateChanges = aGsonHandler.fromJson(body, StateChangeBody.class);
} catch (Exception e) {
log.warn("Could not parse state change body. Light state not changed.");
return aGsonHandler.toJson(HueErrorResponse.createResponse("2", "/lights/" + lightId, "Could not parse state change body.", null, null, null).getTheErrors(), HueError[].class);
}
DeviceDescriptor device = repository.findOne(lightId);
if (device == null) {
log.warn("Could not find device: " + lightId + " for hue state change request: " + userId + " from " + ipAddress + " body: " + body);
return aGsonHandler.toJson(HueErrorResponse.createResponse("3", "/lights/" + lightId, "Could not find device.", "/lights/" + lightId, null, null).getTheErrors(), HueError[].class);
}
if (body.contains("\"bri_inc\""))
targetBriInc = Integer.valueOf(theStateChanges.getBri_inc());
else if (body.contains("\"bri\"")) {
targetBri = Integer.valueOf(theStateChanges.getBri());
}
state = device.getDeviceState();
if (state == null)
state = DeviceState.createDeviceState(device.isColorDevice());
colorData = parseColorInfo(body, theStateChanges, state, targetBri, targetBriInc);
responseString = this.formatSuccessHueResponse(theStateChanges, body, lightId, state, targetBri, targetBriInc, colorData, device.isOffState());
device.setDeviceState(state);
return responseString;
}
use of com.bwssystems.HABridge.hue.ColorData in project ha-bridge by bwssytems.
the class ConvertCIEColorTestCase method testColorConversionXYtoRGB1.
@Test
public void testColorConversionXYtoRGB1() {
ArrayList<Double> xy = new ArrayList<Double>(Arrays.asList(Double.parseDouble("0.671254"), Double.parseDouble("0.303273")));
List<Integer> colorDecode = ColorDecode.convertCIEtoRGB(xy, 50);
List<Integer> assertDecode = new ArrayList<Integer>();
assertDecode.add(0, 255);
assertDecode.add(1, 0);
assertDecode.add(2, 5);
Assert.assertEquals(colorDecode, assertDecode);
ColorData colorData = new ColorData(ColorData.ColorMode.XY, xy);
int rgbIntVal = ColorDecode.getIntRGB(colorData, 50);
Assert.assertEquals(rgbIntVal, 16711685);
}
use of com.bwssystems.HABridge.hue.ColorData in project ha-bridge by bwssytems.
the class HueMulator method changeGroupState.
@SuppressWarnings("unchecked")
private String changeGroupState(String userId, String groupId, String body, String ipAddress, boolean fakeLightResponse) {
ColorData colorData = null;
log.debug("PUT action to group " + groupId + " from " + ipAddress + " user " + userId + " with body " + body);
HueError[] theErrors = null;
theErrors = bridgeSettingMaster.getBridgeSecurity().validateWhitelistUser(userId, null, bridgeSettingMaster.getBridgeSecurity().isUseLinkButton());
if (theErrors == null) {
if (bridgeSettingMaster.getBridgeSecurity().isSettingsChanged())
bridgeSettingMaster.updateConfigFile();
GroupDescriptor group = null;
Integer targetBriInc = null;
Integer targetBri = null;
DeviceState state = null;
Map<String, DeviceResponse> lights = null;
if (groupId.equalsIgnoreCase("0")) {
lights = (Map<String, DeviceResponse>) lightsListHandler(userId, ipAddress);
} else {
group = groupRepository.findOne(groupId);
if (group == null || group.isInactive()) {
return aGsonHandler.toJson(HueErrorResponse.createResponse("3", "/groups/" + groupId, "resource, /groups/" + groupId + ", not available", null, null, null).getTheErrors(), HueError[].class);
} else {
if (fakeLightResponse) {
lights = repository.findAllByGroupWithState(group.getLights(), ipAddress, myHueHome, aGsonHandler, true);
} else {
lights = repository.findAllByGroupWithState(group.getLights(), ipAddress, myHueHome, aGsonHandler);
}
}
}
if (lights != null) {
StateChangeBody theStateChanges = null;
try {
theStateChanges = aGsonHandler.fromJson(body, StateChangeBody.class);
} catch (Exception e) {
theStateChanges = null;
}
if (theStateChanges == null) {
log.warn("Could not parse state change body. Light state not changed.");
return aGsonHandler.toJson(HueErrorResponse.createResponse("2", "/groups/" + groupId + "/action", "Could not parse state change body.", null, null, null).getTheErrors(), HueError[].class);
}
if (group != null) {
if (body.contains("\"bri_inc\"")) {
targetBriInc = Integer.valueOf(theStateChanges.getBri_inc());
} else if (body.contains("\"bri\"")) {
targetBri = Integer.valueOf(theStateChanges.getBri());
}
state = group.getAction();
if (state == null) {
state = DeviceState.createDeviceState(true);
group.setAction(state);
}
}
boolean turnOn = false;
boolean turnOff = false;
if (!(body.contains("\"bri_inc\"") || body.contains("\"bri\""))) {
if (!(body.contains("\"xy\"") || body.contains("\"ct\"") || body.contains("\"hue\""))) {
if (theStateChanges.isOn()) {
turnOn = true;
} else if (!theStateChanges.isOn()) {
turnOff = true;
}
}
}
for (Map.Entry<String, DeviceResponse> light : lights.entrySet()) {
log.debug("Processing light" + light.getKey() + ": " + turnOn + " " + turnOff + " " + light.getValue().getState().isOn());
// ignore on/off for devices that are already on/off
if (turnOff && !light.getValue().getState().isOn())
continue;
if (turnOn && light.getValue().getState().isOn())
continue;
changeState(userId, light.getKey(), body, ipAddress, fakeLightResponse);
}
// per light
if (group != null) {
// if not group 0
String response = formatSuccessHueResponse(theStateChanges, body, String.valueOf(Integer.parseInt(groupId) + 10000), state, targetBri, targetBriInc, colorData, true);
group.setAction(state);
if (fakeLightResponse) {
return response;
}
}
String successString = "[";
for (String pairStr : body.replaceAll("[{|}]", "").split(",\\s*\"")) {
String[] pair = pairStr.split(":");
if (fakeLightResponse) {
successString += "{\"success\":{ \"/lights/" + String.valueOf(Integer.parseInt(groupId) + 10000) + "/state/" + pair[0].replaceAll("\"", "").trim() + "\": " + pair[1].trim() + "}},";
} else {
successString += "{\"success\":{ \"address\": \"/groups/" + groupId + "/action/" + pair[0].replaceAll("\"", "").trim() + "\", \"value\": " + pair[1].trim() + "}},";
}
}
return (successString.length() == 1) ? "[]" : successString.substring(0, successString.length() - 1) + "]";
}
}
return aGsonHandler.toJson(theErrors);
}
Aggregations