mirror of
https://github.com/bwssytems/ha-bridge.git
synced 2025-12-18 08:13:23 +00:00
Added Tests for broadlink ircmomand conversion. Added checks for
successful auth with broadlink devices. Updated broadlink API CmdPacket build.
This commit is contained in:
4
pom.xml
4
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.0RC12</version>
|
<version>5.2.0RC13</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<name>HA Bridge</name>
|
<name>HA Bridge</name>
|
||||||
@@ -127,7 +127,7 @@
|
|||||||
<version>2.1.6</version>
|
<version>2.1.6</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.github.mob41</groupId>
|
<groupId>com.github.bwssytems</groupId>
|
||||||
<artifactId>broadlink-java-api</artifactId>
|
<artifactId>broadlink-java-api</artifactId>
|
||||||
<version>-SNAPSHOT</version>
|
<version>-SNAPSHOT</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|||||||
@@ -28,7 +28,6 @@ import com.bwssystems.HABridge.hue.MultiCommandUtil;
|
|||||||
import com.bwssystems.HABridge.hue.TimeDecode;
|
import com.bwssystems.HABridge.hue.TimeDecode;
|
||||||
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.RM2Device;
|
|
||||||
import com.github.mob41.blapi.SP1Device;
|
import com.github.mob41.blapi.SP1Device;
|
||||||
import com.github.mob41.blapi.SP2Device;
|
import com.github.mob41.blapi.SP2Device;
|
||||||
import com.github.mob41.blapi.pkt.cmd.rm2.SendDataCmdPayload;
|
import com.github.mob41.blapi.pkt.cmd.rm2.SendDataCmdPayload;
|
||||||
@@ -159,18 +158,24 @@ public class BroadlinkHome implements Home {
|
|||||||
else
|
else
|
||||||
changeState = false;
|
changeState = false;
|
||||||
try {
|
try {
|
||||||
if(!isDevMode)
|
if(!isDevMode) {
|
||||||
theDevice.auth();
|
if(!theDevice.auth()) {
|
||||||
|
log.error("Call to " + broadlinkCommand.getId() + " - " + _rm2 + " device authorization failed.");
|
||||||
|
theReturn = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId
|
||||||
|
+ "\",\"description\": \"" + broadlinkCommand.getId() + " - " + _rm2 + " device auth error.\", \"parameter\": \"/lights/"
|
||||||
|
+ lightId + "state\"}}]";
|
||||||
|
}
|
||||||
|
}
|
||||||
((MP1Device) theDevice).setState(Integer.parseInt(broadlinkCommand.getData()), changeState);
|
((MP1Device) theDevice).setState(Integer.parseInt(broadlinkCommand.getData()), changeState);
|
||||||
} catch (NumberFormatException e1) {
|
} catch (NumberFormatException e1) {
|
||||||
log.error("Call to " + _mp1 + " device failed with number format exception.", e1);
|
log.error("Call to " + broadlinkCommand.getId() + " - " + _mp1 + " device failed with number format exception.", e1);
|
||||||
theReturn = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId
|
theReturn = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId
|
||||||
+ "\",\"description\": \"" + _mp1 + " number format error.\", \"parameter\": \"/lights/"
|
+ "\",\"description\": \"" + broadlinkCommand.getId() + " - " + _mp1 + " number format error.\", \"parameter\": \"/lights/"
|
||||||
+ lightId + "state\"}}]";
|
+ lightId + "state\"}}]";
|
||||||
} catch (Exception e1) {
|
} catch (Exception e1) {
|
||||||
log.error("Call to " + _mp1 + " device failed with exception.", e1);
|
log.error("Call to " + broadlinkCommand.getId() + " - " + _mp1 + " device failed with exception.", e1);
|
||||||
theReturn = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId
|
theReturn = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId
|
||||||
+ "\",\"description\": \"" + _mp1 + " device call error.\", \"parameter\": \"/lights/"
|
+ "\",\"description\": \"" + broadlinkCommand.getId() + " - " + _mp1 + " device call error.\", \"parameter\": \"/lights/"
|
||||||
+ lightId + "state\"}}]";
|
+ lightId + "state\"}}]";
|
||||||
};
|
};
|
||||||
break;
|
break;
|
||||||
@@ -190,13 +195,19 @@ public class BroadlinkHome implements Home {
|
|||||||
else
|
else
|
||||||
changeState = false;
|
changeState = false;
|
||||||
try {
|
try {
|
||||||
if(!isDevMode)
|
if(!isDevMode) {
|
||||||
theDevice.auth();
|
if(!theDevice.auth()) {
|
||||||
|
log.error("Call to " + broadlinkCommand.getId() + " - " + _rm2 + " device authorization failed.");
|
||||||
|
theReturn = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId
|
||||||
|
+ "\",\"description\": \"" + broadlinkCommand.getId() + " - " + _rm2 + " device auth error.\", \"parameter\": \"/lights/"
|
||||||
|
+ lightId + "state\"}}]";
|
||||||
|
}
|
||||||
|
}
|
||||||
((SP2Device) theDevice).setState(changeState);
|
((SP2Device) theDevice).setState(changeState);
|
||||||
} catch (Exception e1) {
|
} catch (Exception e1) {
|
||||||
log.error("Call to " + _sp2 + " device failed with exception.", e1);
|
log.error("Call to " + broadlinkCommand.getId() + " - " + _sp2 + " device failed with exception.", e1);
|
||||||
theReturn = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId
|
theReturn = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId
|
||||||
+ "\",\"description\": \"" + _sp2 + " device call error.\", \"parameter\": \"/lights/"
|
+ "\",\"description\": \"" + broadlinkCommand.getId() + " - " + _sp2 + " device call error.\", \"parameter\": \"/lights/"
|
||||||
+ lightId + "state\"}}]";
|
+ lightId + "state\"}}]";
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -206,11 +217,17 @@ public class BroadlinkHome implements Home {
|
|||||||
else
|
else
|
||||||
changeState = false;
|
changeState = false;
|
||||||
try {
|
try {
|
||||||
if(!isDevMode)
|
if(!isDevMode) {
|
||||||
theDevice.auth();
|
if(!theDevice.auth()) {
|
||||||
|
log.error("Call to " + broadlinkCommand.getId() + " - " + _rm2 + " device authorization failed.");
|
||||||
|
theReturn = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId
|
||||||
|
+ "\",\"description\": \"" + broadlinkCommand.getId() + " - " + _rm2 + " device auth error.\", \"parameter\": \"/lights/"
|
||||||
|
+ lightId + "state\"}}]";
|
||||||
|
}
|
||||||
|
}
|
||||||
((SP1Device) theDevice).setPower(changeState);
|
((SP1Device) theDevice).setPower(changeState);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("Call to " + _sp1 + " device failed with exception.", e);
|
log.error("Call to " + broadlinkCommand.getId() + " - " + _sp1 + " device failed with exception.", e);
|
||||||
theReturn = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId
|
theReturn = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId
|
||||||
+ "\",\"description\": \"" + _sp1 + " device call error.\", \"parameter\": \"/lights/"
|
+ "\",\"description\": \"" + _sp1 + " device call error.\", \"parameter\": \"/lights/"
|
||||||
+ lightId + "state\"}}]";
|
+ lightId + "state\"}}]";
|
||||||
@@ -238,22 +255,29 @@ public class BroadlinkHome implements Home {
|
|||||||
byte[] theData = DatatypeConverter.parseHexBinary(theStringData);
|
byte[] theData = DatatypeConverter.parseHexBinary(theStringData);
|
||||||
SendDataCmdPayload thePayload = new SendDataCmdPayload(theData);
|
SendDataCmdPayload thePayload = new SendDataCmdPayload(theData);
|
||||||
try {
|
try {
|
||||||
if(!isDevMode)
|
if(!isDevMode) {
|
||||||
theDevice.auth();
|
if(!theDevice.auth()) {
|
||||||
DatagramPacket thePacket = ((RM2Device) theDevice).sendCmdPkt(Configuration.BROADLINK_DISCONVER_TIMEOUT, thePayload);
|
log.error("Call to " + broadlinkCommand.getId() + " - " + _rm2 + " device authorization failed.");
|
||||||
|
theReturn = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId
|
||||||
|
+ "\",\"description\": \"" + broadlinkCommand.getId() + " - " + _rm2 + " device auth error.\", \"parameter\": \"/lights/"
|
||||||
|
+ lightId + "state\"}}]";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
DatagramPacket thePacket = theDevice.sendCmdPkt(Configuration.BROADLINK_DISCONVER_TIMEOUT, thePayload);
|
||||||
String returnData = DatatypeConverter.printHexBinary(thePacket.getData());
|
String returnData = DatatypeConverter.printHexBinary(thePacket.getData());
|
||||||
log.debug("RM2 Device data return: <<<" + returnData + ">>>");
|
log.debug("RM2 Device data return: <<<" + returnData + ">>>");
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
log.error("Call to " + _rm2 + " device failed with exception.", e);
|
log.error("Call to " + broadlinkCommand.getId() + " - " + _rm2 + " device failed with exception.", e);
|
||||||
theReturn = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId
|
theReturn = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId
|
||||||
+ "\",\"description\": \"" + _rm2 + " device call error.\", \"parameter\": \"/lights/"
|
+ "\",\"description\": \"" + broadlinkCommand.getId() + " - " + _rm2 + " device call error.\", \"parameter\": \"/lights/"
|
||||||
+ lightId + "state\"}}]";
|
+ lightId + "state\"}}]";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
log.error("Call to " + _rm2 + " with no data, noop");
|
log.error("Call to " + broadlinkCommand.getId() + " - " + _rm2 + " with no data, noop");
|
||||||
theReturn = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId
|
theReturn = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId
|
||||||
+ "\",\"description\": \"" + _rm2 + " could not call device without data.\", \"parameter\": \"/lights/"
|
+ "\",\"description\": \"" + broadlinkCommand.getId() + " - " + _rm2 + " could not call device without data.\", \"parameter\": \"/lights/"
|
||||||
+ lightId + "state\"}}]";
|
+ lightId + "state\"}}]";
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -0,0 +1,37 @@
|
|||||||
|
package com.bwssystems.broadlink.test;
|
||||||
|
|
||||||
|
import javax.xml.bind.DatatypeConverter;
|
||||||
|
|
||||||
|
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 String hexString1 = "5468697349734154657374537472696E6731";
|
||||||
|
public final static byte[] testData2 = { 110, 0, 110, 1, 13, 4, 40, 0, 1, 79, 0, 1, 26, (byte) 139, 18, 17, 18, 52, 17, 53, 17, 18, 27, 8, 17, 18, 30, 5, 17, 18, 18, 51};
|
||||||
|
public final static String hexString2 = "6e006e010d042800014f00011a8b12111234113511121b0811121e051112123312111211123412331234123412331212111212111112123312121211111212341134113412341211113511341135110004db00011a8c11121134123412111112111212111212113411121112113511341135113511341112111211121112123411121112121111351135113412331212113412341135110004db00011a8c11121135113412111211111211131112113411121211123411351134123411341211121112111212113411121212111211341135113412341112113511341234110004dc0001198d11121134113511121112111211121112113511121112113511341135113412341112111212111211123411121112111211351134123411341212113412341134120004db00011a8c11121135113411121112121211111211123411121112123411341234123411341211121112111212113412111211121112341234113412341112113511341234110005dc00000000000000000000";
|
||||||
|
public final static byte[] testData3 = { 38, 0, 120, 0, 88, 28};
|
||||||
|
public final static String hexString3 = "26007800581c111b0f0f0e0f2b2b100e0f0f100d0f0f0e0f100d0f0f0e0f100e1d0f0e100e0f0e1e1f000add571c111b0f0f110c2c2b100e0e0f100e0e0f0e0f100e0e0f0e0f110d1d100e0f0e0f0e1e1f000add571d0f1c100e100d2c2c0e0f0e0f0f0f0e0f0e0f110d0f0e100d100f1c100e0f0f0e0f1e1d000d05";
|
||||||
|
public Boolean validateStructure() {
|
||||||
|
String theHexString = hexString3;
|
||||||
|
byte[] theBytes = testData3;
|
||||||
|
byte[] theData;
|
||||||
|
|
||||||
|
|
||||||
|
System.out.println("----------------------------------");
|
||||||
|
System.out.println("This is the test hex string: <<<" + theHexString + ">>>");
|
||||||
|
try {
|
||||||
|
theData = DatatypeConverter.parseHexBinary(theHexString);
|
||||||
|
System.out.println("This is the test hex string from data bytes: <<<" + DatatypeConverter.printHexBinary(theData) + ">>>");
|
||||||
|
} catch(Exception e) {
|
||||||
|
System.out.println("Error parsing he string: " + e.getMessage());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
for( int i = 0; i < theBytes.length; i++) {
|
||||||
|
if(theBytes[i] != theData[i]) {
|
||||||
|
System.out.println("the compare data is not the same length at index: " + i);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.out.println("----------------------------------");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package com.bwssystems.broadlink.test;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class BroadlinkDataTestCase {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testValidateStructure() {
|
||||||
|
BroadlinkDataConstructor aTestService = new BroadlinkDataConstructor();
|
||||||
|
Assert.assertEquals(aTestService.validateStructure(), true);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user