mirror of
https://github.com/bwssytems/ha-bridge.git
synced 2025-12-18 08:13:23 +00:00
remove JAXB dependency and update harmony disconnect detection
This commit is contained in:
8
pom.xml
8
pom.xml
@@ -5,16 +5,16 @@
|
|||||||
|
|
||||||
<groupId>com.bwssystems.HABridge</groupId>
|
<groupId>com.bwssystems.HABridge</groupId>
|
||||||
<artifactId>ha-bridge</artifactId>
|
<artifactId>ha-bridge</artifactId>
|
||||||
<version>5.2.2RC3</version>
|
<version>5.2.2RC4</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<name>HA Bridge</name>
|
<name>HA Bridge</name>
|
||||||
<description>Emulates a Philips Hue bridge to allow the Amazon Echo to hook up to other HA systems, i.e. Vera or Harmony Hub or Nest, using lightweight frameworks</description>
|
<description>Emulates a Philips Hue bridge to allow the Amazon Echo to hook up to other HA systems, i.e. Vera or Harmony Hub or Nest, using lightweight frameworks</description>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<java.version>1.8</java.version>
|
<java.version>1.11</java.version>
|
||||||
<maven.compiler.source>1.8</maven.compiler.source>
|
<maven.compiler.source>1.11</maven.compiler.source>
|
||||||
<maven.compiler.target>1.8</maven.compiler.target>
|
<maven.compiler.target>1.11</maven.compiler.target>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package com.bwssystems.HABridge.dao;
|
|||||||
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.math.BigInteger;
|
|
||||||
import java.nio.file.FileSystems;
|
import java.nio.file.FileSystems;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
@@ -13,8 +12,6 @@ import java.util.HashMap;
|
|||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import javax.xml.bind.DatatypeConverter;
|
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@@ -25,6 +22,7 @@ import com.bwssystems.HABridge.dao.DeviceDescriptor;
|
|||||||
import com.bwssystems.HABridge.plugins.hue.HueHome;
|
import com.bwssystems.HABridge.plugins.hue.HueHome;
|
||||||
import com.bwssystems.HABridge.util.BackupHandler;
|
import com.bwssystems.HABridge.util.BackupHandler;
|
||||||
import com.bwssystems.HABridge.util.JsonTransformer;
|
import com.bwssystems.HABridge.util.JsonTransformer;
|
||||||
|
import com.bwssystems.HABridge.util.HexLibrary;
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.GsonBuilder;
|
import com.google.gson.GsonBuilder;
|
||||||
import com.google.gson.JsonSyntaxException;
|
import com.google.gson.JsonSyntaxException;
|
||||||
@@ -180,9 +178,7 @@ public class DeviceRepository extends BackupHandler {
|
|||||||
descriptors[i].setId(String.valueOf(nextId));
|
descriptors[i].setId(String.valueOf(nextId));
|
||||||
}
|
}
|
||||||
if(descriptors[i].getUniqueid() == null || descriptors[i].getUniqueid().length() == 0) {
|
if(descriptors[i].getUniqueid() == null || descriptors[i].getUniqueid().length() == 0) {
|
||||||
BigInteger bigInt = BigInteger.valueOf(Integer.decode(descriptors[i].getId()));
|
String hexValue = HexLibrary.encodeUsingBigIntegerToString(descriptors[i].getId());
|
||||||
byte[] theBytes = bigInt.toByteArray();
|
|
||||||
String hexValue = DatatypeConverter.printHexBinary(theBytes);
|
|
||||||
|
|
||||||
descriptors[i].setUniqueid("00:17:88:5E:D3:" + hexValue + "-" + hexValue);
|
descriptors[i].setUniqueid("00:17:88:5E:D3:" + hexValue + "-" + hexValue);
|
||||||
}
|
}
|
||||||
@@ -204,9 +200,7 @@ public class DeviceRepository extends BackupHandler {
|
|||||||
nextId++;
|
nextId++;
|
||||||
DeviceDescriptor theDevice = deviceIterator.next();
|
DeviceDescriptor theDevice = deviceIterator.next();
|
||||||
theDevice.setId(String.valueOf(nextId));
|
theDevice.setId(String.valueOf(nextId));
|
||||||
BigInteger bigInt = BigInteger.valueOf(nextId);
|
String hexValue = HexLibrary.encodeUsingBigIntegerToString(nextId.toString());
|
||||||
byte[] theBytes = bigInt.toByteArray();
|
|
||||||
String hexValue = DatatypeConverter.printHexBinary(theBytes);
|
|
||||||
|
|
||||||
theDevice.setUniqueid("00:17:88:5E:D3:" + hexValue + "-" + hexValue);
|
theDevice.setUniqueid("00:17:88:5E:D3:" + hexValue + "-" + hexValue);
|
||||||
newdevices.put(theDevice.getId(), theDevice);
|
newdevices.put(theDevice.getId(), theDevice);
|
||||||
|
|||||||
@@ -11,8 +11,6 @@ import java.util.Iterator;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import javax.xml.bind.DatatypeConverter;
|
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@@ -28,6 +26,7 @@ import com.bwssystems.HABridge.hue.ColorDecode;
|
|||||||
import com.bwssystems.HABridge.hue.DeviceDataDecode;
|
import com.bwssystems.HABridge.hue.DeviceDataDecode;
|
||||||
import com.bwssystems.HABridge.hue.MultiCommandUtil;
|
import com.bwssystems.HABridge.hue.MultiCommandUtil;
|
||||||
import com.bwssystems.HABridge.hue.TimeDecode;
|
import com.bwssystems.HABridge.hue.TimeDecode;
|
||||||
|
import com.bwssystems.HABridge.util.HexLibrary;
|
||||||
import com.github.mob41.blapi.BLDevice;
|
import com.github.mob41.blapi.BLDevice;
|
||||||
import com.github.mob41.blapi.MP1Device;
|
import com.github.mob41.blapi.MP1Device;
|
||||||
import com.github.mob41.blapi.SP1Device;
|
import com.github.mob41.blapi.SP1Device;
|
||||||
@@ -118,7 +117,7 @@ public class BroadlinkHome implements Home {
|
|||||||
|
|
||||||
if (theDevice == null) {
|
if (theDevice == null) {
|
||||||
if(broadlinkCommand.hasIpAndMac()) {
|
if(broadlinkCommand.hasIpAndMac()) {
|
||||||
byte[] intBytes = DatatypeConverter.parseHexBinary(broadlinkCommand.getType());
|
byte[] intBytes = HexLibrary.decodeHexString(broadlinkCommand.getType());
|
||||||
BigInteger theBig = new BigInteger(intBytes);
|
BigInteger theBig = new BigInteger(intBytes);
|
||||||
int theType = theBig.intValue();
|
int theType = theBig.intValue();
|
||||||
try {
|
try {
|
||||||
@@ -218,13 +217,13 @@ public class BroadlinkHome implements Home {
|
|||||||
}
|
}
|
||||||
theStringData = DeviceDataDecode.replaceDeviceData(theStringData, device);
|
theStringData = DeviceDataDecode.replaceDeviceData(theStringData, device);
|
||||||
theStringData = TimeDecode.replaceTimeValue(theStringData);
|
theStringData = TimeDecode.replaceTimeValue(theStringData);
|
||||||
byte[] theData = DatatypeConverter.parseHexBinary(theStringData);
|
byte[] theData = HexLibrary.decodeHexString(theStringData);
|
||||||
SendDataCmdPayload thePayload = new SendDataCmdPayload(theData);
|
SendDataCmdPayload thePayload = new SendDataCmdPayload(theData);
|
||||||
|
|
||||||
DatagramPacket thePacket = theDevice.sendCmdPkt(Configuration.BROADLINK_DISCONVER_TIMEOUT, thePayload);
|
DatagramPacket thePacket = theDevice.sendCmdPkt(Configuration.BROADLINK_DISCONVER_TIMEOUT, thePayload);
|
||||||
String returnData = null;
|
String returnData = null;
|
||||||
if(thePacket != null)
|
if(thePacket != null)
|
||||||
returnData = DatatypeConverter.printHexBinary(thePacket.getData());
|
returnData = HexLibrary.encodeHexString(thePacket.getData());
|
||||||
else
|
else
|
||||||
returnData = "No Data - null";
|
returnData = "No Data - null";
|
||||||
log.debug("RM2 Device data return: <<<" + returnData + ">>>");
|
log.debug("RM2 Device data return: <<<" + returnData + ">>>");
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import java.io.IOException;
|
|||||||
import java.net.DatagramPacket;
|
import java.net.DatagramPacket;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
|
|
||||||
import javax.xml.bind.DatatypeConverter;
|
import com.bwssystems.HABridge.util.HexLibrary;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
@@ -33,7 +33,7 @@ public class TestBLDevice extends BLDevice {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public DatagramPacket sendCmdPkt(int timeout, CmdPayload aCmd) {
|
public DatagramPacket sendCmdPkt(int timeout, CmdPayload aCmd) {
|
||||||
log.info("sendCmdPkt called with " + DatatypeConverter.printHexBinary(aCmd.getPayload().getData()));
|
log.info("sendCmdPkt called with " + HexLibrary.encodeHexString(aCmd.getPayload().getData()));
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ package com.bwssystems.HABridge.plugins.broadlink;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.DatagramPacket;
|
import java.net.DatagramPacket;
|
||||||
import javax.xml.bind.DatatypeConverter;
|
import com.bwssystems.HABridge.util.HexLibrary;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
@@ -23,7 +23,7 @@ public class TestMP1Device extends MP1Device {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public DatagramPacket sendCmdPkt(int timeout, CmdPayload aCmd) {
|
public DatagramPacket sendCmdPkt(int timeout, CmdPayload aCmd) {
|
||||||
log.info("sendCmdPkt called with " + DatatypeConverter.printHexBinary(aCmd.getPayload().getData()));
|
log.info("sendCmdPkt called with " + HexLibrary.encodeHexString(aCmd.getPayload().getData()));
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package com.bwssystems.HABridge.plugins.broadlink;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.DatagramPacket;
|
import java.net.DatagramPacket;
|
||||||
|
|
||||||
import javax.xml.bind.DatatypeConverter;
|
import com.bwssystems.HABridge.util.HexLibrary;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
@@ -21,7 +21,7 @@ public class TestRM2Device extends RM2Device {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public DatagramPacket sendCmdPkt(int timeout, CmdPayload aCmd) {
|
public DatagramPacket sendCmdPkt(int timeout, CmdPayload aCmd) {
|
||||||
log.info("sendCmdPkt called with " + DatatypeConverter.printHexBinary(((SendDataCmdPayload)aCmd).getData()));
|
log.info("sendCmdPkt called with " + HexLibrary.encodeHexString(((SendDataCmdPayload)aCmd).getData()));
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package com.bwssystems.HABridge.plugins.broadlink;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.DatagramPacket;
|
import java.net.DatagramPacket;
|
||||||
|
|
||||||
import javax.xml.bind.DatatypeConverter;
|
import com.bwssystems.HABridge.util.HexLibrary;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
@@ -24,7 +24,7 @@ public class TestSP1Device extends SP1Device {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public DatagramPacket sendCmdPkt(int timeout, CmdPayload aCmd) {
|
public DatagramPacket sendCmdPkt(int timeout, CmdPayload aCmd) {
|
||||||
log.info("sendCmdPkt called with " + DatatypeConverter.printHexBinary(aCmd.getPayload().getData()));
|
log.info("sendCmdPkt called with " + HexLibrary.encodeHexString(aCmd.getPayload().getData()));
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package com.bwssystems.HABridge.plugins.broadlink;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.DatagramPacket;
|
import java.net.DatagramPacket;
|
||||||
|
|
||||||
import javax.xml.bind.DatatypeConverter;
|
import com.bwssystems.HABridge.util.HexLibrary;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
@@ -24,7 +24,7 @@ public class TestSP2Device extends SP2Device {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public DatagramPacket sendCmdPkt(int timeout, CmdPayload aCmd) {
|
public DatagramPacket sendCmdPkt(int timeout, CmdPayload aCmd) {
|
||||||
log.info("sendCmdPkt called with " + DatatypeConverter.printHexBinary(aCmd.getPayload().getData()));
|
log.info("sendCmdPkt called with " + HexLibrary.encodeHexString(aCmd.getPayload().getData()));
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -163,7 +163,7 @@ public class HarmonyHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
boolean handleExceptionError(Exception e) {
|
boolean handleExceptionError(Exception e) {
|
||||||
if(e.getMessage().equalsIgnoreCase("Failed communicating with Harmony Hub")) {
|
if(e.getMessage().contains("Failed communicating with Harmony Hub") || e.getMessage().contains("Send heartbeat failed")) {
|
||||||
log.warn("Issue in communcicating with Harmony Hub, retrying connect....");
|
log.warn("Issue in communcicating with Harmony Hub, retrying connect....");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -93,7 +93,13 @@ public class HarmonyHome implements Home {
|
|||||||
resetHub(hubs.get(key).getMyHarmony());
|
resetHub(hubs.get(key).getMyHarmony());
|
||||||
activities = hubs.get(key).getMyHarmony().getActivities().iterator();
|
activities = hubs.get(key).getMyHarmony().getActivities().iterator();
|
||||||
if (activities == null) {
|
if (activities == null) {
|
||||||
log.error("Could not get communication restored with hub: " + key + ", please restart...");
|
if (resetHub(hubs.get(key).getMyHarmony())) {
|
||||||
|
activities = hubs.get(key).getMyHarmony().getActivities().iterator();
|
||||||
|
if (activities == null) {
|
||||||
|
log.error("Could not get communication restored with hub: " + key
|
||||||
|
+ ", please restart...");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (activities != null) {
|
if (activities != null) {
|
||||||
@@ -120,7 +126,13 @@ public class HarmonyHome implements Home {
|
|||||||
resetHub(hubs.get(key).getMyHarmony());
|
resetHub(hubs.get(key).getMyHarmony());
|
||||||
theActivity = hubs.get(key).getMyHarmony().getCurrentActivity();
|
theActivity = hubs.get(key).getMyHarmony().getCurrentActivity();
|
||||||
if (theActivity == null) {
|
if (theActivity == null) {
|
||||||
log.error("Could not get communication restored with hub: " + key + ", please restart...");
|
if (resetHub(hubs.get(key).getMyHarmony())) {
|
||||||
|
theActivity = hubs.get(key).getMyHarmony().getCurrentActivity();
|
||||||
|
if (theActivity == null) {
|
||||||
|
log.error("Could not get communication restored with hub: " + key
|
||||||
|
+ ", please restart...");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (theActivity != null) {
|
if (theActivity != null) {
|
||||||
@@ -145,7 +157,13 @@ public class HarmonyHome implements Home {
|
|||||||
resetHub(hubs.get(key).getMyHarmony());
|
resetHub(hubs.get(key).getMyHarmony());
|
||||||
devices = hubs.get(key).getMyHarmony().getDevices().iterator();
|
devices = hubs.get(key).getMyHarmony().getDevices().iterator();
|
||||||
if (devices == null) {
|
if (devices == null) {
|
||||||
log.error("Could not get communication restored with hub: " + key + ", please restart...");
|
if (resetHub(hubs.get(key).getMyHarmony())) {
|
||||||
|
devices = hubs.get(key).getMyHarmony().getDevices().iterator();
|
||||||
|
if (devices == null) {
|
||||||
|
log.error("Could not get communication restored with hub: " + key
|
||||||
|
+ ", please restart...");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (devices != null) {
|
if (devices != null) {
|
||||||
@@ -239,7 +257,7 @@ public class HarmonyHome implements Home {
|
|||||||
if (myHarmony == null)
|
if (myHarmony == null)
|
||||||
log.warn("Button Press - Should not get here, no harmony hub available");
|
log.warn("Button Press - Should not get here, no harmony hub available");
|
||||||
else{
|
else{
|
||||||
if (myHarmony.pressButton(deviceButtons[z])) {
|
if (!myHarmony.pressButton(deviceButtons[z])) {
|
||||||
if (resetHub(myHarmony)) {
|
if (resetHub(myHarmony)) {
|
||||||
myHarmony = getHarmonyHandler(deviceButtons[z].getHub());
|
myHarmony = getHarmonyHandler(deviceButtons[z].getHub());
|
||||||
if (!myHarmony.pressButton(deviceButtons[z])) {
|
if (!myHarmony.pressButton(deviceButtons[z])) {
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import java.util.HashMap;
|
|||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import javax.xml.bind.DatatypeConverter;
|
import com.bwssystems.HABridge.util.HexLibrary;
|
||||||
|
|
||||||
import org.apache.commons.lang3.StringEscapeUtils;
|
import org.apache.commons.lang3.StringEscapeUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
@@ -92,7 +92,7 @@ public class TCPHome implements Home {
|
|||||||
if (colorData != null) {
|
if (colorData != null) {
|
||||||
theUrlBody = ColorDecode.replaceColorData(theUrlBody, colorData, BrightnessDecode.calculateIntensity(intensity, targetBri, targetBriInc), true);
|
theUrlBody = ColorDecode.replaceColorData(theUrlBody, colorData, BrightnessDecode.calculateIntensity(intensity, targetBri, targetBriInc), true);
|
||||||
}
|
}
|
||||||
sendData = DatatypeConverter.parseHexBinary(theUrlBody.substring(2));
|
sendData = HexLibrary.decodeHexString(theUrlBody.substring(2));
|
||||||
} else {
|
} else {
|
||||||
theUrlBody = BrightnessDecode.calculateReplaceIntensityValue(theUrlBody, intensity, targetBri, targetBriInc, false);
|
theUrlBody = BrightnessDecode.calculateReplaceIntensityValue(theUrlBody, intensity, targetBri, targetBriInc, false);
|
||||||
if (colorData != null) {
|
if (colorData != null) {
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import java.io.IOException;
|
|||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
|
|
||||||
import javax.xml.bind.DatatypeConverter;
|
import com.bwssystems.HABridge.util.HexLibrary;
|
||||||
|
|
||||||
import org.apache.commons.lang3.StringEscapeUtils;
|
import org.apache.commons.lang3.StringEscapeUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
@@ -68,7 +68,7 @@ public class UDPHome implements Home {
|
|||||||
if (colorData != null) {
|
if (colorData != null) {
|
||||||
theUrlBody = ColorDecode.replaceColorData(theUrlBody, colorData, BrightnessDecode.calculateIntensity(intensity, targetBri, targetBriInc), true);
|
theUrlBody = ColorDecode.replaceColorData(theUrlBody, colorData, BrightnessDecode.calculateIntensity(intensity, targetBri, targetBriInc), true);
|
||||||
}
|
}
|
||||||
sendData = DatatypeConverter.parseHexBinary(theUrlBody.substring(2));
|
sendData = HexLibrary.decodeHexString(theUrlBody.substring(2));
|
||||||
} else {
|
} else {
|
||||||
theUrlBody = BrightnessDecode.calculateReplaceIntensityValue(theUrlBody, intensity, targetBri, targetBriInc, false);
|
theUrlBody = BrightnessDecode.calculateReplaceIntensityValue(theUrlBody, intensity, targetBri, targetBriInc, false);
|
||||||
|
|
||||||
|
|||||||
72
src/main/java/com/bwssystems/HABridge/util/HexLibrary.java
Normal file
72
src/main/java/com/bwssystems/HABridge/util/HexLibrary.java
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
package com.bwssystems.HABridge.util;
|
||||||
|
|
||||||
|
import java.math.BigInteger;
|
||||||
|
|
||||||
|
public class HexLibrary {
|
||||||
|
|
||||||
|
public static String byteToHex(byte num) {
|
||||||
|
char[] hexDigits = new char[2];
|
||||||
|
hexDigits[0] = Character.forDigit((num >> 4) & 0xF, 16);
|
||||||
|
hexDigits[1] = Character.forDigit((num & 0xF), 16);
|
||||||
|
return new String(hexDigits);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static byte hexToByte(String hexString) {
|
||||||
|
int firstDigit = toDigit(hexString.charAt(0));
|
||||||
|
int secondDigit = toDigit(hexString.charAt(1));
|
||||||
|
return (byte) ((firstDigit << 4) + secondDigit);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int toDigit(char hexChar) {
|
||||||
|
int digit = Character.digit(hexChar, 16);
|
||||||
|
if (digit == -1) {
|
||||||
|
throw new IllegalArgumentException("Invalid Hexadecimal Character: " + hexChar);
|
||||||
|
}
|
||||||
|
return digit;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String encodeHexString(byte[] byteArray) {
|
||||||
|
StringBuffer hexStringBuffer = new StringBuffer();
|
||||||
|
for (int i = 0; i < byteArray.length; i++) {
|
||||||
|
hexStringBuffer.append(byteToHex(byteArray[i]));
|
||||||
|
}
|
||||||
|
return hexStringBuffer.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static byte[] decodeHexString(String hexString) {
|
||||||
|
if (hexString.length() % 2 == 1) {
|
||||||
|
throw new IllegalArgumentException("Invalid hexadecimal String supplied.");
|
||||||
|
}
|
||||||
|
|
||||||
|
byte[] bytes = new byte[hexString.length() / 2];
|
||||||
|
for (int i = 0; i < hexString.length(); i += 2) {
|
||||||
|
bytes[i / 2] = hexToByte(hexString.substring(i, i + 2));
|
||||||
|
}
|
||||||
|
return bytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String encodeUsingBigIntegerStringFormat(byte[] bytes) {
|
||||||
|
BigInteger bigInteger = new BigInteger(1, bytes);
|
||||||
|
return String.format("%0" + (bytes.length << 1) + "x", bigInteger);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String encodeUsingBigIntegerToString(String intValue) {
|
||||||
|
BigInteger bigInteger = BigInteger.valueOf(Integer.decode(intValue));
|
||||||
|
return bigInteger.toString(16);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String encodeUsingBigIntegerToString(byte[] bytes) {
|
||||||
|
BigInteger bigInteger = new BigInteger(1, bytes);
|
||||||
|
return bigInteger.toString(16);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static byte[] decodeUsingBigInteger(String hexString) {
|
||||||
|
byte[] byteArray = new BigInteger(hexString, 16).toByteArray();
|
||||||
|
if (byteArray[0] == 0) {
|
||||||
|
byte[] output = new byte[byteArray.length - 1];
|
||||||
|
System.arraycopy(byteArray, 1, output, 0, output.length);
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
return byteArray;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
package com.bwssystems.broadlink.test;
|
package com.bwssystems.broadlink.test;
|
||||||
|
|
||||||
import javax.xml.bind.DatatypeConverter;
|
import com.bwssystems.HABridge.util.HexLibrary;
|
||||||
|
|
||||||
public class BroadlinkDataConstructor {
|
public class BroadlinkDataConstructor {
|
||||||
public final static byte[] testData1 = { 84, 104, 105, 115, 73, 115, 65, 84, 101, 115, 116, 83, 116, 114, 105, 110, 103, 49 };
|
public final static byte[] testData1 = { 84, 104, 105, 115, 73, 115, 65, 84, 101, 115, 116, 83, 116, 114, 105, 110, 103, 49 };
|
||||||
@@ -18,8 +18,8 @@ public class BroadlinkDataConstructor {
|
|||||||
System.out.println("----------------------------------");
|
System.out.println("----------------------------------");
|
||||||
System.out.println("This is the test hex string: <<<" + theHexString + ">>>");
|
System.out.println("This is the test hex string: <<<" + theHexString + ">>>");
|
||||||
try {
|
try {
|
||||||
theData = DatatypeConverter.parseHexBinary(theHexString);
|
theData = HexLibrary.decodeHexString(theHexString);
|
||||||
System.out.println("This is the test hex string from data bytes: <<<" + DatatypeConverter.printHexBinary(theData) + ">>>");
|
System.out.println("This is the test hex string from data bytes: <<<" + HexLibrary.encodeHexString(theData) + ">>>");
|
||||||
} catch(Exception e) {
|
} catch(Exception e) {
|
||||||
System.out.println("Error parsing he string: " + e.getMessage());
|
System.out.println("Error parsing he string: " + e.getMessage());
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
Reference in New Issue
Block a user