Fixed issue with http client pool and with test user for web interface.

This commit is contained in:
Admin
2017-11-17 12:37:46 -06:00
parent 5eca809c4a
commit 05b9f195d7
15 changed files with 99 additions and 33 deletions

View File

@@ -5,7 +5,7 @@
<groupId>com.bwssystems.HABridge</groupId>
<artifactId>ha-bridge</artifactId>
<version>5.0.0rc5</version>
<version>5.0.0rc6</version>
<packaging>jar</packaging>
<name>HA Bridge</name>

View File

@@ -9,6 +9,7 @@ import org.slf4j.LoggerFactory;
import com.bwssystems.HABridge.devicemanagmeent.*;
import com.bwssystems.HABridge.hue.HueMulator;
import com.bwssystems.HABridge.plugins.http.HttpClientPool;
import com.bwssystems.HABridge.upnp.UpnpListener;
import com.bwssystems.HABridge.upnp.UpnpSettingsResource;
import com.bwssystems.HABridge.util.UDPDatagramSender;
@@ -41,14 +42,17 @@ public class HABridge {
SystemControl theSystem;
BridgeSettings bridgeSettings;
Version theVersion;
@SuppressWarnings("unused")
HttpClientPool thePool;
theVersion = new Version();
// Singleton initialization
thePool = new HttpClientPool();
log.info("HA Bridge (v" + theVersion.getVersion() + ") starting....");
bridgeSettings = new BridgeSettings();
// sparkjava config directive to set html static file location for Jetty
staticFileLocation("/public");
while(!bridgeSettings.getBridgeControl().isStop()) {
bridgeSettings.buildSettings();
bridgeSettings.getBridgeSecurity().removeTestUsers();
@@ -57,6 +61,7 @@ public class HABridge {
ipAddress(bridgeSettings.getBridgeSettingsDescriptor().getWebaddress());
// sparkjava config directive to set port for the web server to listen on
port(bridgeSettings.getBridgeSettingsDescriptor().getServerPort());
staticFileLocation("/public");
initExceptionHandler((e) -> HABridge.theExceptionHandler(e, bridgeSettings.getBridgeSettingsDescriptor().getServerPort()));
if(!bridgeSettings.getBridgeControl().isReinit())
init();
@@ -115,6 +120,14 @@ public class HABridge {
bridgeSettings.getBridgeSecurity().removeTestUsers();
if(bridgeSettings.getBridgeSecurity().isSettingsChanged())
bridgeSettings.updateConfigFile();
try {
HttpClientPool.shutdown();
thePool = null;
} catch (InterruptedException e) {
log.warn("Error shutting down http pool: " + e.getMessage());;
} catch (IOException e) {
log.warn("Error shutting down http pool: " + e.getMessage());;
}
log.info("HA Bridge (v" + theVersion.getVersion() + ") exiting....");
System.exit(0);
}

View File

@@ -10,15 +10,18 @@ public class LinkButtonPressed extends TimerTask {
private static final Logger log = LoggerFactory.getLogger(LinkButtonPressed.class);
private BridgeControlDescriptor linkDescriptor;
private Timer myTimer;
private boolean isSilent;
public LinkButtonPressed(BridgeControlDescriptor theDescriptor, Timer aTimer) {
public LinkButtonPressed(BridgeControlDescriptor theDescriptor, Timer aTimer, boolean keepSilent) {
linkDescriptor = theDescriptor;
myTimer = aTimer;
isSilent = keepSilent;
}
@Override
public void run() {
log.info("Link button time ended....");
if(!isSilent)
log.info("Link button time ended....");
linkDescriptor.setLinkButton(false);
myTimer.cancel();
}

View File

@@ -0,0 +1,20 @@
package com.bwssystems.HABridge;
public class LinkParams {
private Integer seconds;
private boolean silent;
public Integer getSeconds() {
return seconds;
}
public void setSeconds(Integer seconds) {
this.seconds = seconds;
}
public boolean isSilent() {
return silent;
}
public void setSilent(boolean silent) {
this.silent = silent;
}
}

View File

@@ -245,10 +245,22 @@ public class SystemControl {
});
// http://ip_address:port/system/presslinkbutton which sets the link button for device registration
put(SYSTEM_CONTEXT + "/presslinkbutton", (request, response) -> {
log.info("Link button pressed....");
LinkParams linkParams = null;
if(!request.body().isEmpty()) {
linkParams = new Gson().fromJson(request.body(), LinkParams.class);
if(linkParams.getSeconds() <= 0)
linkParams.setSeconds(1);
}
else {
linkParams = new LinkParams();
linkParams.setSilent(false);
linkParams.setSeconds(30);
}
if(!linkParams.isSilent())
log.info("Link button pressed....");
bridgeSettings.getBridgeControl().setLinkButton(true);
Timer theTimer = new Timer();
theTimer.schedule(new LinkButtonPressed(bridgeSettings.getBridgeControl(), theTimer), 30000);
theTimer.schedule(new LinkButtonPressed(bridgeSettings.getBridgeControl(), theTimer, linkParams.isSilent()), (linkParams.getSeconds() * 1000));
response.status(HttpStatus.SC_OK);
response.type("application/json");
return "";

View File

@@ -1097,8 +1097,10 @@ public class HueMulator {
aMultiUtil.setSetCount(1);
log.debug("hue state change requested: " + userId + " from " + ipAddress + " body: " + body);
HueError[] theErrors = bridgeSettingMaster.getBridgeSecurity().validateWhitelistUser(userId, null, bridgeSettingMaster.getBridgeSecurity().isUseLinkButton());
if (theErrors != null)
if (theErrors != null) {
log.warn("Errors in security: <<<" + aGsonHandler.toJson(theErrors) + ">>>");
return aGsonHandler.toJson(theErrors);
}
try {
theStateChanges = aGsonHandler.fromJson(body, StateChangeBody.class);
} catch (Exception e) {
@@ -1228,14 +1230,19 @@ public class HueMulator {
} else if (ctInc != null && ctInc != 0) {
colorData = new ColorData(ColorData.ColorMode.CT, state.getCt() + ctInc);
}
log.debug("Calling Home device handler for type : " + callItems[i].getType().trim());
responseString = homeManager.findHome(callItems[i].getType().trim()).deviceHandler(callItems[i], aMultiUtil, lightId, state.getBri(), targetBri, targetBriInc, colorData, device, body);
if(responseString != null && responseString.contains("{\"error\":")) {
x = aMultiUtil.getSetCount();
}
}
}
else
log.warn("Call Items type is null <<<" + callItems[i] + ">>>");
}
if(callItems.length == 0)
log.warn("No call items were available: <<<" + url + ">>>");
} else {
log.warn("Could not find url: " + lightId + " for hue state change request: " + userId + " from "
+ ipAddress + " body: " + body);
@@ -1244,6 +1251,7 @@ public class HueMulator {
}
if (responseString == null || !responseString.contains("[{\"error\":")) {
log.debug("Response is in error: " + ((responseString == null) ? "null" : responseString));
if(!device.isNoState()) {
responseString = this.formatSuccessHueResponse(theStateChanges, body, lightId, state, targetBri, targetBriInc, device.isOffState());
device.setDeviceState(state);

View File

@@ -20,6 +20,7 @@ import com.bwssystems.HABridge.hue.BrightnessDecode;
import com.bwssystems.HABridge.hue.ColorData;
import com.bwssystems.HABridge.hue.MultiCommandUtil;
import com.bwssystems.HABridge.plugins.http.HTTPHandler;
import com.bwssystems.HABridge.plugins.http.HTTPHome;
import com.google.gson.Gson;
public class DomoticzHome implements Home {
@@ -132,7 +133,7 @@ public class DomoticzHome implements Home {
log.info("Domoticz Home created." + (validDomoticz ? "" : " No Domoticz devices configured."));
if(!validDomoticz)
return null;
httpClient = new HTTPHandler();
httpClient = HTTPHome.getHandler();
domoticzs = new HashMap<String, DomoticzHandler>();
Iterator<NamedIP> theList = bridgeSettings.getBridgeSettingsDescriptor().getDomoticzaddress().getDevices().iterator();
while(theList.hasNext()) {

View File

@@ -9,6 +9,7 @@ import org.slf4j.LoggerFactory;
import com.bwssystems.HABridge.NamedIP;
import com.bwssystems.HABridge.plugins.http.HTTPHandler;
import com.bwssystems.HABridge.plugins.http.HTTPHome;
import com.bwssystems.HABridge.util.TextStringFormatter;
import com.google.gson.Gson;
@@ -38,7 +39,7 @@ public class HalInfo {
public HalInfo(NamedIP addressName, String aGivenToken) {
super();
httpClient = new HTTPHandler();
httpClient = HTTPHome.getHandler();
halAddress = addressName;
if(halAddress.getPassword() == null || halAddress.getPassword().trim().isEmpty())
halAddress.setPassword(aGivenToken);

View File

@@ -5,6 +5,8 @@ import static java.lang.String.format;
import javax.inject.Inject;
import com.bwssystems.HABridge.plugins.http.HTTPHandler;
import com.bwssystems.HABridge.plugins.http.HTTPHome;
import org.apache.http.client.methods.HttpGet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -44,7 +46,7 @@ public class HarmonyServer {
dummyProvider = null;
myNameAndIP = theHarmonyAddress;
isDevMode = false;
httpClient = new HTTPHandler();
httpClient = HTTPHome.getHandler();
}
public static HarmonyServer setup(

View File

@@ -12,6 +12,7 @@ import org.slf4j.LoggerFactory;
import com.bwssystems.HABridge.NamedIP;
import com.bwssystems.HABridge.api.NameValue;
import com.bwssystems.HABridge.plugins.http.HTTPHandler;
import com.bwssystems.HABridge.plugins.http.HTTPHome;
import com.google.gson.Gson;
public class HomeAssistant {
@@ -21,7 +22,7 @@ public class HomeAssistant {
public HomeAssistant(NamedIP addressName) {
super();
anHttpHandler = new HTTPHandler();
anHttpHandler = HTTPHome.getHandler();
hassAddress = addressName;
}

View File

@@ -1,7 +1,5 @@
package com.bwssystems.HABridge.plugins.http;
import java.io.IOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -22,17 +20,21 @@ import com.google.gson.Gson;
public class HTTPHome implements Home {
private static final Logger log = LoggerFactory.getLogger(HTTPHome.class);
private HTTPHandler anHttpHandler;
protected HttpClientPool thePool;
private static HTTPHandler anHttpHandler = null;
private boolean closed;
public HTTPHome(BridgeSettings bridgeSettings) {
super();
closed = true;
thePool = new HttpClientPool();
createHome(bridgeSettings);
closed = false;
}
public static HTTPHandler getHandler() {
if(anHttpHandler == null)
anHttpHandler = new HTTPHandler();
return anHttpHandler;
}
@Override
public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int intensity,
@@ -95,7 +97,8 @@ public class HTTPHome implements Home {
@Override
public Home createHome(BridgeSettings bridgeSettings) {
anHttpHandler = new HTTPHandler();
if(anHttpHandler == null)
anHttpHandler = new HTTPHandler();
log.info("Http Home created.");
return this;
}
@@ -116,13 +119,6 @@ public class HTTPHome implements Home {
if(anHttpHandler != null)
anHttpHandler.closeHandler();
anHttpHandler = null;
try {
HttpClientPool.shutdown();
} catch (InterruptedException e) {
log.warn("Error shutting down http pool: " + e.getMessage());;
} catch (IOException e) {
log.warn("Error shutting down http pool: " + e.getMessage());;
}
closed = true;
}

View File

@@ -13,6 +13,7 @@ import com.bwssystems.HABridge.api.hue.DeviceResponse;
import com.bwssystems.HABridge.api.hue.HueApiResponse;
import com.bwssystems.HABridge.dao.DeviceDescriptor;
import com.bwssystems.HABridge.plugins.http.HTTPHandler;
import com.bwssystems.HABridge.plugins.http.HTTPHome;
import com.google.gson.Gson;
@@ -25,7 +26,7 @@ public class HueInfo {
public HueInfo(NamedIP addressName, HueHome theHome) {
super();
httpHandler = new HTTPHandler();
httpHandler = HTTPHome.getHandler();
hueAddress = addressName;
myHome = theHome;
}

View File

@@ -3,6 +3,7 @@ package com.bwssystems.HABridge.plugins.somfy;
import com.bwssystems.HABridge.NamedIP;
import com.bwssystems.HABridge.api.NameValue;
import com.bwssystems.HABridge.plugins.http.HTTPHandler;
import com.bwssystems.HABridge.plugins.http.HTTPHome;
import com.bwssystems.HABridge.plugins.somfy.jsonschema2pojo.getsetup.Device;
import com.bwssystems.HABridge.plugins.somfy.jsonschema2pojo.getsetup.GetSetup;
import com.google.gson.Gson;
@@ -39,7 +40,7 @@ public class SomfyInfo {
private void initHttpClient() throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException {
if(httpClient==null) {
httpClient = new HTTPHandler();
httpClient = HTTPHome.getHandler();
}
}

View File

@@ -9,6 +9,7 @@ import org.slf4j.LoggerFactory;
import com.bwssystems.HABridge.NamedIP;
import com.bwssystems.HABridge.plugins.http.HTTPHandler;
import com.bwssystems.HABridge.plugins.http.HTTPHome;
import com.bwssystems.HABridge.plugins.vera.luupRequests.Categorie;
import com.bwssystems.HABridge.plugins.vera.luupRequests.Device;
import com.bwssystems.HABridge.plugins.vera.luupRequests.Room;
@@ -25,7 +26,7 @@ public class VeraInfo {
public VeraInfo(NamedIP addressName) {
super();
httpClient = new HTTPHandler();
httpClient = HTTPHome.getHandler();
veraAddress = addressName;
}

View File

@@ -110,6 +110,7 @@ app.run(function ($rootScope, $location, Auth, bridgeService) {
$rootScope.$on('securityReinit', function(event, data) {
event.preventDefault();
Auth.logout();
bridgeService.state.testuser = "";
$location.path("/login");
});
@@ -216,7 +217,7 @@ app.service ('bridgeService', function ($rootScope, $http, $base64, $location, n
if (error.status === 401)
$rootScope.$broadcast('securityReinit', 'done');
else
self.displayError("Cannot renumber devices from habridge: ", error);
self.displayError("Cannot renumber devices from habridge: ", error);
}
);
};
@@ -249,7 +250,12 @@ app.service ('bridgeService', function ($rootScope, $http, $base64, $location, n
this.getTestUser = function () {
if(self.state.testuser === undefined || self.state.testuser === "") {
return $http.put(this.state.systemsbase + "/presslinkbutton").then(
var linkParams = {};
linkParams = {
seconds: 3,
silent: true
};
return $http.put(this.state.systemsbase + "/presslinkbutton", linkParams).then(
function (response) {
self.getAUser();
},
@@ -287,7 +293,7 @@ app.service ('bridgeService', function ($rootScope, $http, $base64, $location, n
if (error.status === 401)
$rootScope.$broadcast('securityReinit', 'done');
else
self.displayWarn("Cannot get security info: ", error);
self.displayWarn("Cannot get security info: ", error);
}
);
};
@@ -1202,7 +1208,7 @@ app.service ('bridgeService', function ($rootScope, $http, $base64, $location, n
msgDescription = "success " + angular.toJson(response.data);
}
if (typeof(response.data[0].error) !== 'undefined') {
if(reponse.data[0].error.indexOf("unauthorized") > -1) {
if(response.data[0].error.description.indexOf("unauthorized") > -1) {
self.displayWarn("Authorization error, please retry...", null);
}
else {