mirror of
https://github.com/bwssytems/ha-bridge.git
synced 2025-12-19 08:28:46 +00:00
Updated Hue description response emulation. Updated the management of
devices in the map. Updated logging information to be less in normal mode.
This commit is contained in:
@@ -75,7 +75,6 @@ public class HABridge {
|
|||||||
|
|
||||||
// start the upnp ssdp discovery listener
|
// start the upnp ssdp discovery listener
|
||||||
theUpnpListener = new UpnpListener(bridgeSettings);
|
theUpnpListener = new UpnpListener(bridgeSettings);
|
||||||
log.info("Done setup, application to run....");
|
|
||||||
theUpnpListener.startListening();
|
theUpnpListener.startListening();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ public class DeviceRepository {
|
|||||||
DeviceDescriptor theDevice = null;
|
DeviceDescriptor theDevice = null;
|
||||||
while (theIterator.hasNext()) {
|
while (theIterator.hasNext()) {
|
||||||
theDevice = theIterator.next();
|
theDevice = theIterator.next();
|
||||||
put(Integer.parseInt(theDevice.getId()), theDevice);
|
put(theDevice.getId(), theDevice);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -62,17 +62,16 @@ public class DeviceRepository {
|
|||||||
return devices.get(id);
|
return devices.get(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void put(int id, DeviceDescriptor aDescriptor) {
|
private void put(String id, DeviceDescriptor aDescriptor) {
|
||||||
devices.put(String.valueOf(id), aDescriptor);
|
devices.put(id, aDescriptor);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void save(DeviceDescriptor aDescriptor) {
|
public void save(DeviceDescriptor aDescriptor) {
|
||||||
int id = random.nextInt(Integer.MAX_VALUE);
|
|
||||||
if(aDescriptor.getId() != null)
|
if(aDescriptor.getId() != null)
|
||||||
devices.remove(aDescriptor.getId());
|
devices.remove(aDescriptor.getId());
|
||||||
else
|
else
|
||||||
aDescriptor.setId(String.valueOf(id));
|
aDescriptor.setId(String.valueOf(random.nextInt(Integer.MAX_VALUE)));
|
||||||
put(id, aDescriptor);
|
put(aDescriptor.getId(), aDescriptor);
|
||||||
JsonTransformer aRenderer = new JsonTransformer();
|
JsonTransformer aRenderer = new JsonTransformer();
|
||||||
String jsonValue = aRenderer.render(findAll());
|
String jsonValue = aRenderer.render(findAll());
|
||||||
repositoryWriter(jsonValue, repositoryPath);
|
repositoryWriter(jsonValue, repositoryPath);
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ public class DeviceResource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void setupEndpoints() {
|
private void setupEndpoints() {
|
||||||
log.debug("Setting up endpoints");
|
log.info("HABridge device management service started.... ");
|
||||||
post(API_CONTEXT, "application/json", (request, response) -> {
|
post(API_CONTEXT, "application/json", (request, response) -> {
|
||||||
log.debug("Create a Device - request body: " + request.body());
|
log.debug("Create a Device - request body: " + request.body());
|
||||||
DeviceDescriptor device = new Gson().fromJson(request.body(), DeviceDescriptor.class);
|
DeviceDescriptor device = new Gson().fromJson(request.body(), DeviceDescriptor.class);
|
||||||
|
|||||||
@@ -53,10 +53,11 @@ public class HueMulator {
|
|||||||
|
|
||||||
// This function sets up the sparkjava rest calls for the hue api
|
// This function sets up the sparkjava rest calls for the hue api
|
||||||
private void setupEndpoints() {
|
private void setupEndpoints() {
|
||||||
|
log.info("Hue emulator service started....");
|
||||||
// http://ip_address:port/api/{userId}/lights returns json objects of all lights configured
|
// http://ip_address:port/api/{userId}/lights returns json objects of all lights configured
|
||||||
get(HUE_CONTEXT + "/:userid/lights", "application/json", (request, response) -> {
|
get(HUE_CONTEXT + "/:userid/lights", "application/json", (request, response) -> {
|
||||||
String userId = request.params(":userid");
|
String userId = request.params(":userid");
|
||||||
log.info("hue lights list requested: " + userId + " from " + request.ip());
|
log.debug("hue lights list requested: " + userId + " from " + request.ip());
|
||||||
List<DeviceDescriptor> deviceList = repository.findAll();
|
List<DeviceDescriptor> deviceList = repository.findAll();
|
||||||
Map<String, DeviceResponse> deviceResponseMap = new HashMap<>();
|
Map<String, DeviceResponse> deviceResponseMap = new HashMap<>();
|
||||||
for (DeviceDescriptor device : deviceList) {
|
for (DeviceDescriptor device : deviceList) {
|
||||||
@@ -83,7 +84,7 @@ public class HueMulator {
|
|||||||
// http://ip_address:port/api/{userId} returns json objects for the full state
|
// http://ip_address:port/api/{userId} returns json objects for the full state
|
||||||
get(HUE_CONTEXT + "/:userid", "application/json", (request, response) -> {
|
get(HUE_CONTEXT + "/:userid", "application/json", (request, response) -> {
|
||||||
String userId = request.params(":userid");
|
String userId = request.params(":userid");
|
||||||
log.info("hue api full state requested: " + userId + " from " + request.ip());
|
log.debug("hue api full state requested: " + userId + " from " + request.ip());
|
||||||
List<DeviceDescriptor> descriptorList = repository.findAll();
|
List<DeviceDescriptor> descriptorList = repository.findAll();
|
||||||
if (descriptorList == null) {
|
if (descriptorList == null) {
|
||||||
response.status(404);
|
response.status(404);
|
||||||
@@ -107,13 +108,13 @@ public class HueMulator {
|
|||||||
get(HUE_CONTEXT + "/:userid/lights/:id", "application/json", (request, response) -> {
|
get(HUE_CONTEXT + "/:userid/lights/:id", "application/json", (request, response) -> {
|
||||||
String userId = request.params(":userid");
|
String userId = request.params(":userid");
|
||||||
String lightId = request.params(":id");
|
String lightId = request.params(":id");
|
||||||
log.info("hue light requested: " + lightId + "for user: " + userId + " from " + request.ip());
|
log.debug("hue light requested: " + lightId + " for user: " + userId + " from " + request.ip());
|
||||||
DeviceDescriptor device = repository.findOne(lightId);
|
DeviceDescriptor device = repository.findOne(lightId);
|
||||||
if (device == null) {
|
if (device == null) {
|
||||||
response.status(404);
|
response.status(404);
|
||||||
return null;
|
return null;
|
||||||
} else {
|
} else {
|
||||||
log.info("found device named: " + device.getName());
|
log.debug("found device named: " + device.getName());
|
||||||
}
|
}
|
||||||
DeviceResponse lightResponse = DeviceResponse.createResponse(device.getName(), device.getId());
|
DeviceResponse lightResponse = DeviceResponse.createResponse(device.getName(), device.getId());
|
||||||
|
|
||||||
@@ -129,7 +130,7 @@ public class HueMulator {
|
|||||||
*/
|
*/
|
||||||
String userId = request.params(":userid");
|
String userId = request.params(":userid");
|
||||||
String lightId = request.params(":id");
|
String lightId = request.params(":id");
|
||||||
log.info("hue state change requested: " + userId + " from " + request.ip() + " body: " + request.body());
|
log.debug("hue state change requested: " + userId + " from " + request.ip() + " body: " + request.body());
|
||||||
|
|
||||||
DeviceState state = null;
|
DeviceState state = null;
|
||||||
try {
|
try {
|
||||||
@@ -143,7 +144,7 @@ public class HueMulator {
|
|||||||
DeviceDescriptor device = repository.findOne(lightId);
|
DeviceDescriptor device = repository.findOne(lightId);
|
||||||
if (device == null) {
|
if (device == null) {
|
||||||
response.status(404);
|
response.status(404);
|
||||||
log.error("Could not find devcie: " + lightId);
|
log.error("Could not find devcie: " + lightId + " for hue state change request: " + userId + " from " + request.ip() + " body: " + request.body());
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -189,12 +190,12 @@ public class HueMulator {
|
|||||||
|
|
||||||
// This function executes the url from the device repository against the vera
|
// This function executes the url from the device repository against the vera
|
||||||
protected boolean doHttpGETRequest(String url) {
|
protected boolean doHttpGETRequest(String url) {
|
||||||
log.info("calling GET on URL: " + url);
|
log.debug("calling GET on URL: " + url);
|
||||||
HttpGet httpGet = new HttpGet(url);
|
HttpGet httpGet = new HttpGet(url);
|
||||||
try {
|
try {
|
||||||
HttpResponse response = httpClient.execute(httpGet);
|
HttpResponse response = httpClient.execute(httpGet);
|
||||||
EntityUtils.consume(response.getEntity()); //close out inputstream ignore content
|
EntityUtils.consume(response.getEntity()); //close out inputstream ignore content
|
||||||
log.info("GET on URL responded: " + response.getStatusLine().getStatusCode());
|
log.debug("GET on URL responded: " + response.getStatusLine().getStatusCode());
|
||||||
if(response.getStatusLine().getStatusCode() == 200){
|
if(response.getStatusLine().getStatusCode() == 200){
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ public class UpnpListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void startListening(){
|
public void startListening(){
|
||||||
log.info("Starting UPNP Discovery Listener");
|
log.info("UPNP Discovery Listener started....");
|
||||||
|
|
||||||
try (DatagramSocket responseSocket = new DatagramSocket(upnpResponsePort);
|
try (DatagramSocket responseSocket = new DatagramSocket(upnpResponsePort);
|
||||||
MulticastSocket upnpMulticastSocket = new MulticastSocket(UPNP_DISCOVERY_PORT);) {
|
MulticastSocket upnpMulticastSocket = new MulticastSocket(UPNP_DISCOVERY_PORT);) {
|
||||||
|
|||||||
@@ -44,9 +44,10 @@ public class UpnpSettingsResource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void setupListener (BridgeSettings theSettings) {
|
private void setupListener (BridgeSettings theSettings) {
|
||||||
// http://ip_address:port/upnp/:id/description.xml which returns the xml configuration for the location of the hue emulator
|
log.info("Hue description service started....");
|
||||||
|
// http://ip_adress:port/description.xml which returns the xml configuration for the hue emulator
|
||||||
get("/description.xml", "application/xml", (request, response) -> {
|
get("/description.xml", "application/xml", (request, response) -> {
|
||||||
log.info("upnp device settings requested: " + request.params(":id") + " from " + request.ip());
|
log.debug("upnp device settings requested: " + request.params(":id") + " from " + request.ip());
|
||||||
String portNumber = Integer.toString(request.port());
|
String portNumber = Integer.toString(request.port());
|
||||||
String filledTemplate = String.format(hueTemplate, theSettings.getUpnpConfigAddress(), portNumber, theSettings.getUpnpConfigAddress());
|
String filledTemplate = String.format(hueTemplate, theSettings.getUpnpConfigAddress(), portNumber, theSettings.getUpnpConfigAddress());
|
||||||
log.debug("upnp device settings response: " + filledTemplate);
|
log.debug("upnp device settings response: " + filledTemplate);
|
||||||
|
|||||||
Reference in New Issue
Block a user