mirror of
https://github.com/bwssytems/ha-bridge.git
synced 2025-12-19 08:28:46 +00:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d05b6bea5c | ||
|
|
2f567cd604 | ||
|
|
ce97e928ad |
2
pom.xml
2
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>5.2.2RC2</version>
|
<version>5.2.2RC4</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<name>HA Bridge</name>
|
<name>HA Bridge</name>
|
||||||
|
|||||||
@@ -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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -43,6 +43,7 @@ public class HarmonyHandler {
|
|||||||
listOfActivities = harmonyClient.getConfig().getActivities();
|
listOfActivities = harmonyClient.getConfig().getActivities();
|
||||||
} catch (RuntimeException e) {
|
} catch (RuntimeException e) {
|
||||||
handleExceptionError(e);
|
handleExceptionError(e);
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return listOfActivities;
|
return listOfActivities;
|
||||||
@@ -59,6 +60,7 @@ public class HarmonyHandler {
|
|||||||
listOfDevices = harmonyClient.getConfig().getDevices();
|
listOfDevices = harmonyClient.getConfig().getDevices();
|
||||||
} catch (RuntimeException e) {
|
} catch (RuntimeException e) {
|
||||||
handleExceptionError(e);
|
handleExceptionError(e);
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
return listOfDevices;
|
return listOfDevices;
|
||||||
}
|
}
|
||||||
@@ -73,6 +75,7 @@ public class HarmonyHandler {
|
|||||||
aConfig = harmonyClient.getConfig();
|
aConfig = harmonyClient.getConfig();
|
||||||
} catch (RuntimeException e) {
|
} catch (RuntimeException e) {
|
||||||
handleExceptionError(e);
|
handleExceptionError(e);
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
return aConfig;
|
return aConfig;
|
||||||
}
|
}
|
||||||
@@ -87,6 +90,7 @@ public class HarmonyHandler {
|
|||||||
anActivity = harmonyClient.getCurrentActivity();
|
anActivity = harmonyClient.getCurrentActivity();
|
||||||
} catch (RuntimeException e) {
|
} catch (RuntimeException e) {
|
||||||
handleExceptionError(e);
|
handleExceptionError(e);
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
return anActivity;
|
return anActivity;
|
||||||
}
|
}
|
||||||
@@ -112,16 +116,14 @@ public class HarmonyHandler {
|
|||||||
harmonyClient.startActivityByName(anActivity.getName());
|
harmonyClient.startActivityByName(anActivity.getName());
|
||||||
} catch (IllegalArgumentException ei) {
|
} catch (IllegalArgumentException ei) {
|
||||||
log.error("Error in finding activity: " + anActivity.getName() + " for a hub: " + anActivity.getHub());
|
log.error("Error in finding activity: " + anActivity.getName() + " for a hub: " + anActivity.getHub());
|
||||||
return false;
|
|
||||||
} catch (RuntimeException e1) {
|
} catch (RuntimeException e1) {
|
||||||
handleExceptionError(e1);
|
return handleExceptionError(e1);
|
||||||
}
|
}
|
||||||
} catch (RuntimeException e1) {
|
} catch (RuntimeException e1) {
|
||||||
handleExceptionError(e1);
|
return handleExceptionError(e1);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
log.error("Error in finding activity: " + anActivity.getName() + " for a hub: " + anActivity.getHub());
|
log.error("Error in finding activity: " + anActivity.getName() + " for a hub: " + anActivity.getHub());
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@@ -147,40 +149,45 @@ public class HarmonyHandler {
|
|||||||
harmonyClient.pressButton(aDeviceButton.getDevice(), aDeviceButton.getButton());
|
harmonyClient.pressButton(aDeviceButton.getDevice(), aDeviceButton.getButton());
|
||||||
} catch (IllegalArgumentException ei) {
|
} catch (IllegalArgumentException ei) {
|
||||||
log.error("Error in finding device: " + aDeviceButton.getDevice() +" and a button: " + aDeviceButton.getButton() + " for a hub: " + aDeviceButton.getHub());
|
log.error("Error in finding device: " + aDeviceButton.getDevice() +" and a button: " + aDeviceButton.getButton() + " for a hub: " + aDeviceButton.getHub());
|
||||||
return false;
|
|
||||||
} catch (RuntimeException e1) {
|
} catch (RuntimeException e1) {
|
||||||
handleExceptionError(e1);
|
return handleExceptionError(e1);
|
||||||
}
|
}
|
||||||
} catch (RuntimeException e1) {
|
} catch (RuntimeException e1) {
|
||||||
handleExceptionError(e1);
|
return handleExceptionError(e1);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
log.error("Error in finding device: " + aDeviceButton.getDevice() +" and a button: " + aDeviceButton.getButton() + " for a hub: " + aDeviceButton.getHub());
|
log.error("Error in finding device: " + aDeviceButton.getDevice() +" and a button: " + aDeviceButton.getButton() + " for a hub: " + aDeviceButton.getHub());
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean handleExceptionError(Exception e) {
|
||||||
|
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....");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void handleExceptionError(Exception e) {
|
|
||||||
if(e.getMessage().equalsIgnoreCase("Failed communicating with Harmony Hub")) {
|
|
||||||
log.warn("Issue in communcicating with Harmony Hub, retrying connect....");
|
|
||||||
try {
|
|
||||||
harmonyClient.disconnect();
|
|
||||||
} catch(Exception e1) {
|
|
||||||
log.warn("Hub disconnect failed, continuing....");
|
|
||||||
}
|
|
||||||
harmonyClient.connect(myNameAndIP.getIp());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void shutdown() {
|
public void shutdown() {
|
||||||
log.debug("Harmony api shutdown requested.");
|
log.debug("Harmony api shutdown requested.");
|
||||||
if(devMode)
|
if(devMode)
|
||||||
return;
|
return;
|
||||||
|
try {
|
||||||
harmonyClient.disconnect();
|
harmonyClient.disconnect();
|
||||||
|
} catch(Exception e)
|
||||||
|
{
|
||||||
|
// noop
|
||||||
|
}
|
||||||
harmonyClient = null;
|
harmonyClient = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the myNameAndIP
|
||||||
|
*/
|
||||||
|
public NamedIP getMyNameAndIP() {
|
||||||
|
return myNameAndIP;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -74,11 +74,9 @@ public class HarmonyHome implements Home {
|
|||||||
Set<String> keys = hubs.keySet();
|
Set<String> keys = hubs.keySet();
|
||||||
if (!keys.isEmpty()) {
|
if (!keys.isEmpty()) {
|
||||||
aHandler = hubs.get(keys.toArray()[0]).getMyHarmony();
|
aHandler = hubs.get(keys.toArray()[0]).getMyHarmony();
|
||||||
}
|
} else
|
||||||
else
|
|
||||||
aHandler = null;
|
aHandler = null;
|
||||||
}
|
} else
|
||||||
else
|
|
||||||
aHandler = hubs.get(aName).getMyHarmony();
|
aHandler = hubs.get(aName).getMyHarmony();
|
||||||
return aHandler;
|
return aHandler;
|
||||||
}
|
}
|
||||||
@@ -91,6 +89,20 @@ public class HarmonyHome implements Home {
|
|||||||
while (keys.hasNext()) {
|
while (keys.hasNext()) {
|
||||||
String key = keys.next();
|
String key = keys.next();
|
||||||
Iterator<Activity> activities = hubs.get(key).getMyHarmony().getActivities().iterator();
|
Iterator<Activity> activities = hubs.get(key).getMyHarmony().getActivities().iterator();
|
||||||
|
if (activities == null) {
|
||||||
|
resetHub(hubs.get(key).getMyHarmony());
|
||||||
|
activities = hubs.get(key).getMyHarmony().getActivities().iterator();
|
||||||
|
if (activities == null) {
|
||||||
|
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) {
|
||||||
while (activities.hasNext()) {
|
while (activities.hasNext()) {
|
||||||
HarmonyActivity anActivity = new HarmonyActivity();
|
HarmonyActivity anActivity = new HarmonyActivity();
|
||||||
anActivity.setActivity(activities.next());
|
anActivity.setActivity(activities.next());
|
||||||
@@ -98,8 +110,10 @@ public class HarmonyHome implements Home {
|
|||||||
activityList.add(anActivity);
|
activityList.add(anActivity);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return activityList;
|
return activityList;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<HarmonyActivity> getCurrentActivities() {
|
public List<HarmonyActivity> getCurrentActivities() {
|
||||||
Iterator<String> keys = hubs.keySet().iterator();
|
Iterator<String> keys = hubs.keySet().iterator();
|
||||||
ArrayList<HarmonyActivity> activityList = new ArrayList<HarmonyActivity>();
|
ArrayList<HarmonyActivity> activityList = new ArrayList<HarmonyActivity>();
|
||||||
@@ -108,13 +122,29 @@ public class HarmonyHome implements Home {
|
|||||||
while (keys.hasNext()) {
|
while (keys.hasNext()) {
|
||||||
String key = keys.next();
|
String key = keys.next();
|
||||||
Activity theActivity = hubs.get(key).getMyHarmony().getCurrentActivity();
|
Activity theActivity = hubs.get(key).getMyHarmony().getCurrentActivity();
|
||||||
|
if (theActivity == null) {
|
||||||
|
resetHub(hubs.get(key).getMyHarmony());
|
||||||
|
theActivity = hubs.get(key).getMyHarmony().getCurrentActivity();
|
||||||
|
if (theActivity == null) {
|
||||||
|
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) {
|
||||||
HarmonyActivity anActivity = new HarmonyActivity();
|
HarmonyActivity anActivity = new HarmonyActivity();
|
||||||
anActivity.setActivity(theActivity);
|
anActivity.setActivity(theActivity);
|
||||||
anActivity.setHub(key);
|
anActivity.setHub(key);
|
||||||
activityList.add(anActivity);
|
activityList.add(anActivity);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return activityList;
|
return activityList;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<HarmonyDevice> getDevices() {
|
public List<HarmonyDevice> getDevices() {
|
||||||
Iterator<String> keys = hubs.keySet().iterator();
|
Iterator<String> keys = hubs.keySet().iterator();
|
||||||
ArrayList<HarmonyDevice> deviceList = new ArrayList<HarmonyDevice>();
|
ArrayList<HarmonyDevice> deviceList = new ArrayList<HarmonyDevice>();
|
||||||
@@ -123,6 +153,20 @@ public class HarmonyHome implements Home {
|
|||||||
while (keys.hasNext()) {
|
while (keys.hasNext()) {
|
||||||
String key = keys.next();
|
String key = keys.next();
|
||||||
Iterator<Device> devices = hubs.get(key).getMyHarmony().getDevices().iterator();
|
Iterator<Device> devices = hubs.get(key).getMyHarmony().getDevices().iterator();
|
||||||
|
if (devices == null) {
|
||||||
|
resetHub(hubs.get(key).getMyHarmony());
|
||||||
|
devices = hubs.get(key).getMyHarmony().getDevices().iterator();
|
||||||
|
if (devices == null) {
|
||||||
|
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) {
|
||||||
while (devices.hasNext()) {
|
while (devices.hasNext()) {
|
||||||
HarmonyDevice aDevice = new HarmonyDevice();
|
HarmonyDevice aDevice = new HarmonyDevice();
|
||||||
aDevice.setDevice(devices.next());
|
aDevice.setDevice(devices.next());
|
||||||
@@ -130,6 +174,7 @@ public class HarmonyHome implements Home {
|
|||||||
deviceList.add(aDevice);
|
deviceList.add(aDevice);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return deviceList;
|
return deviceList;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -144,8 +189,7 @@ public class HarmonyHome implements Home {
|
|||||||
+ "\",\"description\": \"Should not get here, no harmony configured\", \"parameter\": \"/lights/"
|
+ "\",\"description\": \"Should not get here, no harmony configured\", \"parameter\": \"/lights/"
|
||||||
+ lightId + "state\"}}]";
|
+ lightId + "state\"}}]";
|
||||||
} else {
|
} else {
|
||||||
if(anItem.getType().trim().equalsIgnoreCase(DeviceMapTypes.HARMONY_ACTIVITY[DeviceMapTypes.typeIndex]))
|
if (anItem.getType().trim().equalsIgnoreCase(DeviceMapTypes.HARMONY_ACTIVITY[DeviceMapTypes.typeIndex])) {
|
||||||
{
|
|
||||||
RunActivity anActivity = null;
|
RunActivity anActivity = null;
|
||||||
if (anItem.getItem().isJsonObject())
|
if (anItem.getItem().isJsonObject())
|
||||||
anActivity = aGsonHandler.fromJson(anItem.getItem(), RunActivity.class);
|
anActivity = aGsonHandler.fromJson(anItem.getItem(), RunActivity.class);
|
||||||
@@ -160,9 +204,21 @@ public class HarmonyHome implements Home {
|
|||||||
+ "\",\"description\": \"Should not get here, no harmony hub available\", \"parameter\": \"/lights/"
|
+ "\",\"description\": \"Should not get here, no harmony hub available\", \"parameter\": \"/lights/"
|
||||||
+ lightId + "state\"}}]";
|
+ lightId + "state\"}}]";
|
||||||
} else {
|
} else {
|
||||||
myHarmony.startActivity(anActivity);
|
if (!myHarmony.startActivity(anActivity)) {
|
||||||
|
if (resetHub(myHarmony)) {
|
||||||
|
myHarmony = getHarmonyHandler(anActivity.getHub());
|
||||||
|
if (!myHarmony.startActivity(anActivity)) {
|
||||||
|
log.error("Could not get communication restored with hub: " + anActivity.getHub()
|
||||||
|
+ ", please restart...");
|
||||||
|
responseString = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId
|
||||||
|
+ "\",\"description\": \"Could not communicate with harmony\", \"parameter\": \"/lights/"
|
||||||
|
+ lightId + "state\"}}]";
|
||||||
}
|
}
|
||||||
} else if(anItem.getType().trim().equalsIgnoreCase(DeviceMapTypes.HARMONY_BUTTON[DeviceMapTypes.typeIndex])) {
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (anItem.getType().trim()
|
||||||
|
.equalsIgnoreCase(DeviceMapTypes.HARMONY_BUTTON[DeviceMapTypes.typeIndex])) {
|
||||||
String url = null;
|
String url = null;
|
||||||
if (anItem.getItem().isJsonObject() || anItem.getItem().isJsonArray()) {
|
if (anItem.getItem().isJsonObject() || anItem.getItem().isJsonArray()) {
|
||||||
url = aGsonHandler.toJson(anItem.getItem());
|
url = aGsonHandler.toJson(anItem.getItem());
|
||||||
@@ -191,14 +247,29 @@ public class HarmonyHome implements Home {
|
|||||||
aMultiUtil.setTheDelay(anItem.getDelay());
|
aMultiUtil.setTheDelay(anItem.getDelay());
|
||||||
else
|
else
|
||||||
aMultiUtil.setTheDelay(aMultiUtil.getDelayDefault());
|
aMultiUtil.setTheDelay(aMultiUtil.getDelayDefault());
|
||||||
log.debug("pressing button: " + deviceButtons[z].getDevice() + " - " + deviceButtons[z].getButton() + " with pressTime of: " + deviceButtons[z].getPressTime() + " - iteration: " + String.valueOf(z) + " - count: " + String.valueOf(y));
|
log.debug("pressing button: " + deviceButtons[z].getDevice() + " - "
|
||||||
|
+ deviceButtons[z].getButton() + " with pressTime of: "
|
||||||
|
+ deviceButtons[z].getPressTime() + " - iteration: " + String.valueOf(z) + " - count: "
|
||||||
|
+ String.valueOf(y));
|
||||||
if (deviceButtons[z].getHub() == null || deviceButtons[z].getHub().isEmpty())
|
if (deviceButtons[z].getHub() == null || deviceButtons[z].getHub().isEmpty())
|
||||||
deviceButtons[z].setHub(device.getTargetDevice());
|
deviceButtons[z].setHub(device.getTargetDevice());
|
||||||
HarmonyHandler myHarmony = getHarmonyHandler(deviceButtons[z].getHub());
|
HarmonyHandler myHarmony = getHarmonyHandler(deviceButtons[z].getHub());
|
||||||
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{
|
||||||
myHarmony.pressButton(deviceButtons[z]);
|
if (!myHarmony.pressButton(deviceButtons[z])) {
|
||||||
|
if (resetHub(myHarmony)) {
|
||||||
|
myHarmony = getHarmonyHandler(deviceButtons[z].getHub());
|
||||||
|
if (!myHarmony.pressButton(deviceButtons[z])) {
|
||||||
|
log.error("Could not get communication restored with hub: " + deviceButtons[z].getHub()
|
||||||
|
+ ", please restart...");
|
||||||
|
responseString = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId
|
||||||
|
+ "\",\"description\": \"Could not communicate with harmony\", \"parameter\": \"/lights/"
|
||||||
|
+ lightId + "state\"}}]";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -210,12 +281,11 @@ public class HarmonyHome implements Home {
|
|||||||
public Home createHome(BridgeSettings bridgeSettings) {
|
public Home createHome(BridgeSettings bridgeSettings) {
|
||||||
isDevMode = Boolean.parseBoolean(System.getProperty("dev.mode", "false"));
|
isDevMode = Boolean.parseBoolean(System.getProperty("dev.mode", "false"));
|
||||||
validHarmony = bridgeSettings.getBridgeSettingsDescriptor().isValidHarmony();
|
validHarmony = bridgeSettings.getBridgeSettingsDescriptor().isValidHarmony();
|
||||||
log.info("Harmony Home created." + (validHarmony ? "" : " No Harmony devices configured.") + (isDevMode ? " DevMode is set." : ""));
|
log.info("Harmony Home created." + (validHarmony ? "" : " No Harmony devices configured.")
|
||||||
|
+ (isDevMode ? " DevMode is set." : ""));
|
||||||
if (validHarmony || isDevMode) {
|
if (validHarmony || isDevMode) {
|
||||||
hubs = new HashMap<String, HarmonyServer>();
|
hubs = new HashMap<String, HarmonyServer>();
|
||||||
aGsonHandler =
|
aGsonHandler = new GsonBuilder().create();
|
||||||
new GsonBuilder()
|
|
||||||
.create();
|
|
||||||
if (isDevMode) {
|
if (isDevMode) {
|
||||||
NamedIP devModeIp = new NamedIP();
|
NamedIP devModeIp = new NamedIP();
|
||||||
devModeIp.setIp("10.10.10.10");
|
devModeIp.setIp("10.10.10.10");
|
||||||
@@ -226,18 +296,20 @@ public class HarmonyHome implements Home {
|
|||||||
thedevList.setDevices(theList);
|
thedevList.setDevices(theList);
|
||||||
bridgeSettings.getBridgeSettingsDescriptor().setHarmonyAddress(thedevList);
|
bridgeSettings.getBridgeSettingsDescriptor().setHarmonyAddress(thedevList);
|
||||||
}
|
}
|
||||||
Iterator<NamedIP> theList = bridgeSettings.getBridgeSettingsDescriptor().getHarmonyAddress().getDevices().iterator();
|
Iterator<NamedIP> theList = bridgeSettings.getBridgeSettingsDescriptor().getHarmonyAddress().getDevices()
|
||||||
|
.iterator();
|
||||||
while (theList.hasNext() && validHarmony) {
|
while (theList.hasNext() && validHarmony) {
|
||||||
NamedIP aHub = theList.next();
|
NamedIP aHub = theList.next();
|
||||||
boolean loopControl = true;
|
boolean loopControl = true;
|
||||||
int retryCount = 0;
|
int retryCount = 0;
|
||||||
while (loopControl) {
|
while (loopControl) {
|
||||||
try {
|
try {
|
||||||
hubs.put(aHub.getName(), HarmonyServer.setup(bridgeSettings.getBridgeSettingsDescriptor(), isDevMode, aHub));
|
hubs.put(aHub.getName(), HarmonyServer.setup(isDevMode, aHub));
|
||||||
loopControl = false;
|
loopControl = false;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
if (retryCount > 3) {
|
if (retryCount > 3) {
|
||||||
log.error("Cannot get harmony client (" + aHub.getName() + ") setup, Exiting with message: " + e.getMessage(), e);
|
log.error("Cannot get harmony client (" + aHub.getName() + ") setup, Exiting with message: "
|
||||||
|
+ e.getMessage(), e);
|
||||||
loopControl = false;
|
loopControl = false;
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
@@ -257,6 +329,25 @@ public class HarmonyHome implements Home {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean resetHub(HarmonyHandler aHarmony) {
|
||||||
|
boolean resetSuccess = false;
|
||||||
|
isDevMode = Boolean.parseBoolean(System.getProperty("dev.mode", "false"));
|
||||||
|
NamedIP resetIp = aHarmony.getMyNameAndIP();
|
||||||
|
log.info("Resetting harmony hub due to communication errror: " + resetIp.getName());
|
||||||
|
if (!isDevMode) {
|
||||||
|
try {
|
||||||
|
hubs.remove(resetIp.getName());
|
||||||
|
aHarmony.shutdown();
|
||||||
|
hubs.put(resetIp.getName(), HarmonyServer.setup(isDevMode, resetIp));
|
||||||
|
resetSuccess = true;
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("Cannot reset harmony client (" + resetIp.getName() + "), Exiting with message: "
|
||||||
|
+ e.getMessage(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return resetSuccess;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object getItems(String type) {
|
public Object getItems(String type) {
|
||||||
if (validHarmony) {
|
if (validHarmony) {
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ import org.apache.http.client.methods.HttpGet;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import com.bwssystems.HABridge.BridgeSettingsDescriptor;
|
|
||||||
import com.bwssystems.HABridge.NamedIP;
|
import com.bwssystems.HABridge.NamedIP;
|
||||||
import com.google.inject.Guice;
|
import com.google.inject.Guice;
|
||||||
import com.google.inject.Injector;
|
import com.google.inject.Injector;
|
||||||
@@ -50,11 +49,10 @@ public class HarmonyServer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static HarmonyServer setup(
|
public static HarmonyServer setup(
|
||||||
BridgeSettingsDescriptor bridgeSettings,
|
|
||||||
Boolean harmonyDevMode,
|
Boolean harmonyDevMode,
|
||||||
NamedIP theHarmonyAddress
|
NamedIP theHarmonyAddress
|
||||||
) throws Exception {
|
) throws Exception {
|
||||||
if (!bridgeSettings.isValidHarmony() && harmonyDevMode) {
|
if (harmonyDevMode) {
|
||||||
return new HarmonyServer(theHarmonyAddress);
|
return new HarmonyServer(theHarmonyAddress);
|
||||||
}
|
}
|
||||||
Injector injector = null;
|
Injector injector = null;
|
||||||
@@ -65,11 +63,11 @@ public class HarmonyServer {
|
|||||||
if (!harmonyDevMode) {
|
if (!harmonyDevMode) {
|
||||||
injector.injectMembers(mainObject);
|
injector.injectMembers(mainObject);
|
||||||
}
|
}
|
||||||
mainObject.execute(bridgeSettings, harmonyDevMode);
|
mainObject.execute(harmonyDevMode);
|
||||||
return mainObject;
|
return mainObject;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void execute(BridgeSettingsDescriptor mySettings, Boolean harmonyDevMode) throws Exception {
|
private void execute(Boolean harmonyDevMode) throws Exception {
|
||||||
Boolean noopCalls = Boolean.parseBoolean(System.getProperty("noop.calls", "false"));
|
Boolean noopCalls = Boolean.parseBoolean(System.getProperty("noop.calls", "false"));
|
||||||
isDevMode = harmonyDevMode;
|
isDevMode = harmonyDevMode;
|
||||||
String modeString = "";
|
String modeString = "";
|
||||||
|
|||||||
@@ -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