mirror of
https://github.com/bwssytems/ha-bridge.git
synced 2025-12-19 08:28:46 +00:00
Fixed openhab build screen and openhab invalid responses.
Completed Broadlink impl Testing
This commit is contained in:
@@ -41,7 +41,6 @@ public class BroadlinkHome implements Home {
|
||||
private static final String _rm2 = "RM2";
|
||||
private Map<String, BLDevice> broadlinkMap;
|
||||
private Boolean validBroadlink;
|
||||
private Gson aGsonHandler;
|
||||
private boolean closed;
|
||||
private Boolean isDevMode;
|
||||
|
||||
@@ -55,7 +54,6 @@ public class BroadlinkHome implements Home {
|
||||
@Override
|
||||
public Home createHome(BridgeSettings bridgeSettings) {
|
||||
broadlinkMap = null;
|
||||
aGsonHandler = null;
|
||||
BLDevice[] clients;
|
||||
|
||||
isDevMode = Boolean.parseBoolean(System.getProperty("dev.mode", "false"));
|
||||
@@ -127,10 +125,7 @@ public class BroadlinkHome implements Home {
|
||||
|
||||
} else {
|
||||
BroadlinkEntry broadlinkCommand = null;
|
||||
if(anItem.getItem().isJsonObject())
|
||||
broadlinkCommand = aGsonHandler.fromJson(anItem.getItem(), BroadlinkEntry.class);
|
||||
else
|
||||
broadlinkCommand = aGsonHandler.fromJson(anItem.getItem().getAsString(), BroadlinkEntry.class);
|
||||
broadlinkCommand = new Gson().fromJson(anItem.getItem().getAsString(), BroadlinkEntry.class);
|
||||
BLDevice theDevice = broadlinkMap.get(broadlinkCommand.getId());
|
||||
if (theDevice == null) {
|
||||
log.warn("Should not get here, no BroadlinkDevices available");
|
||||
@@ -149,6 +144,8 @@ public class BroadlinkHome implements Home {
|
||||
else
|
||||
changeState = false;
|
||||
try {
|
||||
if(!isDevMode)
|
||||
theDevice.auth();
|
||||
((MP1Device) theDevice).setState(Integer.parseInt(broadlinkCommand.getData()), changeState);
|
||||
} catch (NumberFormatException e1) {
|
||||
log.error("Call to " + _mp1 + " device failed with number format exception.", e1);
|
||||
@@ -178,6 +175,8 @@ public class BroadlinkHome implements Home {
|
||||
else
|
||||
changeState = false;
|
||||
try {
|
||||
if(!isDevMode)
|
||||
theDevice.auth();
|
||||
((SP2Device) theDevice).setState(changeState);
|
||||
} catch (Exception e1) {
|
||||
log.error("Call to " + _sp2 + " device failed with exception.", e1);
|
||||
@@ -192,6 +191,8 @@ public class BroadlinkHome implements Home {
|
||||
else
|
||||
changeState = false;
|
||||
try {
|
||||
if(!isDevMode)
|
||||
theDevice.auth();
|
||||
((SP1Device) theDevice).setPower(changeState);
|
||||
} catch (Exception e) {
|
||||
log.error("Call to " + _sp1 + " device failed with exception.", e);
|
||||
@@ -210,8 +211,9 @@ public class BroadlinkHome implements Home {
|
||||
case BLDevice.DEV_RM_2_PRO_PLUS_2_BL:
|
||||
case BLDevice.DEV_RM_MINI_SHATE:
|
||||
if(broadlinkCommand.getData() != null && !broadlinkCommand.getData().trim().isEmpty()) {
|
||||
theStringData = broadlinkCommand.getData().trim();
|
||||
if(targetBri != null || targetBriInc != null) {
|
||||
theStringData = BrightnessDecode.calculateReplaceIntensityValue(broadlinkCommand.getData().trim(), intensity, targetBri, targetBriInc, true);
|
||||
theStringData = BrightnessDecode.calculateReplaceIntensityValue(theStringData, intensity, targetBri, targetBriInc, true);
|
||||
}
|
||||
if(colorData != null) {
|
||||
theStringData = ColorDecode.replaceColorData(theStringData, colorData, BrightnessDecode.calculateIntensity(intensity, targetBri, targetBriInc), true);
|
||||
@@ -221,6 +223,8 @@ public class BroadlinkHome implements Home {
|
||||
byte[] theData = DatatypeConverter.parseHexBinary(theStringData);
|
||||
SendDataCmdPayload thePayload = new SendDataCmdPayload(theData);
|
||||
try {
|
||||
if(!isDevMode)
|
||||
theDevice.auth();
|
||||
((RM2Device) theDevice).sendCmdPkt(Configuration.BROADLINK_DISCONVER_TIMEOUT, thePayload);
|
||||
} catch (IOException e) {
|
||||
log.error("Call to " + _rm2 + " device failed with exception.", e);
|
||||
|
||||
@@ -15,17 +15,9 @@ import com.github.mob41.blapi.pkt.CmdPayload;
|
||||
|
||||
public class TestBLDevice extends BLDevice {
|
||||
private static final Logger log = LoggerFactory.getLogger(TestBLDevice.class);
|
||||
short adeviceType;
|
||||
String adeviceDesc;
|
||||
String ahost;
|
||||
Mac amac;
|
||||
|
||||
protected TestBLDevice(short deviceType, String deviceDesc, String host, Mac mac) throws IOException {
|
||||
super(deviceType, host, host, mac);
|
||||
adeviceType = deviceType;
|
||||
adeviceDesc = deviceDesc;
|
||||
ahost = host;
|
||||
|
||||
super(deviceType, deviceDesc, host, mac);
|
||||
}
|
||||
|
||||
public void setState(boolean aState) {
|
||||
@@ -46,30 +38,30 @@ public class TestBLDevice extends BLDevice {
|
||||
}
|
||||
|
||||
public static BLDevice[] discoverDevices(InetAddress theAddress, int aport, int timeout) {
|
||||
TestBLDevice mp1Device = null;
|
||||
TestBLDevice sp1Device = null;
|
||||
TestBLDevice sp2Device = null;
|
||||
TestBLDevice rm2Device = null;
|
||||
TestMP1Device mp1Device = null;
|
||||
TestSP1Device sp1Device = null;
|
||||
TestSP2Device sp2Device = null;
|
||||
TestRM2Device rm2Device = null;
|
||||
try {
|
||||
mp1Device = new TestBLDevice(BLDevice.DEV_MP1, BLDevice.DESC_MP1, "mp1host", null);
|
||||
mp1Device = new TestMP1Device("mp1host", null);
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
try {
|
||||
sp1Device = new TestBLDevice(BLDevice.DEV_SP1, BLDevice.DESC_SP1, "sp1host", null);
|
||||
sp1Device = new TestSP1Device("sp1host", null);
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
try {
|
||||
sp2Device = new TestBLDevice(BLDevice.DEV_SP2, BLDevice.DESC_SP2, "sp2host", null);
|
||||
sp2Device = new TestSP2Device("sp2host", null);
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
try {
|
||||
rm2Device = new TestBLDevice(BLDevice.DEV_RM_2, BLDevice.DESC_RM_2, "rm2host", null);
|
||||
rm2Device = new TestRM2Device("rm2host", null);
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.bwssystems.HABridge.plugins.broadlink;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
import javax.xml.bind.DatatypeConverter;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.github.mob41.blapi.MP1Device;
|
||||
import com.github.mob41.blapi.mac.Mac;
|
||||
import com.github.mob41.blapi.pkt.CmdPayload;
|
||||
|
||||
public class TestMP1Device extends MP1Device {
|
||||
private static final Logger log = LoggerFactory.getLogger(TestMP1Device.class);
|
||||
|
||||
protected TestMP1Device(String host, Mac mac) throws IOException {
|
||||
super(host, mac);
|
||||
}
|
||||
|
||||
public void setState(int anIndex, boolean aState) {
|
||||
log.info("setState called with index " + anIndex + " and state " + aState);
|
||||
}
|
||||
|
||||
public DatagramPacket sendCmdPkt(int timeout, CmdPayload aCmd) {
|
||||
log.info("sendCmdPkt called with " + DatatypeConverter.printHexBinary(aCmd.getPayload().getData()));
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.bwssystems.HABridge.plugins.broadlink;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
|
||||
import javax.xml.bind.DatatypeConverter;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.github.mob41.blapi.RM2Device;
|
||||
import com.github.mob41.blapi.mac.Mac;
|
||||
import com.github.mob41.blapi.pkt.CmdPayload;
|
||||
import com.github.mob41.blapi.pkt.cmd.rm2.SendDataCmdPayload;
|
||||
|
||||
public class TestRM2Device extends RM2Device {
|
||||
private static final Logger log = LoggerFactory.getLogger(TestRM2Device.class);
|
||||
|
||||
protected TestRM2Device(String host, Mac mac) throws IOException {
|
||||
super(host, mac);
|
||||
}
|
||||
|
||||
public DatagramPacket sendCmdPkt(int timeout, CmdPayload aCmd) {
|
||||
log.info("sendCmdPkt called with " + DatatypeConverter.printHexBinary(((SendDataCmdPayload)aCmd).getData()));
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.bwssystems.HABridge.plugins.broadlink;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
|
||||
import javax.xml.bind.DatatypeConverter;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.github.mob41.blapi.SP1Device;
|
||||
import com.github.mob41.blapi.mac.Mac;
|
||||
import com.github.mob41.blapi.pkt.CmdPayload;
|
||||
|
||||
public class TestSP1Device extends SP1Device {
|
||||
private static final Logger log = LoggerFactory.getLogger(TestSP1Device.class);
|
||||
|
||||
protected TestSP1Device(String host, Mac mac) throws IOException {
|
||||
super(host, mac);
|
||||
}
|
||||
|
||||
public void setPower(boolean aState) {
|
||||
log.info("setPower called with " + aState);
|
||||
}
|
||||
|
||||
public DatagramPacket sendCmdPkt(int timeout, CmdPayload aCmd) {
|
||||
log.info("sendCmdPkt called with " + DatatypeConverter.printHexBinary(aCmd.getPayload().getData()));
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.bwssystems.HABridge.plugins.broadlink;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
|
||||
import javax.xml.bind.DatatypeConverter;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.github.mob41.blapi.SP2Device;
|
||||
import com.github.mob41.blapi.mac.Mac;
|
||||
import com.github.mob41.blapi.pkt.CmdPayload;
|
||||
|
||||
public class TestSP2Device extends SP2Device {
|
||||
private static final Logger log = LoggerFactory.getLogger(TestSP2Device.class);
|
||||
|
||||
protected TestSP2Device(String host, Mac mac) throws IOException {
|
||||
super(host, mac);
|
||||
}
|
||||
|
||||
public void setState(boolean aState) {
|
||||
log.info("setState called with " + aState);
|
||||
}
|
||||
|
||||
public DatagramPacket sendCmdPkt(int timeout, CmdPayload aCmd) {
|
||||
log.info("sendCmdPkt called with " + DatatypeConverter.printHexBinary(aCmd.getPayload().getData()));
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -99,6 +99,8 @@ public class HTTPHandler {
|
||||
}
|
||||
}
|
||||
if (response != null && response.getStatusLine().getStatusCode() >= 200 && response.getStatusLine().getStatusCode() < 300) {
|
||||
if(theContent == null)
|
||||
theContent = "";
|
||||
log.debug("Successfull response - The http response is <<<" + theContent + ">>>");
|
||||
retryCount = 2;
|
||||
} else if (response != null) {
|
||||
@@ -107,10 +109,9 @@ public class HTTPHandler {
|
||||
if (response.getStatusLine().getStatusCode() == 504) {
|
||||
log.warn("HTTP response code was 504, retrying...");
|
||||
log.debug("The 504 error content is <<<" + theContent + ">>>");
|
||||
theContent = null;
|
||||
} else
|
||||
retryCount = 2;
|
||||
|
||||
theContent = null;
|
||||
}
|
||||
|
||||
} catch (ClientProtocolException e) {
|
||||
|
||||
@@ -88,7 +88,13 @@ public class OpenHABHome implements Home {
|
||||
aCommand = TimeDecode.replaceTimeValue(aCommand);
|
||||
}
|
||||
try {
|
||||
theHandler.callCommand(anUrl, aCommand, httpClient);
|
||||
boolean success = theHandler.callCommand(anUrl, aCommand, httpClient);
|
||||
if(!success) {
|
||||
log.warn("Comand had error to OpenHAB");
|
||||
responseString = new Gson().toJson(HueErrorResponse.createResponse("6", "/lights/" + lightId,
|
||||
"Error on calling url to change device state", "/lights/"
|
||||
+ lightId + "state", null, null).getTheErrors(), HueError[].class);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("Cannot send comand to OpenHAB", e);
|
||||
responseString = new Gson().toJson(HueErrorResponse.createResponse("6", "/lights/" + lightId,
|
||||
|
||||
@@ -44,6 +44,8 @@ public class OpenHABInstance {
|
||||
aUrl = aUrl + theOpenHAB.getIp() + ":" + theOpenHAB.getPort() + "/" + aCommand;
|
||||
String theData = httpClient.doHttpRequest(aUrl, HttpPost.METHOD_NAME, "text/plain", commandData, headers);
|
||||
log.debug("call Command return is: <" + theData + ">");
|
||||
if(theData.contains("error") || theData.contains("ERROR") || theData.contains("Error"))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user