mirror of
https://github.com/bwssytems/ha-bridge.git
synced 2025-12-18 00:10:20 +00:00
Fixed hex handling of intensity. Added handling for \n, \t, \r and so
forth in tcp,udp and mqtt.
This commit is contained in:
7
pom.xml
7
pom.xml
@@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
<groupId>com.bwssystems.HABridge</groupId>
|
<groupId>com.bwssystems.HABridge</groupId>
|
||||||
<artifactId>ha-bridge</artifactId>
|
<artifactId>ha-bridge</artifactId>
|
||||||
<version>4.2.0c</version>
|
<version>4.2.0d</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<name>HA Bridge</name>
|
<name>HA Bridge</name>
|
||||||
@@ -126,6 +126,11 @@
|
|||||||
<artifactId>lifx-sdk-java</artifactId>
|
<artifactId>lifx-sdk-java</artifactId>
|
||||||
<version>2.1.6</version>
|
<version>2.1.6</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-lang3</artifactId>
|
||||||
|
<version>3.5</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ package com.bwssystems.HABridge.hue;
|
|||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.Conversion;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import net.java.dev.eval.Expression;
|
import net.java.dev.eval.Expression;
|
||||||
@@ -44,7 +46,7 @@ public class BrightnessDecode {
|
|||||||
}
|
}
|
||||||
if (request.contains(INTENSITY_BYTE)) {
|
if (request.contains(INTENSITY_BYTE)) {
|
||||||
if (isHex) {
|
if (isHex) {
|
||||||
String hexValue = Integer.toHexString(intensity);
|
String hexValue = convertToHex(intensity);
|
||||||
request = request.replace(INTENSITY_BYTE, hexValue);
|
request = request.replace(INTENSITY_BYTE, hexValue);
|
||||||
} else {
|
} else {
|
||||||
String intensityByte = String.valueOf(intensity);
|
String intensityByte = String.valueOf(intensity);
|
||||||
@@ -53,7 +55,7 @@ public class BrightnessDecode {
|
|||||||
} else if (request.contains(INTENSITY_PERCENT)) {
|
} else if (request.contains(INTENSITY_PERCENT)) {
|
||||||
int percentBrightness = (int) Math.round(intensity / 255.0 * 100);
|
int percentBrightness = (int) Math.round(intensity / 255.0 * 100);
|
||||||
if (isHex) {
|
if (isHex) {
|
||||||
String hexValue = Integer.toHexString(percentBrightness);
|
String hexValue = convertToHex(percentBrightness);
|
||||||
request = request.replace(INTENSITY_PERCENT, hexValue);
|
request = request.replace(INTENSITY_PERCENT, hexValue);
|
||||||
} else {
|
} else {
|
||||||
String intensityPercent = String.valueOf(percentBrightness);
|
String intensityPercent = String.valueOf(percentBrightness);
|
||||||
@@ -72,7 +74,7 @@ public class BrightnessDecode {
|
|||||||
BigDecimal result = exp.eval(variables);
|
BigDecimal result = exp.eval(variables);
|
||||||
Integer endResult = Math.round(result.floatValue());
|
Integer endResult = Math.round(result.floatValue());
|
||||||
if (isHex) {
|
if (isHex) {
|
||||||
String hexValue = Integer.toHexString(endResult);
|
String hexValue = convertToHex(endResult);
|
||||||
request = request.replace(INTENSITY_MATH + mathDescriptor + INTENSITY_MATH_CLOSE, hexValue);
|
request = request.replace(INTENSITY_MATH + mathDescriptor + INTENSITY_MATH_CLOSE, hexValue);
|
||||||
} else {
|
} else {
|
||||||
request = request.replace(INTENSITY_MATH + mathDescriptor + INTENSITY_MATH_CLOSE,
|
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) {
|
public static String calculateReplaceIntensityValue(String request, int theIntensity, Integer targetBri, Integer targetBriInc, boolean isHex) {
|
||||||
return replaceIntensityValue(request, calculateIntensity(theIntensity, targetBri, targetBriInc), 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.bwssystems.HABridge.plugins.mqtt;
|
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.MqttClient;
|
||||||
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
|
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
|
||||||
import org.eclipse.paho.client.mqttv3.MqttException;
|
import org.eclipse.paho.client.mqttv3.MqttException;
|
||||||
@@ -47,7 +48,7 @@ public class MQTTHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void publishMessage(String topic, String content) {
|
public void publishMessage(String topic, String content) {
|
||||||
MqttMessage message = new MqttMessage(content.getBytes());
|
MqttMessage message = new MqttMessage(StringEscapeUtils.unescapeJava(content).getBytes());
|
||||||
message.setQos(qos);
|
message.setQos(qos);
|
||||||
try {
|
try {
|
||||||
myClient.publish(topic, message);
|
myClient.publish(topic, message);
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import java.util.Map;
|
|||||||
|
|
||||||
import javax.xml.bind.DatatypeConverter;
|
import javax.xml.bind.DatatypeConverter;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.StringEscapeUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@@ -73,6 +74,7 @@ public class TCPHome implements Home {
|
|||||||
sendData = DatatypeConverter.parseHexBinary(theUrlBody.substring(2));
|
sendData = DatatypeConverter.parseHexBinary(theUrlBody.substring(2));
|
||||||
} else {
|
} else {
|
||||||
theUrlBody = BrightnessDecode.calculateReplaceIntensityValue(theUrlBody, intensity, targetBri, targetBriInc, false);
|
theUrlBody = BrightnessDecode.calculateReplaceIntensityValue(theUrlBody, intensity, targetBri, targetBriInc, false);
|
||||||
|
theUrlBody = StringEscapeUtils.unescapeJava(theUrlBody);
|
||||||
sendData = theUrlBody.getBytes();
|
sendData = theUrlBody.getBytes();
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import java.net.UnknownHostException;
|
|||||||
|
|
||||||
import javax.xml.bind.DatatypeConverter;
|
import javax.xml.bind.DatatypeConverter;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.StringEscapeUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@@ -59,6 +60,7 @@ public class UDPHome implements Home {
|
|||||||
sendData = DatatypeConverter.parseHexBinary(theUrlBody.substring(2));
|
sendData = DatatypeConverter.parseHexBinary(theUrlBody.substring(2));
|
||||||
} else {
|
} else {
|
||||||
theUrlBody = BrightnessDecode.calculateReplaceIntensityValue(theUrlBody, intensity, targetBri, targetBriInc, false);
|
theUrlBody = BrightnessDecode.calculateReplaceIntensityValue(theUrlBody, intensity, targetBri, targetBriInc, false);
|
||||||
|
theUrlBody = StringEscapeUtils.unescapeJava(theUrlBody);
|
||||||
sendData = theUrlBody.getBytes();
|
sendData = theUrlBody.getBytes();
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
|
|||||||
Reference in New Issue
Block a user