Update application from amazon-echo-bridge-compact to ha-bridge.

Try to remove most code naming away from amazon.

Fix package names being incorrect.

Added Vera query functionality, needs some fine tuning.
This commit is contained in:
Admin
2015-08-12 16:44:48 -05:00
parent 3911f802ee
commit f8bebe38c0
17 changed files with 398 additions and 164 deletions

View File

@@ -1,4 +1,4 @@
package com.bwssytems.HABridge;
package com.bwssystems.HABridge;
import static spark.Spark.*;
@@ -8,12 +8,13 @@ import java.net.UnknownHostException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.bwssytems.HABridge.devicemanagmeent.*;
import com.bwssytems.HABridge.hue.HueMulator;
import com.bwssytems.HABridge.upnp.UpnpListener;
import com.bwssytems.HABridge.upnp.UpnpSettingsResource;
import com.bwssystems.HABridge.devicemanagmeent.*;
import com.bwssystems.HABridge.hue.HueMulator;
import com.bwssystems.HABridge.upnp.UpnpListener;
import com.bwssystems.HABridge.upnp.UpnpSettingsResource;
import com.bwssystems.vera.VeraInfo;
public class AmazonEchoBridge {
public class HABridge {
/*
* This program is based on the work of armzilla from this github repository:
@@ -31,15 +32,16 @@ public class AmazonEchoBridge {
*
*/
public static void main(String[] args) {
Logger log = LoggerFactory.getLogger(AmazonEchoBridge.class);
Logger log = LoggerFactory.getLogger(HABridge.class);
DeviceResource theResources;
HueMulator theHueMulator;
UpnpSettingsResource theSettingResponder;
UpnpListener theUpnpListener;
VeraInfo theVera;
InetAddress address;
String addressString;
String upnpAddressString;
String serverPort;
BridgeSettings bridgeSettings;
//get ip address for upnp requests
try {
address = InetAddress.getLocalHost();
@@ -49,27 +51,33 @@ public class AmazonEchoBridge {
return;
}
upnpAddressString = System.getProperty("upnp.config.address", addressString);
bridgeSettings = new BridgeSettings();
bridgeSettings.setUpnpConfigAddress(System.getProperty("upnp.config.address", addressString));
bridgeSettings.setUpnpDeviceDb(System.getProperty("upnp.device.db", "data/device.db"));
bridgeSettings.setUpnpResponsePort(System.getProperty("upnp.response.port", "50000"));
bridgeSettings.setVeraAddress(System.getProperty("vera.address", "192.168.1.100"));
// 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
serverPort = System.getProperty("server.port", "8080");
port(Integer.valueOf(serverPort));
bridgeSettings.setServerPort(System.getProperty("server.port", "8080"));
port(Integer.valueOf(bridgeSettings.getServerPort()));
// sparkjava config directive to set html static file location for Jetty
staticFileLocation("/public");
log.info("Starting setup....");
theVera = new VeraInfo(bridgeSettings.getVeraAddress());
theVera.getSdata();
// setup the class to handle the resource setup rest api
theResources = new DeviceResource();
theResources = new DeviceResource(bridgeSettings);
// setup the class to handle the hue emulator rest api
theHueMulator = new HueMulator(theResources.getDeviceRepository());
// setup the class to handle the upnp response rest api
theSettingResponder = new UpnpSettingsResource(upnpAddressString);
theSettingResponder = new UpnpSettingsResource(bridgeSettings);
// wait for the sparkjava initialization of the rest api classes to be complete
awaitInitialization();
// start the upnp ssdp discovery listener
theUpnpListener = new UpnpListener(upnpAddressString, serverPort);
theUpnpListener = new UpnpListener(bridgeSettings);
log.info("Done setup, application to run....");
theUpnpListener.startListening();
}

View File

@@ -1,4 +1,4 @@
package com.bwssytems.HABridge;
package com.bwssystems.HABridge;
import com.google.gson.Gson;
import spark.ResponseTransformer;

View File

@@ -1,4 +1,4 @@
package com.bwssytems.HABridge.api;
package com.bwssystems.HABridge.api;
/**
* Created by arm on 4/13/15.

View File

@@ -1,4 +1,4 @@
package com.bwssytems.HABridge.api.hue;
package com.bwssystems.HABridge.api.hue;
import java.util.HashMap;
import java.util.LinkedList;

View File

@@ -1,4 +1,4 @@
package com.bwssytems.HABridge.api.hue;
package com.bwssystems.HABridge.api.hue;
import java.util.List;

View File

@@ -1,8 +1,8 @@
package com.bwssytems.HABridge.api.hue;
package com.bwssystems.HABridge.api.hue;
import java.util.Map;
import com.bwssytems.HABridge.api.hue.DeviceResponse;
import com.bwssystems.HABridge.api.hue.DeviceResponse;
/**
* Created by arm on 4/14/15.

View File

@@ -1,4 +1,4 @@
package com.bwssytems.HABridge.dao;
package com.bwssystems.HABridge.dao;
/*
* Object to handle the device configuration
*/

View File

@@ -1,4 +1,4 @@
package com.bwssytems.HABridge.dao;
package com.bwssystems.HABridge.dao;
import java.io.IOException;
@@ -14,8 +14,8 @@ import java.util.Random;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.bwssytems.HABridge.JsonTransformer;
import com.bwssytems.HABridge.dao.DeviceDescriptor;
import com.bwssystems.HABridge.JsonTransformer;
import com.bwssystems.HABridge.dao.DeviceDescriptor;
import com.google.gson.stream.JsonReader;
import java.util.List;
@@ -30,9 +30,9 @@ public class DeviceRepository {
final Random random = new Random();
final Logger log = LoggerFactory.getLogger(DeviceRepository.class);
public DeviceRepository() {
public DeviceRepository(String deviceDb) {
super();
repositoryPath = Paths.get(System.getProperty("upnp.device.db", "data/device.db"));
repositoryPath = Paths.get(deviceDb);
String jsonContent = repositoryReader(repositoryPath);
devices = new HashMap<String, DeviceDescriptor>();
if(jsonContent != null)

View File

@@ -1,8 +1,4 @@
package com.bwssytems.HABridge.devicemanagmeent;
import com.bwssytems.HABridge.JsonTransformer;
import com.bwssytems.HABridge.dao.DeviceDescriptor;
import com.bwssytems.HABridge.dao.DeviceRepository;
package com.bwssystems.HABridge.devicemanagmeent;
import static spark.Spark.get;
import static spark.Spark.post;
@@ -14,6 +10,12 @@ import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.bwssystems.HABridge.BridgeSettings;
import com.bwssystems.HABridge.JsonTransformer;
import com.bwssystems.HABridge.dao.DeviceDescriptor;
import com.bwssystems.HABridge.dao.DeviceRepository;
import com.bwssystems.luupRequests.Sdata;
import com.bwssystems.vera.VeraInfo;
import com.google.gson.Gson;
/**
@@ -24,11 +26,13 @@ public class DeviceResource {
private static final Logger log = LoggerFactory.getLogger(DeviceResource.class);
private DeviceRepository deviceRepository;
private VeraInfo veraInfo;
public DeviceResource() {
public DeviceResource(BridgeSettings theSettings) {
super();
deviceRepository = new DeviceRepository();
deviceRepository = new DeviceRepository(theSettings.getUpnpDeviceDb());
veraInfo = new VeraInfo(theSettings.getVeraAddress());
setupEndpoints();
}
@@ -102,5 +106,24 @@ public class DeviceResource {
deviceRepository.delete(deleted);
return null;
}, new JsonTransformer());
get (API_CONTEXT + "/vera/devices", "application/json", (request, response) -> {
log.debug("Get vera devices");
Sdata sData = veraInfo.getSdata();
if(sData == null){
return null;
}
return sData.getDevices();
}, new JsonTransformer());
get (API_CONTEXT + "/vera/scenes", "application/json", (request, response) -> {
log.debug("Get vera scenes");
Sdata sData = veraInfo.getSdata();
if(sData == null){
return null;
}
return sData.getScenes();
}, new JsonTransformer());
}
}

View File

@@ -1,11 +1,10 @@
package com.bwssytems.HABridge.hue;
import com.bwssytems.HABridge.api.hue.DeviceResponse;
import com.bwssytems.HABridge.api.hue.DeviceState;
import com.bwssytems.HABridge.api.hue.HueApiResponse;
import com.bwssytems.HABridge.dao.*;
import com.bwssytems.HABridge.JsonTransformer;
package com.bwssystems.HABridge.hue;
import com.bwssystems.HABridge.JsonTransformer;
import com.bwssystems.HABridge.api.hue.DeviceResponse;
import com.bwssystems.HABridge.api.hue.DeviceState;
import com.bwssystems.HABridge.api.hue.HueApiResponse;
import com.bwssystems.HABridge.dao.*;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

View File

@@ -1,8 +1,10 @@
package com.bwssytems.HABridge.upnp;
package com.bwssystems.HABridge.upnp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.bwssystems.HABridge.BridgeSettings;
import java.io.IOException;
import java.net.*;
@@ -21,11 +23,11 @@ public class UpnpListener {
private String responseAddress;
public UpnpListener(String upnpAddress, String upnpServerPort) {
public UpnpListener(BridgeSettings theSettings) {
super();
upnpResponsePort = Integer.valueOf(System.getProperty("upnp.response.port", "50000"));
httpServerPort = Integer.valueOf(upnpServerPort);
responseAddress = upnpAddress;
upnpResponsePort = Integer.valueOf(theSettings.getUpnpResponsePort());
httpServerPort = Integer.valueOf(theSettings.getServerPort());
responseAddress = theSettings.getUpnpConfigAddress();
}
public void startListening(){

View File

@@ -1,8 +1,11 @@
package com.bwssytems.HABridge.upnp;
package com.bwssystems.HABridge.upnp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.bwssystems.HABridge.BridgeSettings;
import com.bwssystems.HABridge.JsonTransformer;
import static spark.Spark.get;
/**
@@ -17,12 +20,12 @@ public class UpnpSettingsResource {
+ "<specVersion>\n" + "<major>1</major>\n" + "<minor>0</minor>\n" + "</specVersion>\n"
+ "<URLBase>http://%s:%s/</URLBase>\n" + // hostname string
"<device>\n" + "<deviceType>urn:schemas-upnp-org:device:Basic:1</deviceType>\n"
+ "<friendlyName>Amazon-Echo-HA-Bridge (%s)</friendlyName>\n"
+ "<friendlyName>HA-Bridge (%s)</friendlyName>\n"
+ "<manufacturer>Royal Philips Electronics</manufacturer>\n"
+ "<manufacturerURL>http://www.bwssystems..com</manufacturerURL>\n"
+ "<modelDescription>Hue Emulator for Amazon Echo bridge</modelDescription>\n"
+ "<manufacturerURL>http://www.bwssystems.com</manufacturerURL>\n"
+ "<modelDescription>Hue Emulator for HA bridge</modelDescription>\n"
+ "<modelName>Philips hue bridge 2012</modelName>\n" + "<modelNumber>929000226503</modelNumber>\n"
+ "<modelURL>http://www.bwssystems.com/amazon-echo-bridge-compact</modelURL>\n"
+ "<modelURL>http://www.bwssystems.com/ha-bridge</modelURL>\n"
+ "<serialNumber>01189998819991197253</serialNumber>\n"
+ "<UDN>uuid:88f6698f-2c83-4393-bd03-cd54a9f8595</UDN>\n" + "<serviceList>\n" + "<service>\n"
+ "<serviceType>(null)</serviceType>\n" + "<serviceId>(null)</serviceId>\n"
@@ -35,29 +38,30 @@ public class UpnpSettingsResource {
+ "<depth>24</depth>\n" + "<url>hue_logo_3.png</url>\n" + "</icon>\n" + "</iconList>\n" + "</device>\n"
+ "</root>\n";
public UpnpSettingsResource(String upnpAddress) {
public UpnpSettingsResource(BridgeSettings theSettings) {
super();
setupListener(upnpAddress);
setupListener(theSettings);
}
private void setupListener (String hostName) {
private void setupListener (BridgeSettings theSettings) {
// http://ip_address:port/upnp/:id/setup.xml which returns the xml configuration for the location of the hue emulator
get(UPNP_CONTEXT + "/:id/setup.xml", "application/xml", (request, response) -> {
log.info("upnp device settings requested: " + request.params(":id") + " from " + request.ip());
String portNumber = Integer.toString(request.port());
String filledTemplate = String.format(hueTemplate, hostName, portNumber, hostName);
String filledTemplate = String.format(hueTemplate, theSettings.getUpnpConfigAddress(), portNumber, theSettings.getUpnpConfigAddress());
log.debug("upnp device settings response: " + filledTemplate);
response.status(201);
return filledTemplate;
} );
get(UPNP_CONTEXT + "/configaddress", "application/xml", (request, response) -> {
log.info("upnp config address requested: from " + request.ip());
// http://ip_address:port/upnp/settings which returns the bridge configuration settings
get(UPNP_CONTEXT + "/settings", "application/xml", (request, response) -> {
log.debug("bridge settings requested from " + request.ip());
response.status(201);
return hostName;
} );
return theSettings;
}, new JsonTransformer());
}
}