From f6cb41b8801ec1f22330e7823267f6d0e73f823a Mon Sep 17 00:00:00 2001 From: Admin Date: Tue, 7 Mar 2017 16:36:25 -0600 Subject: [PATCH] Fixed hex handling of intensity. Added handling for \n, \t, \r and so forth in tcp,udp and mqtt. --- pom.xml | 7 ++++++- .../HABridge/hue/BrightnessDecode.java | 21 +++++++++++++++---- .../HABridge/plugins/mqtt/MQTTHandler.java | 3 ++- .../HABridge/plugins/tcp/TCPHome.java | 2 ++ .../HABridge/plugins/udp/UDPHome.java | 2 ++ 5 files changed, 29 insertions(+), 6 deletions(-) diff --git a/pom.xml b/pom.xml index e6f5f92..e28f6ac 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ com.bwssystems.HABridge ha-bridge - 4.2.0c + 4.2.0d jar HA Bridge @@ -126,6 +126,11 @@ lifx-sdk-java 2.1.6 + + org.apache.commons + commons-lang3 + 3.5 + diff --git a/src/main/java/com/bwssystems/HABridge/hue/BrightnessDecode.java b/src/main/java/com/bwssystems/HABridge/hue/BrightnessDecode.java index 7c31f11..9c5b7c9 100644 --- a/src/main/java/com/bwssystems/HABridge/hue/BrightnessDecode.java +++ b/src/main/java/com/bwssystems/HABridge/hue/BrightnessDecode.java @@ -3,6 +3,8 @@ package com.bwssystems.HABridge.hue; import java.math.BigDecimal; import java.util.HashMap; import java.util.Map; + +import org.apache.commons.lang3.Conversion; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import net.java.dev.eval.Expression; @@ -44,7 +46,7 @@ public class BrightnessDecode { } if (request.contains(INTENSITY_BYTE)) { if (isHex) { - String hexValue = Integer.toHexString(intensity); + String hexValue = convertToHex(intensity); request = request.replace(INTENSITY_BYTE, hexValue); } else { String intensityByte = String.valueOf(intensity); @@ -53,7 +55,7 @@ public class BrightnessDecode { } else if (request.contains(INTENSITY_PERCENT)) { int percentBrightness = (int) Math.round(intensity / 255.0 * 100); if (isHex) { - String hexValue = Integer.toHexString(percentBrightness); + String hexValue = convertToHex(percentBrightness); request = request.replace(INTENSITY_PERCENT, hexValue); } else { String intensityPercent = String.valueOf(percentBrightness); @@ -72,7 +74,7 @@ public class BrightnessDecode { BigDecimal result = exp.eval(variables); Integer endResult = Math.round(result.floatValue()); if (isHex) { - String hexValue = Integer.toHexString(endResult); + String hexValue = convertToHex(endResult); request = request.replace(INTENSITY_MATH + mathDescriptor + INTENSITY_MATH_CLOSE, hexValue); } else { request = request.replace(INTENSITY_MATH + mathDescriptor + INTENSITY_MATH_CLOSE, @@ -89,4 +91,15 @@ public class BrightnessDecode { public static String calculateReplaceIntensityValue(String request, int theIntensity, Integer targetBri, Integer targetBriInc, boolean isHex) { return replaceIntensityValue(request, calculateIntensity(theIntensity, targetBri, targetBriInc), isHex); } -} + + // Apache Commons Conversion utils likes little endian too much + private static String convertToHex(int theValue) { + String destHex = "00"; + String hexValue = Conversion.intToHex(theValue, 0, destHex, 0, 2); + byte[] theBytes = hexValue.getBytes(); + byte[] newBytes = new byte[2]; + newBytes[0] = theBytes[1]; + newBytes[1] = theBytes[0]; + return new String(newBytes); + } +} \ No newline at end of file diff --git a/src/main/java/com/bwssystems/HABridge/plugins/mqtt/MQTTHandler.java b/src/main/java/com/bwssystems/HABridge/plugins/mqtt/MQTTHandler.java index d96ab68..e40001f 100644 --- a/src/main/java/com/bwssystems/HABridge/plugins/mqtt/MQTTHandler.java +++ b/src/main/java/com/bwssystems/HABridge/plugins/mqtt/MQTTHandler.java @@ -1,5 +1,6 @@ package com.bwssystems.HABridge.plugins.mqtt; +import org.apache.commons.lang3.StringEscapeUtils; import org.eclipse.paho.client.mqttv3.MqttClient; import org.eclipse.paho.client.mqttv3.MqttConnectOptions; import org.eclipse.paho.client.mqttv3.MqttException; @@ -47,7 +48,7 @@ public class MQTTHandler { } public void publishMessage(String topic, String content) { - MqttMessage message = new MqttMessage(content.getBytes()); + MqttMessage message = new MqttMessage(StringEscapeUtils.unescapeJava(content).getBytes()); message.setQos(qos); try { myClient.publish(topic, message); 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 aea477c..42e05f9 100644 --- a/src/main/java/com/bwssystems/HABridge/plugins/tcp/TCPHome.java +++ b/src/main/java/com/bwssystems/HABridge/plugins/tcp/TCPHome.java @@ -11,6 +11,7 @@ import java.util.Map; import javax.xml.bind.DatatypeConverter; +import org.apache.commons.lang3.StringEscapeUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -73,6 +74,7 @@ public class TCPHome implements Home { sendData = DatatypeConverter.parseHexBinary(theUrlBody.substring(2)); } else { theUrlBody = BrightnessDecode.calculateReplaceIntensityValue(theUrlBody, intensity, targetBri, targetBriInc, false); + theUrlBody = StringEscapeUtils.unescapeJava(theUrlBody); sendData = theUrlBody.getBytes(); } try { 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 b93a72e..1244bff 100644 --- a/src/main/java/com/bwssystems/HABridge/plugins/udp/UDPHome.java +++ b/src/main/java/com/bwssystems/HABridge/plugins/udp/UDPHome.java @@ -6,6 +6,7 @@ import java.net.UnknownHostException; import javax.xml.bind.DatatypeConverter; +import org.apache.commons.lang3.StringEscapeUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -59,6 +60,7 @@ public class UDPHome implements Home { sendData = DatatypeConverter.parseHexBinary(theUrlBody.substring(2)); } else { theUrlBody = BrightnessDecode.calculateReplaceIntensityValue(theUrlBody, intensity, targetBri, targetBriInc, false); + theUrlBody = StringEscapeUtils.unescapeJava(theUrlBody); sendData = theUrlBody.getBytes(); } try {