Added reinitialize and stop commands in prep for building the config

file handling.
This commit is contained in:
Admin
2016-02-09 16:41:05 -06:00
parent cca9a6be78
commit 2ff73e5672
7 changed files with 197 additions and 102 deletions

View File

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

View File

@@ -18,6 +18,9 @@ public class BridgeSettings {
private String nestuser;
private String nestpwd;
private boolean nestconfigured;
private String configfile;
private boolean restart;
private boolean stop;
public String getUpnpConfigAddress() {
return upnpconfigaddress;
@@ -109,6 +112,24 @@ public class BridgeSettings {
public void setButtonsleep(Integer buttonsleep) {
this.buttonsleep = buttonsleep;
}
public boolean isRestart() {
return restart;
}
public void setRestart(boolean restart) {
this.restart = restart;
}
public boolean isStop() {
return stop;
}
public void setStop(boolean stop) {
this.stop = stop;
}
public String getConfigfile() {
return configfile;
}
public void setConfigfile(String configfile) {
this.configfile = configfile;
}
public Boolean isValidVera() {
List<NamedIP> devicesList = this.veraaddress.getDevices();
if(devicesList.get(0).getIp().contains(Configuration.DEFAULT_ADDRESS))

View File

@@ -12,4 +12,7 @@ public class Configuration {
public final static String DEFAULT_PWD = "";
public final static String DFAULT_WEB_PORT = "8080";
public final static String DFAULT_BUTTON_SLEEP = "100";
public static final int UPNP_DISCOVERY_PORT = 1900;
public static final String UPNP_MULTICAST_ADDRESS = "239.255.255.250";
public static final String CONFIG_FILE = "data/habridge.config";
}

View File

@@ -51,14 +51,18 @@ public class HABridge {
theVersion = new Version();
log.info("HA Bridge (v" + theVersion.getVersion() + ") starting setup....");
log.info("HA Bridge (v" + theVersion.getVersion() + ") starting....");
bridgeSettings = new BridgeSettings();
bridgeSettings.setRestart(false);
bridgeSettings.setStop(false);
while(!bridgeSettings.isStop()) {
bridgeSettings.setConfigfile(System.getProperty("config.file", Configuration.CONFIG_FILE));
bridgeSettings.setServerPort(System.getProperty("server.port", Configuration.DFAULT_WEB_PORT));
bridgeSettings.setUpnpConfigAddress(System.getProperty("upnp.config.address", Configuration.DEFAULT_ADDRESS));
if(bridgeSettings.getUpnpConfigAddress().equalsIgnoreCase(Configuration.DEFAULT_ADDRESS)) {
try {
log.info("Getting an IP address for this host....");
log.info("HA Bridge (v" + theVersion.getVersion() + ") Getting an IP address for this host....");
Enumeration<NetworkInterface> ifs = NetworkInterface.getNetworkInterfaces();
while (ifs.hasMoreElements() && addressString == null) {
@@ -74,7 +78,7 @@ public class HABridge {
if(!name.equalsIgnoreCase(Configuration.LOOP_BACK_INTERFACE)|| !address.getHostAddress().equalsIgnoreCase(Configuration.LOOP_BACK_ADDRESS)) {
IPsPerNic++;
addressString = address.getHostAddress();
log.info("Adding " + addressString + " from interface " + name + " as our default upnp config address.");
log.info("HA Bridge (v" + theVersion.getVersion() + ") Adding " + addressString + " from interface " + name + " as our default upnp config address.");
}
}
}
@@ -124,6 +128,7 @@ public class HABridge {
bridgeSettings.setNestuser(System.getProperty("nest.user", Configuration.DEFAULT_USER));
bridgeSettings.setNestpwd(System.getProperty("nest.pwd", Configuration.DEFAULT_PWD));
log.info("HA Bridge (v" + theVersion.getVersion() + ") initializing....");
// sparkjava config directive to set ip address for the web server to listen on
// ipAddress("0.0.0.0"); // not used
// sparkjava config directive to set port for the web server to listen on
@@ -147,6 +152,13 @@ public class HABridge {
// start the upnp ssdp discovery listener
theUpnpListener = new UpnpListener(bridgeSettings);
theUpnpListener.startListening();
if(theUpnpListener.startListening())
log.info("HA Bridge (v" + theVersion.getVersion() + ") reinitialization requessted....");
bridgeSettings.setRestart(false);
stop();
}
log.info("HA Bridge (v" + theVersion.getVersion() + ") exiting....");
System.exit(0);
}
}

View File

@@ -23,9 +23,7 @@ import com.bwssystems.HABridge.dao.DeviceDescriptor;
import com.bwssystems.HABridge.dao.DeviceRepository;
import com.bwssystems.NestBridge.NestHome;
import com.bwssystems.harmony.HarmonyHome;
import com.bwssystems.luupRequests.Sdata;
import com.bwssystems.vera.VeraHome;
import com.bwssystems.vera.VeraInfo;
import com.google.gson.Gson;
/**

View File

@@ -1,6 +1,7 @@
package com.bwssystems.HABridge.hue;
import com.bwssystems.HABridge.BridgeSettings;
import com.bwssystems.HABridge.Configuration;
import com.bwssystems.HABridge.JsonTransformer;
import com.bwssystems.HABridge.api.UserCreateRequest;
import com.bwssystems.HABridge.api.hue.DeviceResponse;
@@ -45,6 +46,7 @@ import java.math.BigDecimal;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -439,6 +441,38 @@ public class HueMulator {
return responseString;
});
// http://ip_address:port/api/control/restart CORS request
options(HUE_CONTEXT + "/control/restart", "application/json", (request, response) -> {
response.status(HttpStatus.SC_OK);
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.header("Access-Control-Allow-Methods", "GET, POST, PUT");
response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
response.header("Content-Type", "text/html; charset=utf-8");
return "";
});
// http://ip_address:port/api//control/restart sets the parameter restart the server
put(HUE_CONTEXT + "/control/restart", "application/json", (request, response) -> {
bridgeSettings.setRestart(true);
pingListener();
return "{\"control\":\"restarting\"}";
});
// http://ip_address:port/api/control/stop CORS request
options(HUE_CONTEXT + "/control/stop", "application/json", (request, response) -> {
response.status(HttpStatus.SC_OK);
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.header("Access-Control-Allow-Methods", "GET, POST, PUT");
response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
response.header("Content-Type", "text/html; charset=utf-8");
return "";
});
// http://ip_address:port/api//control/stop sets the parameter stop the server
put(HUE_CONTEXT + "/control/stop", "application/json", (request, response) -> {
bridgeSettings.setStop(true);
pingListener();
return "{\"control\":\"stopping\"}";
});
}
/* light weight templating here, was going to use free marker but it was a bit too
@@ -510,4 +544,23 @@ public class HueMulator {
}
return false;
}
protected void pingListener() {
try {
byte[] buf = new byte[256];
String testData = "M-SEARCH * HTTP/1.1\nHOST: " + Configuration.UPNP_MULTICAST_ADDRESS + ":" + Configuration.UPNP_DISCOVERY_PORT + "ST: urn:schemas-upnp-org:device:CloudProxy:1\nMAN: \"ssdp:discover\"\nMX: 3";
buf = testData.getBytes();
MulticastSocket socket = new MulticastSocket(Configuration.UPNP_DISCOVERY_PORT);
InetAddress group = InetAddress.getByName(Configuration.UPNP_MULTICAST_ADDRESS);
DatagramPacket packet;
packet = new DatagramPacket(buf, buf.length, group, Configuration.UPNP_DISCOVERY_PORT);
socket.send(packet);
socket.close();
}
catch (IOException e) {
log.warn("Error pinging listener.", e);
}
}
}

View File

@@ -4,6 +4,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.bwssystems.HABridge.BridgeSettings;
import com.bwssystems.HABridge.Configuration;
import java.io.IOException;
import java.net.*;
@@ -14,8 +15,6 @@ import org.apache.http.conn.util.*;
public class UpnpListener {
private Logger log = LoggerFactory.getLogger(UpnpListener.class);
private static final int UPNP_DISCOVERY_PORT = 1900;
private static final String UPNP_MULTICAST_ADDRESS = "239.255.255.250";
private int upnpResponsePort;
@@ -26,6 +25,7 @@ public class UpnpListener {
private boolean strict;
private boolean traceupnp;
private BridgeSettings bridgeSettings;
public UpnpListener(BridgeSettings theSettings) {
super();
@@ -34,14 +34,15 @@ public class UpnpListener {
responseAddress = theSettings.getUpnpConfigAddress();
strict = theSettings.isUpnpStrict();
traceupnp = theSettings.isTraceupnp();
bridgeSettings = theSettings;
}
public void startListening(){
public boolean startListening(){
log.info("UPNP Discovery Listener starting....");
try (DatagramSocket responseSocket = new DatagramSocket(upnpResponsePort);
MulticastSocket upnpMulticastSocket = new MulticastSocket(UPNP_DISCOVERY_PORT);) {
InetSocketAddress socketAddress = new InetSocketAddress(UPNP_MULTICAST_ADDRESS, UPNP_DISCOVERY_PORT);
MulticastSocket upnpMulticastSocket = new MulticastSocket(Configuration.UPNP_DISCOVERY_PORT);) {
InetSocketAddress socketAddress = new InetSocketAddress(Configuration.UPNP_MULTICAST_ADDRESS, Configuration.UPNP_DISCOVERY_PORT);
Enumeration<NetworkInterface> ifs = NetworkInterface.getNetworkInterfaces();
while (ifs.hasMoreElements()) {
@@ -71,22 +72,29 @@ public class UpnpListener {
}
log.info("UPNP Discovery Listener running and ready....");
while(true){ //trigger shutdown here
boolean loopControl = true;
while(loopControl){ //trigger shutdown here
byte[] buf = new byte[1024];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
upnpMulticastSocket.receive(packet);
if(isSSDPDiscovery(packet)){
sendUpnpResponse(responseSocket, packet.getAddress(), packet.getPort());
}
if(bridgeSettings.isRestart() || bridgeSettings.isStop())
loopControl = false;
}
upnpMulticastSocket.close();
responseSocket.close();
} catch (IOException e) {
log.error("UpnpListener encountered an error opening sockets. Shutting down", e);
}
log.info("UPNP Discovery Listener Stopped");
if(bridgeSettings.isRestart())
log.info("UPNP Discovery Listener - ended, restart found");
if(bridgeSettings.isStop())
log.info("UPNP Discovery Listener - ended, stop found");
if(!bridgeSettings.isStop()&& !bridgeSettings.isRestart())
log.info("UPNP Discovery Listener - ended, error found");
return bridgeSettings.isRestart();
}
/**