use of com.bwssystems.HABridge.api.hue.DeviceState in project ha-bridge by bwssytems.
the class HueMulator method changeState.
private String changeState(String userId, String lightId, String body, String ipAddress) {
String responseString = null;
String url = null;
StateChangeBody theStateChanges = null;
DeviceState state = null;
Integer targetBri = null;
Integer targetBriInc = null;
MultiCommandUtil aMultiUtil = new MultiCommandUtil();
aMultiUtil.setTheDelay(bridgeSettings.getButtonsleep());
aMultiUtil.setDelayDefault(bridgeSettings.getButtonsleep());
aMultiUtil.setSetCount(1);
log.debug("hue state change 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) {
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);
}
if (body.contains("\"bri_inc\"")) {
targetBriInc = new Integer(theStateChanges.getBri_inc());
} else if (body.contains("\"bri\"")) {
targetBri = new Integer(theStateChanges.getBri());
}
state = device.getDeviceState();
if (state == null) {
state = DeviceState.createDeviceState();
device.setDeviceState(state);
}
if (targetBri != null || targetBriInc != null) {
url = device.getDimUrl();
if (url == null || url.length() == 0)
url = device.getOnUrl();
} else {
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("")) {
if (!url.startsWith("[")) {
if (url.startsWith("{\"item"))
url = "[" + url + "]";
else {
if (url.startsWith("{"))
url = "[{\"item\":" + url + "}]";
else
url = "[{\"item\":\"" + url + "\"}]";
}
} else if (!url.startsWith("[{\"item\""))
url = "[{\"item\":" + url + "}]";
log.debug("Decode Json for url items: " + url);
CallItem[] callItems = null;
try {
callItems = aGsonHandler.fromJson(url, CallItem[].class);
} catch (JsonSyntaxException e) {
log.warn("Could not decode Json for url items: " + lightId + " for hue state change request: " + userId + " from " + ipAddress + " body: " + body + " url items: " + url);
return aGsonHandler.toJson(HueErrorResponse.createResponse("3", "/lights/" + lightId, "Could decode json in request", "/lights/" + lightId, null, null).getTheErrors(), HueError[].class);
}
for (int i = 0; callItems != null && i < callItems.length; i++) {
if (!filterByRequester(device.getRequesterAddress(), ipAddress) || !filterByRequester(callItems[i].getFilterIPs(), ipAddress)) {
log.warn("filter for requester address not present in: (device)" + device.getRequesterAddress() + " OR then (item)" + callItems[i].getFilterIPs() + " with request ip of: " + ipAddress);
continue;
}
if (callItems[i].getCount() != null && callItems[i].getCount() > 0)
aMultiUtil.setSetCount(callItems[i].getCount());
else
aMultiUtil.setSetCount(1);
// code for backwards compatibility
if ((callItems[i].getType() == null || callItems[i].getType().trim().isEmpty())) {
if (validMapTypes.validateType(device.getMapType()))
callItems[i].setType(device.getMapType().trim());
else if (validMapTypes.validateType(device.getDeviceType()))
callItems[i].setType(device.getDeviceType().trim());
else
callItems[i].setType(DeviceMapTypes.CUSTOM_DEVICE[DeviceMapTypes.typeIndex]);
}
if (callItems[i].getType() != null) {
for (int x = 0; x < aMultiUtil.getSetCount(); x++) {
if (x > 0 || i > 0) {
try {
Thread.sleep(aMultiUtil.getTheDelay());
} catch (InterruptedException e) {
// ignore
}
}
if (callItems[i].getDelay() != null && callItems[i].getDelay() > 0)
aMultiUtil.setTheDelay(callItems[i].getDelay());
else
aMultiUtil.setTheDelay(aMultiUtil.getDelayDefault());
responseString = homeManager.findHome(callItems[i].getType().trim()).deviceHandler(callItems[i], aMultiUtil, lightId, state.getBri(), targetBri, targetBriInc, device, body);
if (responseString != null && responseString.contains("{\"error\":")) {
x = aMultiUtil.getSetCount();
}
}
}
}
} else {
log.warn("Could not find url: " + lightId + " for hue state change request: " + userId + " from " + ipAddress + " body: " + body);
responseString = aGsonHandler.toJson(HueErrorResponse.createResponse("3", "/lights/" + lightId, "Could not find url.", "/lights/" + lightId, null, null).getTheErrors(), HueError[].class);
}
if (responseString == null || !responseString.contains("[{\"error\":")) {
if (!device.isNoState()) {
responseString = this.formatSuccessHueResponse(theStateChanges, body, lightId, state, targetBri, targetBriInc, device.isOffState());
device.setDeviceState(state);
} else {
DeviceState dummyState = DeviceState.createDeviceState();
responseString = this.formatSuccessHueResponse(theStateChanges, body, lightId, dummyState, targetBri, targetBriInc, device.isOffState());
}
}
return responseString;
}
use of com.bwssystems.HABridge.api.hue.DeviceState 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.api.hue.DeviceState 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.api.hue.DeviceState 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