diff --git a/pom.xml b/pom.xml index e34ba1f..b5b61f9 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ com.bwssystems.HABridge ha-bridge - 4beta2 + 4beta2.2 jar HA Bridge diff --git a/src/main/java/com/bwssystems/HABridge/hue/BrightnessDecode.java b/src/main/java/com/bwssystems/HABridge/hue/BrightnessDecode.java index 201098f..0c26dfd 100644 --- a/src/main/java/com/bwssystems/HABridge/hue/BrightnessDecode.java +++ b/src/main/java/com/bwssystems/HABridge/hue/BrightnessDecode.java @@ -10,9 +10,6 @@ import javax.xml.bind.DatatypeConverter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.bwssystems.HABridge.api.hue.DeviceState; -import com.bwssystems.HABridge.api.hue.StateChangeBody; - import net.java.dev.eval.Expression; public class BrightnessDecode { @@ -23,17 +20,16 @@ public class BrightnessDecode { private static final String INTENSITY_MATH_VALUE = "X"; private static final String INTENSITY_MATH_CLOSE = ")}"; - public static int calculateIntensity(DeviceState state, StateChangeBody theChanges, boolean hasBri, boolean hasBriInc) { - int setIntensity = state.getBri(); - if (hasBri) { - setIntensity = theChanges.getBri(); - } else if (hasBriInc) { - if ((setIntensity + theChanges.getBri_inc()) <= 0) - setIntensity = theChanges.getBri_inc(); - else if ((setIntensity + theChanges.getBri_inc()) > 255) - setIntensity = theChanges.getBri_inc(); + public static int calculateIntensity(int setIntensity, Integer targetBri, Integer targetBriInc) { + if (targetBri != null) { + setIntensity = targetBri; + } else if (targetBriInc != null) { + if ((setIntensity + targetBriInc) <= 0) + setIntensity = targetBriInc; + else if ((setIntensity + targetBriInc) > 255) + setIntensity = targetBriInc; else - setIntensity = setIntensity + theChanges.getBri_inc(); + setIntensity = setIntensity + targetBriInc; } return setIntensity; } @@ -101,7 +97,7 @@ public class BrightnessDecode { } // Helper Method - public static String calculateReplaceIntensityValue(String request, DeviceState state, StateChangeBody theChanges, boolean hasBri, boolean hasBriInc, boolean isHex) { - return replaceIntensityValue(request, calculateIntensity(state, theChanges, hasBri, hasBriInc), isHex); + public static String calculateReplaceIntensityValue(String request, int theIntensity, Integer targetBri, Integer targetBriInc, boolean isHex) { + return replaceIntensityValue(request, calculateIntensity(theIntensity, targetBri, targetBriInc), isHex); } } diff --git a/src/main/java/com/bwssystems/HABridge/hue/HueMulator.java b/src/main/java/com/bwssystems/HABridge/hue/HueMulator.java index 76e616f..ea58628 100644 --- a/src/main/java/com/bwssystems/HABridge/hue/HueMulator.java +++ b/src/main/java/com/bwssystems/HABridge/hue/HueMulator.java @@ -624,8 +624,8 @@ public class HueMulator { String responseString = null; StateChangeBody theStateChanges = null; DeviceState state = null; - boolean stateHasBri = false; - boolean stateHasBriInc = false; + Integer targetBri = null; + Integer targetBriInc = null; log.debug("Update state requested: " + userId + " from " + ipAddress + " body: " + body); HueError[] theErrors = validateWhitelistUser(userId, false); if (theErrors != null) @@ -637,15 +637,6 @@ public class HueMulator { "Could not parse state change body.", null, null, null).getTheErrors(), HueError[].class); } - if (body.contains("\"bri\"")) { - if (theStateChanges.isOn() && theStateChanges.getBri() == 0) - stateHasBri = false; - else - stateHasBri = true; - } - if (body.contains("\"bri_inc\"")) - stateHasBriInc = true; - DeviceDescriptor device = repository.findOne(lightId); if (device == null) { log.warn("Could not find device: " + lightId + " for hue state change request: " + userId + " from " @@ -653,17 +644,27 @@ public class HueMulator { 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\"")) { + if (theStateChanges.isOn() && theStateChanges.getBri() == 0) + targetBri = null; + else + targetBri = new Integer(theStateChanges.getBri()); + } + state = device.getDeviceState(); if (state == null) state = DeviceState.createDeviceState(); state.fillIn(); - if (stateHasBri) { - if (theStateChanges.getBri() > 0 && !state.isOn()) + if (targetBri != null) { + if (targetBri > 0 && !state.isOn()) state.setOn(true); - } else if (stateHasBriInc) { - if ((state.getBri() + theStateChanges.getBri_inc()) > 0 && !state.isOn()) + } else if (targetBriInc != null) { + if ((state.getBri() + targetBriInc) > 0 && !state.isOn()) state.setOn(true); - else if ((state.getBri() + theStateChanges.getBri_inc()) <= 0 && state.isOn()) + else if ((state.getBri() + targetBriInc) <= 0 && state.isOn()) state.setOn(false); } else { if (theStateChanges.isOn()) { @@ -676,10 +677,9 @@ public class HueMulator { } } responseString = this.formatSuccessHueResponse(theStateChanges, body, lightId, device.getDeviceState()); - device.getDeviceState().setBri(BrightnessDecode.calculateIntensity(state, theStateChanges, stateHasBri, stateHasBriInc)); + device.getDeviceState().setBri(BrightnessDecode.calculateIntensity(state.getBri(), targetBri, targetBriInc)); return responseString; - } private String changeState(String userId, String lightId, String body, String ipAddress) { @@ -687,9 +687,9 @@ public class HueMulator { String url = null; StateChangeBody theStateChanges = null; DeviceState state = null; + Integer targetBri = null; + Integer targetBriInc = null; MultiCommandUtil aMultiUtil = new MultiCommandUtil(); - boolean stateHasBri = false; - boolean stateHasBriInc = false; aMultiUtil.setTheDelay(bridgeSettings.getButtonsleep()); aMultiUtil.setDelayDefault(bridgeSettings.getButtonsleep()); aMultiUtil.setSetCount(1); @@ -705,12 +705,6 @@ public class HueMulator { "Could not parse state change body.", null, null, null).getTheErrors(), HueError[].class); } - if (body.contains("\"bri\"")) { - stateHasBri = true; - } - if (body.contains("\"bri_inc\"")) - stateHasBriInc = true; - DeviceDescriptor device = repository.findOne(lightId); if (device == null) { log.warn("Could not find device: " + lightId + " for hue state change request: " + userId + " from " @@ -719,26 +713,23 @@ public class HueMulator { "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(); - if (stateHasBri) { + if (targetBri != null || targetBriInc != null) { if(!state.isOn()) state.setOn(true); url = device.getDimUrl(); - if (url == null || url.length() == 0) - url = device.getOnUrl(); - } else if (stateHasBriInc) { - if(!state.isOn()) - state.setOn(true); - if ((state.getBri() + theStateChanges.getBri_inc()) <= 0) - state.setBri(theStateChanges.getBri_inc()); - - url = device.getDimUrl(); - if (url == null || url.length() == 0) url = device.getOnUrl(); } else { @@ -792,7 +783,20 @@ public class HueMulator { } if (callItems[i].getType() != null) { - responseString = homeManager.findHome(callItems[i].getType().trim()).deviceHandler(callItems[i], aMultiUtil, lightId, i, state, theStateChanges, stateHasBri, stateHasBriInc, device, body); + 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); + } } } } else { @@ -804,7 +808,7 @@ public class HueMulator { if (responseString == null || !responseString.contains("[{\"error\":")) { responseString = this.formatSuccessHueResponse(theStateChanges, body, lightId, state); - state.setBri(BrightnessDecode.calculateIntensity(state, theStateChanges, stateHasBri, stateHasBriInc)); + state.setBri(BrightnessDecode.calculateIntensity(state.getBri(), targetBri, targetBriInc)); device.setDeviceState(state); } return responseString; diff --git a/src/main/java/com/bwssystems/HABridge/hue/HueMulatorHandler.java b/src/main/java/com/bwssystems/HABridge/hue/HueMulatorHandler.java index 24d4a4b..af3e6e2 100644 --- a/src/main/java/com/bwssystems/HABridge/hue/HueMulatorHandler.java +++ b/src/main/java/com/bwssystems/HABridge/hue/HueMulatorHandler.java @@ -1,10 +1,8 @@ package com.bwssystems.HABridge.hue; import com.bwssystems.HABridge.api.CallItem; -import com.bwssystems.HABridge.api.hue.DeviceState; -import com.bwssystems.HABridge.api.hue.StateChangeBody; import com.bwssystems.HABridge.dao.DeviceDescriptor; public interface HueMulatorHandler { - public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int iterationCount, DeviceState state, StateChangeBody theStateChanges, boolean stateHasBri, boolean stateHasBriInc, DeviceDescriptor device, String body); + public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int intensity, Integer targetBri, Integer targetBriInc, DeviceDescriptor device, String body); } diff --git a/src/main/java/com/bwssystems/HABridge/plugins/NestBridge/NestHome.java b/src/main/java/com/bwssystems/HABridge/plugins/NestBridge/NestHome.java index df08034..8af25a3 100644 --- a/src/main/java/com/bwssystems/HABridge/plugins/NestBridge/NestHome.java +++ b/src/main/java/com/bwssystems/HABridge/plugins/NestBridge/NestHome.java @@ -10,8 +10,6 @@ import org.slf4j.LoggerFactory; import com.bwssystems.HABridge.BridgeSettingsDescriptor; import com.bwssystems.HABridge.DeviceMapTypes; import com.bwssystems.HABridge.api.CallItem; -import com.bwssystems.HABridge.api.hue.DeviceState; -import com.bwssystems.HABridge.api.hue.StateChangeBody; import com.bwssystems.HABridge.dao.DeviceDescriptor; import com.bwssystems.HABridge.hue.BrightnessDecode; import com.bwssystems.HABridge.hue.MultiCommandUtil; @@ -103,8 +101,8 @@ public class NestHome implements com.bwssystems.HABridge.Home { } @Override - public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int iterationCount, - DeviceState state, StateChangeBody theStateChanges, boolean stateHasBri, boolean stateHasBriInc, DeviceDescriptor device, String body) { + public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int intensity, + Integer targetBri,Integer targetBriInc, DeviceDescriptor device, String body) { String responseString = null; log.debug("executing HUE api request to set away for nest " + anItem.getType() + ": " + anItem.getItem().toString()); if(!validNest) { @@ -118,18 +116,18 @@ public class NestHome implements com.bwssystems.HABridge.Home { } else if (anItem.getType() != null && anItem.getType().trim().equalsIgnoreCase(DeviceMapTypes.NEST_THERMO_SET[DeviceMapTypes.typeIndex])) { NestInstruction thermoSetting = aGsonHandler.fromJson(anItem.getItem().toString(), NestInstruction.class); if (thermoSetting.getControl().equalsIgnoreCase("temp")) { - if (stateHasBri) { + if (targetBri != null) { if (isFarenheit) thermoSetting .setTemp( String.valueOf((Double .parseDouble(BrightnessDecode.calculateReplaceIntensityValue(thermoSetting.getTemp(), - state, theStateChanges, stateHasBri, stateHasBriInc, false)) - 32.0) / 1.8)); + intensity, targetBri, targetBriInc, false)) - 32.0) / 1.8)); else thermoSetting .setTemp( String.valueOf(Double.parseDouble(BrightnessDecode.calculateReplaceIntensityValue(thermoSetting.getTemp(), - state, theStateChanges, stateHasBri, stateHasBriInc, false)))); + intensity, targetBri, targetBriInc, false)))); log.debug("Setting thermostat: " + thermoSetting.getName() + " to " + thermoSetting.getTemp() + "C"); theNest.getThermostat(thermoSetting.getName()) diff --git a/src/main/java/com/bwssystems/HABridge/plugins/exec/CommandHome.java b/src/main/java/com/bwssystems/HABridge/plugins/exec/CommandHome.java index 85b6508..71ef5f0 100644 --- a/src/main/java/com/bwssystems/HABridge/plugins/exec/CommandHome.java +++ b/src/main/java/com/bwssystems/HABridge/plugins/exec/CommandHome.java @@ -8,8 +8,6 @@ import org.slf4j.LoggerFactory; import com.bwssystems.HABridge.BridgeSettingsDescriptor; import com.bwssystems.HABridge.Home; import com.bwssystems.HABridge.api.CallItem; -import com.bwssystems.HABridge.api.hue.DeviceState; -import com.bwssystems.HABridge.api.hue.StateChangeBody; import com.bwssystems.HABridge.dao.DeviceDescriptor; import com.bwssystems.HABridge.hue.BrightnessDecode; import com.bwssystems.HABridge.hue.MultiCommandUtil; @@ -23,33 +21,18 @@ public class CommandHome implements Home { } @Override - public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int iterationCount, - DeviceState state, StateChangeBody theStateChanges, boolean stateHasBri, boolean stateHasBriInc, DeviceDescriptor device, String body) { - log.debug("Exec Request called with url: " + anItem.getItem().toString()); + public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int itensity, Integer targetBri, Integer targetBriInc, DeviceDescriptor device, String body) { + log.debug("Exec Request called with url: " + anItem.getItem().getAsString()); String responseString = null; String intermediate; if (anItem.getItem().toString().contains("exec://")) - intermediate = anItem.getItem().toString().substring(anItem.getItem().toString().indexOf("://") + 3); + intermediate = anItem.getItem().getAsString().substring(anItem.getItem().toString().indexOf("://") + 3); else - intermediate = anItem.getItem().toString(); - for (int x = 0; x < aMultiUtil.getSetCount(); x++) { - if (x > 0 || iterationCount > 0) { - try { - Thread.sleep(aMultiUtil.getTheDelay()); - } catch (InterruptedException e) { - // ignore - } - } - if (anItem.getDelay() != null && anItem.getDelay() > 0) - aMultiUtil.setTheDelay(anItem.getDelay()); - else - aMultiUtil.setTheDelay(aMultiUtil.getDelayDefault()); - String anError = doExecRequest(intermediate, - BrightnessDecode.calculateIntensity(state, theStateChanges, stateHasBri, stateHasBriInc), lightId); - if (anError != null) { - responseString = anError; - x = aMultiUtil.getSetCount(); - } + intermediate = anItem.getItem().getAsString(); + String anError = doExecRequest(intermediate, + BrightnessDecode.calculateIntensity(itensity, targetBri, targetBriInc), lightId); + if (anError != null) { + responseString = anError; } return responseString; } diff --git a/src/main/java/com/bwssystems/HABridge/plugins/hal/HalHome.java b/src/main/java/com/bwssystems/HABridge/plugins/hal/HalHome.java index 4b23433..a153bf7 100644 --- a/src/main/java/com/bwssystems/HABridge/plugins/hal/HalHome.java +++ b/src/main/java/com/bwssystems/HABridge/plugins/hal/HalHome.java @@ -13,15 +13,20 @@ import com.bwssystems.HABridge.BridgeSettingsDescriptor; import com.bwssystems.HABridge.Home; import com.bwssystems.HABridge.NamedIP; import com.bwssystems.HABridge.api.CallItem; -import com.bwssystems.HABridge.api.hue.DeviceState; -import com.bwssystems.HABridge.api.hue.StateChangeBody; +import com.bwssystems.HABridge.api.NameValue; +import com.bwssystems.HABridge.api.hue.HueError; +import com.bwssystems.HABridge.api.hue.HueErrorResponse; import com.bwssystems.HABridge.dao.DeviceDescriptor; +import com.bwssystems.HABridge.hue.BrightnessDecode; import com.bwssystems.HABridge.hue.MultiCommandUtil; +import com.bwssystems.HABridge.plugins.http.HTTPHandler; +import com.google.gson.Gson; public class HalHome implements Home { private static final Logger log = LoggerFactory.getLogger(HalHome.class); private Map hals; private Boolean validHal; + private HTTPHandler anHttpHandler; public HalHome(BridgeSettingsDescriptor bridgeSettings) { super(); @@ -108,14 +113,30 @@ public class HalHome implements Home { aNewHalDevice.setHalname(theKey); theDeviceList.add(aNewHalDevice); } + anHttpHandler = new HTTPHandler(); return true; } @Override - public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int iterationCount, - DeviceState state, StateChangeBody theStateChanges, boolean stateHasBri, boolean stateHasBriInc, DeviceDescriptor device, String body) { - log.info("device handler not implemented"); - return null; + public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int intensity, + Integer targetBri,Integer targetBriInc, DeviceDescriptor device, String body) { + log.debug("executing HUE api request to HAL Http " + anItem.getItem().getAsString()); + String responseString = null; + + String anUrl = BrightnessDecode.calculateReplaceIntensityValue(anItem.getItem().getAsString(), + intensity, targetBri, targetBriInc, false); + String aBody; + aBody = BrightnessDecode.calculateReplaceIntensityValue(anItem.getHttpBody(), + intensity, targetBri, targetBriInc, false); + // make call + if (anHttpHandler.doHttpRequest(anUrl, anItem.getHttpVerb(), anItem.getContentType(), aBody, + new Gson().fromJson(anItem.getHttpHeaders(), NameValue[].class)) == null) { + log.warn("Error on calling url to change device state: " + anUrl); + responseString = new Gson().toJson(HueErrorResponse.createResponse("6", "/lights/" + lightId, + "Error on calling url to change device state", "/lights/" + + lightId + "state", null, null).getTheErrors(), HueError[].class); + } + return responseString; } @Override diff --git a/src/main/java/com/bwssystems/HABridge/plugins/harmony/HarmonyHome.java b/src/main/java/com/bwssystems/HABridge/plugins/harmony/HarmonyHome.java index 366f9ec..6ff61e2 100644 --- a/src/main/java/com/bwssystems/HABridge/plugins/harmony/HarmonyHome.java +++ b/src/main/java/com/bwssystems/HABridge/plugins/harmony/HarmonyHome.java @@ -16,8 +16,6 @@ import com.bwssystems.HABridge.Home; import com.bwssystems.HABridge.IpList; import com.bwssystems.HABridge.NamedIP; import com.bwssystems.HABridge.api.CallItem; -import com.bwssystems.HABridge.api.hue.DeviceState; -import com.bwssystems.HABridge.api.hue.StateChangeBody; import com.bwssystems.HABridge.dao.DeviceDescriptor; import com.bwssystems.HABridge.hue.MultiCommandUtil; import com.google.gson.Gson; @@ -125,8 +123,8 @@ public class HarmonyHome implements Home { } @Override - public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int iterationCount, - DeviceState state, StateChangeBody theStateChanges, boolean stateHasBri, boolean stateHasBriInc, DeviceDescriptor device, String body) { + public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int intensity, + Integer targetBri,Integer targetBriInc, DeviceDescriptor device, String body) { String responseString = null; log.debug("executing HUE api request to change " + anItem.getType() + " to Harmony: " + device.getName()); if(!validHarmony) { @@ -147,20 +145,7 @@ public class HarmonyHome implements Home { + "\",\"description\": \"Should not get here, no harmony hub available\", \"parameter\": \"/lights/" + lightId + "state\"}}]"; } else { - for (int x = 0; x < aMultiUtil.getSetCount(); x++) { - if (x > 0 || iterationCount > 0) { - try { - Thread.sleep(aMultiUtil.getTheDelay()); - } catch (InterruptedException e) { - // ignore - } - } - if (anItem.getDelay() != null && anItem.getDelay() > 0) - aMultiUtil.setTheDelay(anItem.getDelay()); - else - aMultiUtil.setTheDelay(aMultiUtil.getDelayDefault()); - myHarmony.startActivity(anActivity); - } + myHarmony.startActivity(anActivity); } } else if(anItem.getType().trim().equalsIgnoreCase(DeviceMapTypes.HARMONY_BUTTON[DeviceMapTypes.typeIndex])) { String url = anItem.getItem().toString(); @@ -172,8 +157,6 @@ public class HarmonyHome implements Home { for(int z = 0; z < deviceButtons.length; z++) { if(deviceButtons[z].getCount() != null && deviceButtons[z].getCount() > 0) theCount = deviceButtons[z].getCount(); - else - theCount = aMultiUtil.getSetCount(); for(int y = 0; y < theCount; y++) { if( y > 0 || z > 0) { try { diff --git a/src/main/java/com/bwssystems/HABridge/plugins/hass/HassHome.java b/src/main/java/com/bwssystems/HABridge/plugins/hass/HassHome.java index 8f24df2..7c4871b 100644 --- a/src/main/java/com/bwssystems/HABridge/plugins/hass/HassHome.java +++ b/src/main/java/com/bwssystems/HABridge/plugins/hass/HassHome.java @@ -13,8 +13,6 @@ import com.bwssystems.HABridge.BridgeSettingsDescriptor; import com.bwssystems.HABridge.Home; import com.bwssystems.HABridge.NamedIP; import com.bwssystems.HABridge.api.CallItem; -import com.bwssystems.HABridge.api.hue.DeviceState; -import com.bwssystems.HABridge.api.hue.StateChangeBody; import com.bwssystems.HABridge.dao.DeviceDescriptor; import com.bwssystems.HABridge.hue.BrightnessDecode; import com.bwssystems.HABridge.hue.MultiCommandUtil; @@ -111,8 +109,8 @@ public class HassHome implements Home { } @Override - public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int iterationCount, DeviceState state, - StateChangeBody theStateChanges, boolean stateHasBri, boolean stateHasBriInc, DeviceDescriptor device, String body) { + public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int intensity, + Integer targetBri,Integer targetBriInc, DeviceDescriptor device, String body) { String theReturn = null; log.debug("executing HUE api request to send message to HomeAssistant: " + anItem.getItem().toString()); if(!validHass) { @@ -124,7 +122,7 @@ public class HassHome implements Home { } else { HassCommand hassCommand = aGsonHandler.fromJson(anItem.getItem(), HassCommand.class); hassCommand.setBri(BrightnessDecode.replaceIntensityValue(hassCommand.getBri(), - BrightnessDecode.calculateIntensity(state, theStateChanges, stateHasBri, stateHasBriInc), false)); + BrightnessDecode.calculateIntensity(intensity, targetBri, targetBriInc), false)); HomeAssistant homeAssistant = getHomeAssistant(hassCommand.getHassName()); if (homeAssistant == null) { log.warn("Should not get here, no HomeAssistants available"); @@ -132,23 +130,9 @@ public class HassHome implements Home { + "\",\"description\": \"Should not get here, no HiomeAssistant clients available\", \"parameter\": \"/lights/" + lightId + "state\"}}]"; } else { - for (int x = 0; x < aMultiUtil.getSetCount(); x++) { - if (x > 0 || iterationCount > 0) { - try { - Thread.sleep(aMultiUtil.getTheDelay()); - } catch (InterruptedException e) { - // ignore - } - } - if (anItem.getDelay() != null && anItem.getDelay() > 0) - aMultiUtil.setTheDelay(anItem.getDelay()); - else - aMultiUtil.setTheDelay(aMultiUtil.getDelayDefault()); log.debug("calling HomeAssistant: " + hassCommand.getHassName() + " - " - + hassCommand.getEntityId() + " - " + hassCommand.getState() + " - " + hassCommand.getBri() - + " - iteration: " + String.valueOf(iterationCount) + " - count: " + String.valueOf(x)); + + hassCommand.getEntityId() + " - " + hassCommand.getState() + " - " + hassCommand.getBri()); homeAssistant.callCommand(hassCommand); - } } } return theReturn; diff --git a/src/main/java/com/bwssystems/HABridge/plugins/http/HTTPHome.java b/src/main/java/com/bwssystems/HABridge/plugins/http/HTTPHome.java index abde510..ff561bb 100644 --- a/src/main/java/com/bwssystems/HABridge/plugins/http/HTTPHome.java +++ b/src/main/java/com/bwssystems/HABridge/plugins/http/HTTPHome.java @@ -7,10 +7,8 @@ import com.bwssystems.HABridge.BridgeSettingsDescriptor; import com.bwssystems.HABridge.Home; import com.bwssystems.HABridge.api.CallItem; import com.bwssystems.HABridge.api.NameValue; -import com.bwssystems.HABridge.api.hue.DeviceState; import com.bwssystems.HABridge.api.hue.HueError; import com.bwssystems.HABridge.api.hue.HueErrorResponse; -import com.bwssystems.HABridge.api.hue.StateChangeBody; import com.bwssystems.HABridge.dao.DeviceDescriptor; import com.bwssystems.HABridge.hue.BrightnessDecode; import com.bwssystems.HABridge.hue.MultiCommandUtil; @@ -26,8 +24,8 @@ public class HTTPHome implements Home { } @Override - public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int iterationCount, - DeviceState state, StateChangeBody theStateChanges, boolean stateHasBri, boolean stateHasBriInc, DeviceDescriptor device, String body) { + public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int intensity, + Integer targetBri,Integer targetBriInc, DeviceDescriptor device, String body) { String responseString = null; //Backwards Compatibility Items @@ -46,24 +44,11 @@ public class HTTPHome implements Home { + (anItem.getHttpVerb() == null ? "GET" : anItem.getHttpVerb()) + ": " + anItem.getItem().getAsString()); - for (int x = 0; x < aMultiUtil.getSetCount(); x++) { - if (x > 0 || iterationCount > 0) { - try { - Thread.sleep(aMultiUtil.getTheDelay()); - } catch (InterruptedException e) { - // ignore - } - } - if (anItem.getDelay() != null && anItem.getDelay() > 0) - aMultiUtil.setTheDelay(anItem.getDelay()); - else - aMultiUtil.setTheDelay(aMultiUtil.getDelayDefault()); String anUrl = BrightnessDecode.calculateReplaceIntensityValue(anItem.getItem().getAsString(), - state, theStateChanges, stateHasBri, stateHasBriInc, false); + intensity, targetBri, targetBriInc, false); String aBody; aBody = BrightnessDecode.calculateReplaceIntensityValue(anItem.getHttpBody(), - state, theStateChanges, stateHasBri, stateHasBriInc, - false); + intensity, targetBri, targetBriInc, false); // make call if (anHttpHandler.doHttpRequest(anUrl, anItem.getHttpVerb(), anItem.getContentType(), aBody, new Gson().fromJson(anItem.getHttpHeaders(), NameValue[].class)) == null) { @@ -71,9 +56,7 @@ public class HTTPHome implements Home { responseString = new Gson().toJson(HueErrorResponse.createResponse("6", "/lights/" + lightId, "Error on calling url to change device state", "/lights/" + lightId + "state", null, null).getTheErrors(), HueError[].class); - x = aMultiUtil.getSetCount(); } - } return responseString; } diff --git a/src/main/java/com/bwssystems/HABridge/plugins/hue/HueHome.java b/src/main/java/com/bwssystems/HABridge/plugins/hue/HueHome.java index 8ce9721..3bfcf5c 100644 --- a/src/main/java/com/bwssystems/HABridge/plugins/hue/HueHome.java +++ b/src/main/java/com/bwssystems/HABridge/plugins/hue/HueHome.java @@ -13,9 +13,7 @@ import com.bwssystems.HABridge.Home; import com.bwssystems.HABridge.NamedIP; import com.bwssystems.HABridge.api.CallItem; import com.bwssystems.HABridge.api.hue.DeviceResponse; -import com.bwssystems.HABridge.api.hue.DeviceState; import com.bwssystems.HABridge.api.hue.HueApiResponse; -import com.bwssystems.HABridge.api.hue.StateChangeBody; import com.bwssystems.HABridge.dao.DeviceDescriptor; import com.bwssystems.HABridge.hue.MultiCommandUtil; import com.google.gson.Gson; @@ -77,8 +75,8 @@ public class HueHome implements Home { } @Override - public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int iterationCount, - DeviceState state, StateChangeBody theStateChanges, boolean stateHasBri, boolean stateHasBriInc, DeviceDescriptor device, String body) { + public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int intensity, + Integer targetBri,Integer targetBriInc, DeviceDescriptor device, String body) { if(!validHue) return null; String responseString = null; @@ -89,23 +87,8 @@ public class HueHome implements Home { HueInfo theHue = hues.get(deviceId.getHueName()); // make call - for (int x = 0; x < aMultiUtil.getSetCount(); x++) { - if (x > 0 || iterationCount > 0) { - try { - Thread.sleep(aMultiUtil.getTheDelay()); - } catch (InterruptedException e) { - // ignore - } - } - if (anItem.getDelay() != null && anItem.getDelay() > 0) - aMultiUtil.setTheDelay(anItem.getDelay()); - else - aMultiUtil.setTheDelay(aMultiUtil.getDelayDefault()); - - responseString = theHue.changeState(deviceId, lightId, body); - if (responseString.contains("[{\"error\":")) - x = aMultiUtil.getSetCount(); - } + responseString = theHue.changeState(deviceId, lightId, body); + return responseString; } diff --git a/src/main/java/com/bwssystems/HABridge/plugins/mqtt/MQTTHome.java b/src/main/java/com/bwssystems/HABridge/plugins/mqtt/MQTTHome.java index 7a6b335..c544762 100644 --- a/src/main/java/com/bwssystems/HABridge/plugins/mqtt/MQTTHome.java +++ b/src/main/java/com/bwssystems/HABridge/plugins/mqtt/MQTTHome.java @@ -12,8 +12,6 @@ import com.bwssystems.HABridge.BridgeSettingsDescriptor; import com.bwssystems.HABridge.Home; import com.bwssystems.HABridge.NamedIP; import com.bwssystems.HABridge.api.CallItem; -import com.bwssystems.HABridge.api.hue.DeviceState; -import com.bwssystems.HABridge.api.hue.StateChangeBody; import com.bwssystems.HABridge.dao.DeviceDescriptor; import com.bwssystems.HABridge.hue.BrightnessDecode; import com.bwssystems.HABridge.hue.MultiCommandUtil; @@ -76,24 +74,25 @@ public class MQTTHome implements Home { } @Override - public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int iterationCount, - DeviceState state, StateChangeBody theStateChanges, boolean stateHasBri, boolean stateHasBriInc, DeviceDescriptor device, String body) { + public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int intensity, + Integer targetBri,Integer targetBriInc, DeviceDescriptor device, String body) { String responseString = null; log.debug("executing HUE api request to send message to MQTT broker: " + anItem.getItem().toString()); if (validMqtt) { - MQTTMessage[] mqttMessages = aGsonHandler.fromJson(BrightnessDecode.replaceIntensityValue(anItem.getItem().toString(), - BrightnessDecode.calculateIntensity(state, theStateChanges, stateHasBri, stateHasBriInc), false), MQTTMessage[].class); + String mqttObject = BrightnessDecode.calculateReplaceIntensityValue(anItem.getItem().toString(), + intensity, targetBri, targetBriInc, false); + if (mqttObject.substring(0, 1).equalsIgnoreCase("{")) + mqttObject = "[" + mqttObject + "]"; + MQTTMessage[] mqttMessages = aGsonHandler.fromJson(mqttObject, MQTTMessage[].class); Integer theCount = 1; for(int z = 0; z < mqttMessages.length; z++) { if(mqttMessages[z].getCount() != null && mqttMessages[z].getCount() > 0) theCount = mqttMessages[z].getCount(); - else - theCount = aMultiUtil.getSetCount(); for(int y = 0; y < theCount; y++) { if( y > 0 || z > 0) { log.debug("publishing message: " + mqttMessages[y].getClientId() + " - " + mqttMessages[y].getTopic() + " - " + mqttMessages[y].getMessage() - + " - iteration: " + String.valueOf(iterationCount) + " - count: " + String.valueOf(z)); + + " - count: " + String.valueOf(z)); MQTTHandler mqttHandler = getMQTTHandler(mqttMessages[y].getClientId()); if (mqttHandler == null) { diff --git a/src/main/java/com/bwssystems/HABridge/plugins/tcp/TCPHome.java b/src/main/java/com/bwssystems/HABridge/plugins/tcp/TCPHome.java index b6096cf..6d8673a 100644 --- a/src/main/java/com/bwssystems/HABridge/plugins/tcp/TCPHome.java +++ b/src/main/java/com/bwssystems/HABridge/plugins/tcp/TCPHome.java @@ -13,8 +13,6 @@ import org.slf4j.LoggerFactory; import com.bwssystems.HABridge.BridgeSettingsDescriptor; import com.bwssystems.HABridge.Home; import com.bwssystems.HABridge.api.CallItem; -import com.bwssystems.HABridge.api.hue.DeviceState; -import com.bwssystems.HABridge.api.hue.StateChangeBody; import com.bwssystems.HABridge.dao.DeviceDescriptor; import com.bwssystems.HABridge.hue.BrightnessDecode; import com.bwssystems.HABridge.hue.MultiCommandUtil; @@ -30,58 +28,42 @@ public class TCPHome implements Home { } @Override - public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int iterationCount, - DeviceState state, StateChangeBody theStateChanges, boolean stateHasBri, boolean stateHasBriInc, - DeviceDescriptor device, String body) { - log.debug("executing HUE api request to TCP: " + anItem.getItem().toString()); - for (int x = 0; x < aMultiUtil.getSetCount(); x++) { - if (x > 0 || iterationCount > 0) { - try { - Thread.sleep(aMultiUtil.getTheDelay()); - } catch (InterruptedException e) { - // ignore - } - } - if (anItem.getDelay() != null && anItem.getDelay() > 0) - aMultiUtil.setTheDelay(anItem.getDelay()); - else - aMultiUtil.setTheDelay(aMultiUtil.getDelayDefault()); - String intermediate = anItem.getItem().toString().substring(anItem.getItem().toString().indexOf("://") + 3); - String hostPortion = intermediate.substring(0, intermediate.indexOf('/')); - String theUrlBody = intermediate.substring(intermediate.indexOf('/') + 1); - String hostAddr = null; - String port = null; - InetAddress IPAddress = null; - if (hostPortion.contains(":")) { - hostAddr = hostPortion.substring(0, intermediate.indexOf(':')); - port = hostPortion.substring(intermediate.indexOf(':') + 1); - } else - hostAddr = hostPortion; - try { - IPAddress = InetAddress.getByName(hostAddr); - } catch (UnknownHostException e) { - // noop - } + public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int intensity, + Integer targetBri,Integer targetBriInc, DeviceDescriptor device, String body) { + log.debug("executing HUE api request to TCP: " + anItem.getItem().getAsString()); + String intermediate = anItem.getItem().getAsString().substring(anItem.getItem().getAsString().indexOf("://") + 3); + String hostPortion = intermediate.substring(0, intermediate.indexOf('/')); + String theUrlBody = intermediate.substring(intermediate.indexOf('/') + 1); + String hostAddr = null; + String port = null; + InetAddress IPAddress = null; + if (hostPortion.contains(":")) { + hostAddr = hostPortion.substring(0, intermediate.indexOf(':')); + port = hostPortion.substring(intermediate.indexOf(':') + 1); + } else + hostAddr = hostPortion; + try { + IPAddress = InetAddress.getByName(hostAddr); + } catch (UnknownHostException e) { + // noop + } - if (theUrlBody.startsWith("0x")) { - theUrlBody = BrightnessDecode.calculateReplaceIntensityValue(theUrlBody, state, theStateChanges, - stateHasBri, stateHasBriInc, true); - sendData = DatatypeConverter.parseHexBinary(theUrlBody.substring(2)); - } else { - theUrlBody = BrightnessDecode.calculateReplaceIntensityValue(theUrlBody, state, theStateChanges, - stateHasBri, stateHasBriInc, false); - sendData = theUrlBody.getBytes(); - } + if (theUrlBody.startsWith("0x")) { + theUrlBody = BrightnessDecode.calculateReplaceIntensityValue(theUrlBody, intensity, targetBri, targetBriInc, true); + sendData = DatatypeConverter.parseHexBinary(theUrlBody.substring(2)); + } else { + theUrlBody = BrightnessDecode.calculateReplaceIntensityValue(theUrlBody, intensity, targetBri, targetBriInc, false); + sendData = theUrlBody.getBytes(); + } - try { - Socket dataSendSocket = new Socket(IPAddress, Integer.parseInt(port)); - DataOutputStream outToClient = new DataOutputStream(dataSendSocket.getOutputStream()); - outToClient.write(sendData); - outToClient.flush(); - dataSendSocket.close(); - } catch (Exception e) { - // noop - } + try { + Socket dataSendSocket = new Socket(IPAddress, Integer.parseInt(port)); + DataOutputStream outToClient = new DataOutputStream(dataSendSocket.getOutputStream()); + outToClient.write(sendData); + outToClient.flush(); + dataSendSocket.close(); + } catch (Exception e) { + // noop } return null; } diff --git a/src/main/java/com/bwssystems/HABridge/plugins/udp/UDPHome.java b/src/main/java/com/bwssystems/HABridge/plugins/udp/UDPHome.java index 01af52a..312811d 100644 --- a/src/main/java/com/bwssystems/HABridge/plugins/udp/UDPHome.java +++ b/src/main/java/com/bwssystems/HABridge/plugins/udp/UDPHome.java @@ -12,8 +12,6 @@ import org.slf4j.LoggerFactory; import com.bwssystems.HABridge.BridgeSettingsDescriptor; import com.bwssystems.HABridge.Home; import com.bwssystems.HABridge.api.CallItem; -import com.bwssystems.HABridge.api.hue.DeviceState; -import com.bwssystems.HABridge.api.hue.StateChangeBody; import com.bwssystems.HABridge.dao.DeviceDescriptor; import com.bwssystems.HABridge.hue.BrightnessDecode; import com.bwssystems.HABridge.hue.MultiCommandUtil; @@ -31,56 +29,43 @@ public class UDPHome implements Home { } @Override - public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int iterationCount, - DeviceState state, StateChangeBody theStateChanges, boolean stateHasBri, boolean stateHasBriInc, DeviceDescriptor device, String body) { - log.debug("executing HUE api request to UDP: " + anItem.getItem().toString()); - for (int x = 0; x < aMultiUtil.getSetCount(); x++) { - if (x > 0 || iterationCount > 0) { - try { - Thread.sleep(aMultiUtil.getTheDelay()); - } catch (InterruptedException e) { - // ignore - } - } - if (anItem.getDelay() != null && anItem.getDelay() > 0) - aMultiUtil.setTheDelay(anItem.getDelay()); - else - aMultiUtil.setTheDelay(aMultiUtil.getDelayDefault()); - String intermediate = anItem.getItem().getAsString() - .substring(anItem.getItem().getAsString().indexOf("://") + 3); - String hostPortion = intermediate.substring(0, intermediate.indexOf('/')); - String theUrlBody = intermediate.substring(intermediate.indexOf('/') + 1); - String hostAddr = null; - String port = null; - InetAddress IPAddress = null; - if (hostPortion.contains(":")) { - hostAddr = hostPortion.substring(0, intermediate.indexOf(':')); - port = hostPortion.substring(intermediate.indexOf(':') + 1); - } else - hostAddr = hostPortion; - try { - IPAddress = InetAddress.getByName(hostAddr); - } catch (UnknownHostException e) { - // noop - } + public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int intensity, + Integer targetBri,Integer targetBriInc, DeviceDescriptor device, String body) { + log.debug("executing HUE api request to UDP: " + anItem.getItem().getAsString()); + String intermediate = anItem.getItem().getAsString() + .substring(anItem.getItem().getAsString().indexOf("://") + 3); + String hostPortion = intermediate.substring(0, intermediate.indexOf('/')); + String theUrlBody = intermediate.substring(intermediate.indexOf('/') + 1); + String hostAddr = null; + String port = null; + InetAddress IPAddress = null; + if (hostPortion.contains(":")) { + hostAddr = hostPortion.substring(0, intermediate.indexOf(':')); + port = hostPortion.substring(intermediate.indexOf(':') + 1); + } else + hostAddr = hostPortion; + try { + IPAddress = InetAddress.getByName(hostAddr); + } catch (UnknownHostException e) { + // noop + } - if (theUrlBody.startsWith("0x")) { - theUrlBody = BrightnessDecode.calculateReplaceIntensityValue(theUrlBody, - state, theStateChanges, stateHasBri, stateHasBriInc, true); - sendData = DatatypeConverter.parseHexBinary(theUrlBody.substring(2)); - } else { - theUrlBody = BrightnessDecode.calculateReplaceIntensityValue(theUrlBody, - state, theStateChanges, stateHasBri, stateHasBriInc, false); - sendData = theUrlBody.getBytes(); - } - try { - theUDPDatagramSender.sendUDPResponse(sendData, IPAddress, - Integer.parseInt(port)); - } catch (NumberFormatException e) { - // noop - } catch (IOException e) { - // noop - } + if (theUrlBody.startsWith("0x")) { + theUrlBody = BrightnessDecode.calculateReplaceIntensityValue(theUrlBody, + intensity, targetBri, targetBriInc, true); + sendData = DatatypeConverter.parseHexBinary(theUrlBody.substring(2)); + } else { + theUrlBody = BrightnessDecode.calculateReplaceIntensityValue(theUrlBody, + intensity, targetBri, targetBriInc, false); + sendData = theUrlBody.getBytes(); + } + try { + theUDPDatagramSender.sendUDPResponse(sendData, IPAddress, + Integer.parseInt(port)); + } catch (NumberFormatException e) { + // noop + } catch (IOException e) { + // noop } return null; } diff --git a/src/main/java/com/bwssystems/HABridge/plugins/vera/VeraHome.java b/src/main/java/com/bwssystems/HABridge/plugins/vera/VeraHome.java index afc5b8b..23770d6 100644 --- a/src/main/java/com/bwssystems/HABridge/plugins/vera/VeraHome.java +++ b/src/main/java/com/bwssystems/HABridge/plugins/vera/VeraHome.java @@ -14,8 +14,6 @@ import com.bwssystems.HABridge.DeviceMapTypes; import com.bwssystems.HABridge.Home; import com.bwssystems.HABridge.NamedIP; import com.bwssystems.HABridge.api.CallItem; -import com.bwssystems.HABridge.api.hue.DeviceState; -import com.bwssystems.HABridge.api.hue.StateChangeBody; import com.bwssystems.HABridge.dao.DeviceDescriptor; import com.bwssystems.HABridge.hue.MultiCommandUtil; import com.bwssystems.HABridge.plugins.vera.luupRequests.Device; @@ -74,8 +72,8 @@ public class VeraHome implements Home { } @Override - public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int iterationCount, - DeviceState state, StateChangeBody theStateChanges, boolean stateHasBri, boolean stateHasBriInc, DeviceDescriptor device, String body) { + public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int intensity, + Integer targetBri,Integer targetBriInc, DeviceDescriptor device, String body) { // Not a device handler return null; } diff --git a/src/main/resources/public/scripts/app.js b/src/main/resources/public/scripts/app.js index ee39270..6b91875 100644 --- a/src/main/resources/public/scripts/app.js +++ b/src/main/resources/public/scripts/app.js @@ -843,15 +843,16 @@ app.controller('SystemController', function ($scope, $location, $http, $window, } } }; - $scope.addHasstoSettings = function (newhassname, newhassip, newhassport) { + $scope.addHasstoSettings = function (newhassname, newhassip, newhassport, newhasspassword) { if($scope.bridge.settings.hassaddress == null) { $scope.bridge.settings.hassaddress = { devices: [] }; } - var newhass = {name: newhassname, ip: newhassip, port: newhassport } + var newhass = {name: newhassname, ip: newhassip, port: newhassport, password: newhasspassword } $scope.bridge.settings.hassaddress.devices.push(newhass); $scope.newhassname = null; $scope.newhassip = null; $scope.newhassport = null; + $scope.newhasspassword = null; }; $scope.removeHasstoSettings = function (hassname, hassip) { for(var i = $scope.bridge.settings.hassaddress.devices.length - 1; i >= 0; i--) {