mirror of
https://github.com/bwssytems/ha-bridge.git
synced 2025-12-18 00:10:20 +00:00
Added reinitialize and stop commands in prep for building the config
file handling.
This commit is contained in:
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>1.3.8</version>
|
<version>1.3.8a</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<name>HA Bridge</name>
|
<name>HA Bridge</name>
|
||||||
|
|||||||
@@ -18,6 +18,9 @@ public class BridgeSettings {
|
|||||||
private String nestuser;
|
private String nestuser;
|
||||||
private String nestpwd;
|
private String nestpwd;
|
||||||
private boolean nestconfigured;
|
private boolean nestconfigured;
|
||||||
|
private String configfile;
|
||||||
|
private boolean restart;
|
||||||
|
private boolean stop;
|
||||||
|
|
||||||
public String getUpnpConfigAddress() {
|
public String getUpnpConfigAddress() {
|
||||||
return upnpconfigaddress;
|
return upnpconfigaddress;
|
||||||
@@ -109,6 +112,24 @@ public class BridgeSettings {
|
|||||||
public void setButtonsleep(Integer buttonsleep) {
|
public void setButtonsleep(Integer buttonsleep) {
|
||||||
this.buttonsleep = 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() {
|
public Boolean isValidVera() {
|
||||||
List<NamedIP> devicesList = this.veraaddress.getDevices();
|
List<NamedIP> devicesList = this.veraaddress.getDevices();
|
||||||
if(devicesList.get(0).getIp().contains(Configuration.DEFAULT_ADDRESS))
|
if(devicesList.get(0).getIp().contains(Configuration.DEFAULT_ADDRESS))
|
||||||
|
|||||||
@@ -12,4 +12,7 @@ public class Configuration {
|
|||||||
public final static String DEFAULT_PWD = "";
|
public final static String DEFAULT_PWD = "";
|
||||||
public final static String DFAULT_WEB_PORT = "8080";
|
public final static String DFAULT_WEB_PORT = "8080";
|
||||||
public final static String DFAULT_BUTTON_SLEEP = "100";
|
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";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -51,102 +51,114 @@ public class HABridge {
|
|||||||
|
|
||||||
theVersion = new Version();
|
theVersion = new Version();
|
||||||
|
|
||||||
log.info("HA Bridge (v" + theVersion.getVersion() + ") starting setup....");
|
log.info("HA Bridge (v" + theVersion.getVersion() + ") starting....");
|
||||||
|
|
||||||
bridgeSettings = new BridgeSettings();
|
bridgeSettings = new BridgeSettings();
|
||||||
bridgeSettings.setServerPort(System.getProperty("server.port", Configuration.DFAULT_WEB_PORT));
|
bridgeSettings.setRestart(false);
|
||||||
bridgeSettings.setUpnpConfigAddress(System.getProperty("upnp.config.address", Configuration.DEFAULT_ADDRESS));
|
bridgeSettings.setStop(false);
|
||||||
if(bridgeSettings.getUpnpConfigAddress().equalsIgnoreCase(Configuration.DEFAULT_ADDRESS)) {
|
while(!bridgeSettings.isStop()) {
|
||||||
try {
|
bridgeSettings.setConfigfile(System.getProperty("config.file", Configuration.CONFIG_FILE));
|
||||||
log.info("Getting an IP address for this host....");
|
bridgeSettings.setServerPort(System.getProperty("server.port", Configuration.DFAULT_WEB_PORT));
|
||||||
Enumeration<NetworkInterface> ifs = NetworkInterface.getNetworkInterfaces();
|
bridgeSettings.setUpnpConfigAddress(System.getProperty("upnp.config.address", Configuration.DEFAULT_ADDRESS));
|
||||||
|
if(bridgeSettings.getUpnpConfigAddress().equalsIgnoreCase(Configuration.DEFAULT_ADDRESS)) {
|
||||||
while (ifs.hasMoreElements() && addressString == null) {
|
try {
|
||||||
NetworkInterface xface = ifs.nextElement();
|
log.info("HA Bridge (v" + theVersion.getVersion() + ") Getting an IP address for this host....");
|
||||||
Enumeration<InetAddress> addrs = xface.getInetAddresses();
|
Enumeration<NetworkInterface> ifs = NetworkInterface.getNetworkInterfaces();
|
||||||
String name = xface.getName();
|
|
||||||
int IPsPerNic = 0;
|
while (ifs.hasMoreElements() && addressString == null) {
|
||||||
|
NetworkInterface xface = ifs.nextElement();
|
||||||
while (addrs.hasMoreElements() && IPsPerNic == 0) {
|
Enumeration<InetAddress> addrs = xface.getInetAddresses();
|
||||||
address = addrs.nextElement();
|
String name = xface.getName();
|
||||||
if (InetAddressUtils.isIPv4Address(address.getHostAddress())) {
|
int IPsPerNic = 0;
|
||||||
log.debug(name + " ... has IPV4 addr " + address);
|
|
||||||
if(!name.equalsIgnoreCase(Configuration.LOOP_BACK_INTERFACE)|| !address.getHostAddress().equalsIgnoreCase(Configuration.LOOP_BACK_ADDRESS)) {
|
while (addrs.hasMoreElements() && IPsPerNic == 0) {
|
||||||
IPsPerNic++;
|
address = addrs.nextElement();
|
||||||
addressString = address.getHostAddress();
|
if (InetAddressUtils.isIPv4Address(address.getHostAddress())) {
|
||||||
log.info("Adding " + addressString + " from interface " + name + " as our default upnp config address.");
|
log.debug(name + " ... has IPV4 addr " + address);
|
||||||
|
if(!name.equalsIgnoreCase(Configuration.LOOP_BACK_INTERFACE)|| !address.getHostAddress().equalsIgnoreCase(Configuration.LOOP_BACK_ADDRESS)) {
|
||||||
|
IPsPerNic++;
|
||||||
|
addressString = address.getHostAddress();
|
||||||
|
log.info("HA Bridge (v" + theVersion.getVersion() + ") Adding " + addressString + " from interface " + name + " as our default upnp config address.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} catch (SocketException e) {
|
||||||
|
log.error("Cannot get ip address of this host, Exiting with message: " + e.getMessage(), e);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
} catch (SocketException e) {
|
|
||||||
log.error("Cannot get ip address of this host, Exiting with message: " + e.getMessage(), e);
|
bridgeSettings.setUpnpConfigAddress(addressString);
|
||||||
return;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
bridgeSettings.setUpnpConfigAddress(addressString);
|
bridgeSettings.setUpnpDeviceDb(System.getProperty("upnp.device.db", Configuration.DEVICE_DB_DIRECTORY));
|
||||||
}
|
bridgeSettings.setUpnpResponsePort(System.getProperty("upnp.response.port", Configuration.UPNP_RESPONSE_PORT));
|
||||||
|
IpList theVeraList;
|
||||||
|
|
||||||
|
try {
|
||||||
|
theVeraList = new Gson().fromJson(System.getProperty("vera.address", Configuration.DEFAULT_HARMONY_ADDRESS_LIST), IpList.class);
|
||||||
|
} catch (Exception e) {
|
||||||
|
try {
|
||||||
|
theVeraList = new Gson().fromJson("{devices:[{name:default,ip:" + System.getProperty("vera.address", Configuration.DEFAULT_ADDRESS) + "}]}", IpList.class);
|
||||||
|
} catch (Exception et) {
|
||||||
|
log.error("Cannot parse vera.address, Exiting with message: " + e.getMessage(), e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
bridgeSettings.setVeraAddress(theVeraList);
|
||||||
|
IpList theHarmonyList;
|
||||||
|
|
||||||
|
try {
|
||||||
|
theHarmonyList = new Gson().fromJson(System.getProperty("harmony.address", Configuration.DEFAULT_HARMONY_ADDRESS_LIST), IpList.class);
|
||||||
|
} catch (Exception e) {
|
||||||
|
try {
|
||||||
|
theHarmonyList = new Gson().fromJson("{devices:[{name:default,ip:" + System.getProperty("harmony.address", Configuration.DEFAULT_ADDRESS) + "}]}", IpList.class);
|
||||||
|
} catch (Exception et) {
|
||||||
|
log.error("Cannot parse harmony.address, Exiting with message: " + e.getMessage(), e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
bridgeSettings.setHarmonyAddress(theHarmonyList);
|
||||||
|
bridgeSettings.setHarmonyUser(System.getProperty("harmony.user", Configuration.DEFAULT_USER));
|
||||||
|
bridgeSettings.setHarmonyPwd(System.getProperty("harmony.pwd", Configuration.DEFAULT_PWD));
|
||||||
|
bridgeSettings.setUpnpStrict(Boolean.parseBoolean(System.getProperty("upnp.strict", "true")));
|
||||||
|
bridgeSettings.setTraceupnp(Boolean.parseBoolean(System.getProperty("trace.upnp", "false")));
|
||||||
|
bridgeSettings.setDevMode(Boolean.parseBoolean(System.getProperty("dev.mode", "false")));
|
||||||
|
bridgeSettings.setButtonsleep(Integer.parseInt(System.getProperty("button.sleep", Configuration.DFAULT_BUTTON_SLEEP)));
|
||||||
|
bridgeSettings.setNestuser(System.getProperty("nest.user", Configuration.DEFAULT_USER));
|
||||||
|
bridgeSettings.setNestpwd(System.getProperty("nest.pwd", Configuration.DEFAULT_PWD));
|
||||||
|
|
||||||
bridgeSettings.setUpnpDeviceDb(System.getProperty("upnp.device.db", Configuration.DEVICE_DB_DIRECTORY));
|
log.info("HA Bridge (v" + theVersion.getVersion() + ") initializing....");
|
||||||
bridgeSettings.setUpnpResponsePort(System.getProperty("upnp.response.port", Configuration.UPNP_RESPONSE_PORT));
|
// sparkjava config directive to set ip address for the web server to listen on
|
||||||
IpList theVeraList;
|
// ipAddress("0.0.0.0"); // not used
|
||||||
|
// sparkjava config directive to set port for the web server to listen on
|
||||||
|
port(Integer.valueOf(bridgeSettings.getServerPort()));
|
||||||
|
// sparkjava config directive to set html static file location for Jetty
|
||||||
|
staticFileLocation("/public");
|
||||||
|
//setup the harmony connection if available
|
||||||
|
harmonyHome = new HarmonyHome(bridgeSettings);
|
||||||
|
//setup the nest connection if available
|
||||||
|
nestHome = new NestHome(bridgeSettings);
|
||||||
|
// setup the class to handle the resource setup rest api
|
||||||
|
theResources = new DeviceResource(bridgeSettings, theVersion, harmonyHome, nestHome);
|
||||||
|
// setup the class to handle the hue emulator rest api
|
||||||
|
theHueMulator = new HueMulator(bridgeSettings, theResources.getDeviceRepository(), harmonyHome, nestHome);
|
||||||
|
theHueMulator.setupServer();
|
||||||
|
// setup the class to handle the upnp response rest api
|
||||||
|
theSettingResponder = new UpnpSettingsResource(bridgeSettings);
|
||||||
|
theSettingResponder.setupServer();
|
||||||
|
// wait for the sparkjava initialization of the rest api classes to be complete
|
||||||
|
awaitInitialization();
|
||||||
|
|
||||||
|
// start the upnp ssdp discovery listener
|
||||||
|
theUpnpListener = new UpnpListener(bridgeSettings);
|
||||||
|
if(theUpnpListener.startListening())
|
||||||
|
log.info("HA Bridge (v" + theVersion.getVersion() + ") reinitialization requessted....");
|
||||||
|
|
||||||
try {
|
bridgeSettings.setRestart(false);
|
||||||
theVeraList = new Gson().fromJson(System.getProperty("vera.address", Configuration.DEFAULT_HARMONY_ADDRESS_LIST), IpList.class);
|
stop();
|
||||||
} catch (Exception e) {
|
|
||||||
try {
|
|
||||||
theVeraList = new Gson().fromJson("{devices:[{name:default,ip:" + System.getProperty("vera.address", Configuration.DEFAULT_ADDRESS) + "}]}", IpList.class);
|
|
||||||
} catch (Exception et) {
|
|
||||||
log.error("Cannot parse vera.address, Exiting with message: " + e.getMessage(), e);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
bridgeSettings.setVeraAddress(theVeraList);
|
log.info("HA Bridge (v" + theVersion.getVersion() + ") exiting....");
|
||||||
IpList theHarmonyList;
|
System.exit(0);
|
||||||
|
|
||||||
try {
|
|
||||||
theHarmonyList = new Gson().fromJson(System.getProperty("harmony.address", Configuration.DEFAULT_HARMONY_ADDRESS_LIST), IpList.class);
|
|
||||||
} catch (Exception e) {
|
|
||||||
try {
|
|
||||||
theHarmonyList = new Gson().fromJson("{devices:[{name:default,ip:" + System.getProperty("harmony.address", Configuration.DEFAULT_ADDRESS) + "}]}", IpList.class);
|
|
||||||
} catch (Exception et) {
|
|
||||||
log.error("Cannot parse harmony.address, Exiting with message: " + e.getMessage(), e);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
bridgeSettings.setHarmonyAddress(theHarmonyList);
|
|
||||||
bridgeSettings.setHarmonyUser(System.getProperty("harmony.user", Configuration.DEFAULT_USER));
|
|
||||||
bridgeSettings.setHarmonyPwd(System.getProperty("harmony.pwd", Configuration.DEFAULT_PWD));
|
|
||||||
bridgeSettings.setUpnpStrict(Boolean.parseBoolean(System.getProperty("upnp.strict", "true")));
|
|
||||||
bridgeSettings.setTraceupnp(Boolean.parseBoolean(System.getProperty("trace.upnp", "false")));
|
|
||||||
bridgeSettings.setDevMode(Boolean.parseBoolean(System.getProperty("dev.mode", "false")));
|
|
||||||
bridgeSettings.setButtonsleep(Integer.parseInt(System.getProperty("button.sleep", Configuration.DFAULT_BUTTON_SLEEP)));
|
|
||||||
bridgeSettings.setNestuser(System.getProperty("nest.user", Configuration.DEFAULT_USER));
|
|
||||||
bridgeSettings.setNestpwd(System.getProperty("nest.pwd", Configuration.DEFAULT_PWD));
|
|
||||||
|
|
||||||
// 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
|
|
||||||
port(Integer.valueOf(bridgeSettings.getServerPort()));
|
|
||||||
// sparkjava config directive to set html static file location for Jetty
|
|
||||||
staticFileLocation("/public");
|
|
||||||
//setup the harmony connection if available
|
|
||||||
harmonyHome = new HarmonyHome(bridgeSettings);
|
|
||||||
//setup the nest connection if available
|
|
||||||
nestHome = new NestHome(bridgeSettings);
|
|
||||||
// setup the class to handle the resource setup rest api
|
|
||||||
theResources = new DeviceResource(bridgeSettings, theVersion, harmonyHome, nestHome);
|
|
||||||
// setup the class to handle the hue emulator rest api
|
|
||||||
theHueMulator = new HueMulator(bridgeSettings, theResources.getDeviceRepository(), harmonyHome, nestHome);
|
|
||||||
theHueMulator.setupServer();
|
|
||||||
// setup the class to handle the upnp response rest api
|
|
||||||
theSettingResponder = new UpnpSettingsResource(bridgeSettings);
|
|
||||||
theSettingResponder.setupServer();
|
|
||||||
// wait for the sparkjava initialization of the rest api classes to be complete
|
|
||||||
awaitInitialization();
|
|
||||||
|
|
||||||
// start the upnp ssdp discovery listener
|
|
||||||
theUpnpListener = new UpnpListener(bridgeSettings);
|
|
||||||
theUpnpListener.startListening();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,9 +23,7 @@ import com.bwssystems.HABridge.dao.DeviceDescriptor;
|
|||||||
import com.bwssystems.HABridge.dao.DeviceRepository;
|
import com.bwssystems.HABridge.dao.DeviceRepository;
|
||||||
import com.bwssystems.NestBridge.NestHome;
|
import com.bwssystems.NestBridge.NestHome;
|
||||||
import com.bwssystems.harmony.HarmonyHome;
|
import com.bwssystems.harmony.HarmonyHome;
|
||||||
import com.bwssystems.luupRequests.Sdata;
|
|
||||||
import com.bwssystems.vera.VeraHome;
|
import com.bwssystems.vera.VeraHome;
|
||||||
import com.bwssystems.vera.VeraInfo;
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.bwssystems.HABridge.hue;
|
package com.bwssystems.HABridge.hue;
|
||||||
|
|
||||||
import com.bwssystems.HABridge.BridgeSettings;
|
import com.bwssystems.HABridge.BridgeSettings;
|
||||||
|
import com.bwssystems.HABridge.Configuration;
|
||||||
import com.bwssystems.HABridge.JsonTransformer;
|
import com.bwssystems.HABridge.JsonTransformer;
|
||||||
import com.bwssystems.HABridge.api.UserCreateRequest;
|
import com.bwssystems.HABridge.api.UserCreateRequest;
|
||||||
import com.bwssystems.HABridge.api.hue.DeviceResponse;
|
import com.bwssystems.HABridge.api.hue.DeviceResponse;
|
||||||
@@ -45,6 +46,7 @@ import java.math.BigDecimal;
|
|||||||
import java.net.DatagramPacket;
|
import java.net.DatagramPacket;
|
||||||
import java.net.DatagramSocket;
|
import java.net.DatagramSocket;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
|
import java.net.MulticastSocket;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@@ -439,6 +441,38 @@ public class HueMulator {
|
|||||||
|
|
||||||
return responseString;
|
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
|
/* 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;
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import org.slf4j.Logger;
|
|||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import com.bwssystems.HABridge.BridgeSettings;
|
import com.bwssystems.HABridge.BridgeSettings;
|
||||||
|
import com.bwssystems.HABridge.Configuration;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
@@ -14,8 +15,6 @@ import org.apache.http.conn.util.*;
|
|||||||
|
|
||||||
public class UpnpListener {
|
public class UpnpListener {
|
||||||
private Logger log = LoggerFactory.getLogger(UpnpListener.class);
|
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;
|
private int upnpResponsePort;
|
||||||
|
|
||||||
@@ -26,6 +25,7 @@ public class UpnpListener {
|
|||||||
private boolean strict;
|
private boolean strict;
|
||||||
|
|
||||||
private boolean traceupnp;
|
private boolean traceupnp;
|
||||||
|
private BridgeSettings bridgeSettings;
|
||||||
|
|
||||||
public UpnpListener(BridgeSettings theSettings) {
|
public UpnpListener(BridgeSettings theSettings) {
|
||||||
super();
|
super();
|
||||||
@@ -34,14 +34,15 @@ public class UpnpListener {
|
|||||||
responseAddress = theSettings.getUpnpConfigAddress();
|
responseAddress = theSettings.getUpnpConfigAddress();
|
||||||
strict = theSettings.isUpnpStrict();
|
strict = theSettings.isUpnpStrict();
|
||||||
traceupnp = theSettings.isTraceupnp();
|
traceupnp = theSettings.isTraceupnp();
|
||||||
|
bridgeSettings = theSettings;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void startListening(){
|
public boolean startListening(){
|
||||||
log.info("UPNP Discovery Listener starting....");
|
log.info("UPNP Discovery Listener starting....");
|
||||||
|
|
||||||
try (DatagramSocket responseSocket = new DatagramSocket(upnpResponsePort);
|
try (DatagramSocket responseSocket = new DatagramSocket(upnpResponsePort);
|
||||||
MulticastSocket upnpMulticastSocket = new MulticastSocket(UPNP_DISCOVERY_PORT);) {
|
MulticastSocket upnpMulticastSocket = new MulticastSocket(Configuration.UPNP_DISCOVERY_PORT);) {
|
||||||
InetSocketAddress socketAddress = new InetSocketAddress(UPNP_MULTICAST_ADDRESS, UPNP_DISCOVERY_PORT);
|
InetSocketAddress socketAddress = new InetSocketAddress(Configuration.UPNP_MULTICAST_ADDRESS, Configuration.UPNP_DISCOVERY_PORT);
|
||||||
Enumeration<NetworkInterface> ifs = NetworkInterface.getNetworkInterfaces();
|
Enumeration<NetworkInterface> ifs = NetworkInterface.getNetworkInterfaces();
|
||||||
|
|
||||||
while (ifs.hasMoreElements()) {
|
while (ifs.hasMoreElements()) {
|
||||||
@@ -71,22 +72,29 @@ public class UpnpListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
log.info("UPNP Discovery Listener running and ready....");
|
log.info("UPNP Discovery Listener running and ready....");
|
||||||
|
boolean loopControl = true;
|
||||||
while(true){ //trigger shutdown here
|
while(loopControl){ //trigger shutdown here
|
||||||
byte[] buf = new byte[1024];
|
byte[] buf = new byte[1024];
|
||||||
DatagramPacket packet = new DatagramPacket(buf, buf.length);
|
DatagramPacket packet = new DatagramPacket(buf, buf.length);
|
||||||
upnpMulticastSocket.receive(packet);
|
upnpMulticastSocket.receive(packet);
|
||||||
if(isSSDPDiscovery(packet)){
|
if(isSSDPDiscovery(packet)){
|
||||||
sendUpnpResponse(responseSocket, packet.getAddress(), packet.getPort());
|
sendUpnpResponse(responseSocket, packet.getAddress(), packet.getPort());
|
||||||
}
|
}
|
||||||
|
if(bridgeSettings.isRestart() || bridgeSettings.isStop())
|
||||||
|
loopControl = false;
|
||||||
}
|
}
|
||||||
|
upnpMulticastSocket.close();
|
||||||
|
responseSocket.close();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
log.error("UpnpListener encountered an error opening sockets. Shutting down", 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();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user