Compare commits

..

12 Commits

Author SHA1 Message Date
Admin
937fb5d32d Final fix for resolving room mapping. Also applied this fix to category
mappign in SDATA as well.
2015-08-27 08:44:09 -05:00
Admin
bd60d63d0f Fix issue with devices with no rooms or categories. 2015-08-26 16:52:44 -05:00
Admin
439b081bd5 Updated vera info class logging calls to be debug only. 2015-08-26 11:48:51 -05:00
Admin
41f68f58b0 Updated Hue description response emulation. Updated the management of
devices in the map. Updated logging information to be less in normal
mode.
2015-08-26 11:26:49 -05:00
Admin
fa15cf3952 update upnp description.xml response to come from root instead of
/upnp.....
2015-08-25 16:42:30 -05:00
Admin
3ea7f2903f commit for any latent changes. 2015-08-25 10:19:02 -05:00
Admin
7746938c62 Updated upnp discovery logic to only respond to the echo request for a
upnp device and updated hue response when getting lights to be more
accurate with the hue.
2015-08-21 16:06:06 -05:00
Admin
96074628fb Merge remote-tracking branch 'origin/master' 2015-08-21 08:56:05 -05:00
Admin
9dc8d8f8bc Updated call for hue user create/addition call. Updated upnp response
variables.
2015-08-20 16:49:12 -05:00
Admin
8de39a8bee Work on naming scenes 2015-08-19 16:43:46 -05:00
Admin
626f0641cc Changed navigation to use nav tab pills look. 2015-08-19 15:17:24 -05:00
Admin
020da99e1c Update .gitignore attributes for eclipse 2015-08-18 14:18:24 -05:00
17 changed files with 131 additions and 76 deletions

1
.gitignore vendored
View File

@@ -9,3 +9,4 @@ buildNumber.properties
data
.idea
/target/
/.settings/

View File

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

View File

@@ -75,7 +75,6 @@ public class HABridge {
// start the upnp ssdp discovery listener
theUpnpListener = new UpnpListener(bridgeSettings);
log.info("Done setup, application to run....");
theUpnpListener.startListening();
}
}

View File

@@ -0,0 +1,19 @@
package com.bwssystems.HABridge.api;
public class UserCreateRequest {
private String devicetype;
private String username;
public String getDevicetype() {
return devicetype;
}
public void setDevicetype(String devicetype) {
this.devicetype = devicetype;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}

View File

@@ -43,7 +43,7 @@ public class DeviceRepository {
DeviceDescriptor theDevice = null;
while (theIterator.hasNext()) {
theDevice = theIterator.next();
put(Integer.parseInt(theDevice.getId()), theDevice);
put(theDevice.getId(), theDevice);
}
}
}
@@ -62,20 +62,20 @@ public class DeviceRepository {
return devices.get(id);
}
private void put(int id, DeviceDescriptor aDescriptor) {
devices.put(String.valueOf(id), aDescriptor);
private void put(String id, DeviceDescriptor aDescriptor) {
devices.put(id, aDescriptor);
}
public void save(DeviceDescriptor aDescriptor) {
int id = random.nextInt(Integer.MAX_VALUE);
if(aDescriptor.getId() != null)
devices.remove(aDescriptor.getId());
else
aDescriptor.setId(String.valueOf(id));
put(id, aDescriptor);
aDescriptor.setId(String.valueOf(random.nextInt(Integer.MAX_VALUE)));
put(aDescriptor.getId(), aDescriptor);
JsonTransformer aRenderer = new JsonTransformer();
String jsonValue = aRenderer.render(findAll());
repositoryWriter(jsonValue, repositoryPath);
log.debug("Save device: " + aDescriptor.getName());
}
public String delete(DeviceDescriptor aDescriptor) {

View File

@@ -41,7 +41,7 @@ public class DeviceResource {
}
private void setupEndpoints() {
log.debug("Setting up endpoints");
log.info("HABridge device management service started.... ");
post(API_CONTEXT, "application/json", (request, response) -> {
log.debug("Create a Device - request body: " + request.body());
DeviceDescriptor device = new Gson().fromJson(request.body(), DeviceDescriptor.class);
@@ -56,22 +56,24 @@ public class DeviceResource {
log.debug("Create a Device - device json off URL:" + deviceEntry.getOffUrl());
deviceRepository.save(deviceEntry);
log.debug("Created a Device");
log.debug("Created a Device: " + request.body());
response.status(201);
return deviceEntry;
}, new JsonTransformer());
put (API_CONTEXT + "/:id", "application/json", (request, response) -> {
log.debug("Saved a Device");
DeviceDescriptor device = new Gson().fromJson(request.body(), DeviceDescriptor.class);
DeviceDescriptor deviceEntry = deviceRepository.findOne(request.params(":id"));
if(deviceEntry == null){
log.debug("Could not save an edited Device Id: " + request.params(":id"));
return null;
}
log.debug("Saving an edited Device: " + deviceEntry.getName());
deviceEntry.setName(device.getName());
deviceEntry.setDeviceType(device.getDeviceType());
if(device.getDeviceType() != null)
deviceEntry.setDeviceType(device.getDeviceType());
deviceEntry.setOnUrl(device.getOnUrl());
deviceEntry.setOffUrl(device.getOffUrl());

View File

@@ -1,12 +1,14 @@
package com.bwssystems.HABridge.hue;
import com.bwssystems.HABridge.JsonTransformer;
import com.bwssystems.HABridge.api.UserCreateRequest;
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;
import com.google.gson.Gson;
import static spark.Spark.get;
import static spark.Spark.post;
@@ -51,33 +53,39 @@ public class HueMulator {
// This function sets up the sparkjava rest calls for the hue api
private void setupEndpoints() {
log.info("Hue emulator service started....");
// http://ip_address:port/api/{userId}/lights returns json objects of all lights configured
get(HUE_CONTEXT + "/:userid/lights", "application/json", (request, response) -> {
String userId = request.params(":userid");
log.info("hue lights list requested: " + userId + " from " + request.ip());
List<DeviceDescriptor> deviceList = repository.findByDeviceType("switch");
JsonTransformer aRenderer = new JsonTransformer();
String theStream = aRenderer.render(deviceList);
log.debug("The Device List: " + theStream);
Map<String, String> deviceResponseMap = new HashMap<>();
log.debug("hue lights list requested: " + userId + " from " + request.ip());
List<DeviceDescriptor> deviceList = repository.findAll();
Map<String, DeviceResponse> deviceResponseMap = new HashMap<>();
for (DeviceDescriptor device : deviceList) {
deviceResponseMap.put(device.getId(), device.getName());
DeviceResponse deviceResponse = DeviceResponse.createResponse(device.getName(), device.getId());
deviceResponseMap.put(device.getId(), deviceResponse);
}
response.status(200);
return deviceResponseMap;
} , new JsonTransformer());
// http://ip_address:port/api/* returns json object for a test call
post(HUE_CONTEXT + "/*", "application/json", (request, response) -> {
response.status(200);
return "[{\"success\":{\"username\":\"lights\"}}]";
// http://ip_address:port/api with body of user request returns json object for a success of user add
post(HUE_CONTEXT, "application/json", (request, response) -> {
log.debug("hue api user create requested: " + request.body() + " from " + request.ip());
UserCreateRequest aNewUser = new Gson().fromJson(request.body(), UserCreateRequest.class);
String newUser = aNewUser.getUsername();
if(newUser == null)
newUser = "lightssystem";
log.debug("hue api user create requested for device type: " + aNewUser.getDevicetype() + " and username: " + newUser);
response.status(200);
return "[{\"success\":{\"username\":\"" + newUser + "\"}}]";
} );
// http://ip_address:port/api/{userId} returns json objects for the list of names of lights
// http://ip_address:port/api/{userId} returns json objects for the full state
get(HUE_CONTEXT + "/:userid", "application/json", (request, response) -> {
String userId = request.params(":userid");
log.info("hue api root requested: " + userId + " from " + request.ip());
List<DeviceDescriptor> descriptorList = repository.findByDeviceType("switch");
log.debug("hue api full state requested: " + userId + " from " + request.ip());
List<DeviceDescriptor> descriptorList = repository.findAll();
if (descriptorList == null) {
response.status(404);
return null;
@@ -100,13 +108,13 @@ public class HueMulator {
get(HUE_CONTEXT + "/:userid/lights/:id", "application/json", (request, response) -> {
String userId = request.params(":userid");
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);
if (device == null) {
response.status(404);
return null;
} else {
log.info("found device named: " + device.getName());
log.debug("found device named: " + device.getName());
}
DeviceResponse lightResponse = DeviceResponse.createResponse(device.getName(), device.getId());
@@ -122,14 +130,13 @@ public class HueMulator {
*/
String userId = request.params(":userid");
String lightId = request.params(":id");
log.info("hue state change requested: " + userId + " from " + request.ip());
log.info("hue stage change body: " + request.body() );
log.debug("hue state change requested: " + userId + " from " + request.ip() + " body: " + request.body());
DeviceState state = null;
try {
state = mapper.readValue(request.body(), DeviceState.class);
} catch (IOException e) {
log.info("object mapper barfed on input", e);
log.error("Object mapper barfed on input of body.", e);
response.status(400);
return null;
}
@@ -137,6 +144,7 @@ public class HueMulator {
DeviceDescriptor device = repository.findOne(lightId);
if (device == null) {
response.status(404);
log.error("Could not find devcie: " + lightId + " for hue state change request: " + userId + " from " + request.ip() + " body: " + request.body());
return null;
}
@@ -160,15 +168,18 @@ public class HueMulator {
if(url.contains(INTENSITY_BYTE)){
String intensityByte = String.valueOf(state.getBri());
url = url.replace(INTENSITY_BYTE, intensityByte);
responseString = "[{\"success\":{\"/lights/" + lightId + "/state/bri\":"+ String.valueOf(state.getBri()) + "}}]";
}else if(url.contains(INTENSITY_PERCENT)){
int percentBrightness = (int) Math.round(state.getBri()/255.0*100);
String intensityPercent = String.valueOf(percentBrightness);
url = url.replace(INTENSITY_PERCENT, intensityPercent);
responseString = "[{\"success\":{\"/lights/" + lightId + "/state/bri\":"+ String.valueOf(state.getBri()) + "}}]";
}
//make call
if(!doHttpGETRequest(url)){
response.status(503);
log.error("Error on calling url to change device state: " + url);
return null;
}
@@ -179,12 +190,12 @@ public class HueMulator {
// This function executes the url from the device repository against the vera
protected boolean doHttpGETRequest(String url) {
log.info("calling GET on URL: " + url);
log.debug("calling GET on URL: " + url);
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse response = httpClient.execute(httpGet);
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){
return true;
}

View File

@@ -31,7 +31,7 @@ public class UpnpListener {
}
public void startListening(){
log.info("Starting UPNP Discovery Listener");
log.info("UPNP Discovery Listener started....");
try (DatagramSocket responseSocket = new DatagramSocket(upnpResponsePort);
MulticastSocket upnpMulticastSocket = new MulticastSocket(UPNP_DISCOVERY_PORT);) {
@@ -64,6 +64,12 @@ public class UpnpListener {
upnpMulticastSocket.receive(packet);
String packetString = new String(packet.getData());
if(isSSDPDiscovery(packetString)){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
log.error("could not sleep");
}
log.debug("Got SSDP Discovery packet from " + packet.getAddress().getHostAddress() + ":" + packet.getPort());
sendUpnpResponse(responseSocket, packet.getAddress(), packet.getPort());
}
@@ -83,7 +89,9 @@ public class UpnpListener {
* @return
*/
protected boolean isSSDPDiscovery(String body){
if(body != null && body.startsWith("M-SEARCH * HTTP/1.1") && body.contains("MAN: \"ssdp:discover\"")){
// log.debug("Check if this is a MAN ssdp-discover packet for a upnp basic device: " + body);
//Only respond to discover request for upnp basic device from echo, the others are for the wemo
if(body != null && body.startsWith("M-SEARCH * HTTP/1.1") && body.contains("MAN: \"ssdp:discover\"")&& body.contains("ST: urn:schemas-upnp-org:device:basic:1")){
return true;
}
return false;
@@ -92,11 +100,10 @@ public class UpnpListener {
String discoveryTemplate = "HTTP/1.1 200 OK\r\n" +
"CACHE-CONTROL: max-age=86400\r\n" +
"EXT:\r\n" +
"LOCATION: http://%s:%s/upnp/amazon-ha-bridge/setup.xml\r\n" +
"OPT: \"http://schemas.upnp.org/upnp/1/0/\"; ns=01\r\n" +
"01-NLS: %s\r\n" +
"LOCATION: http://%s:%s/description.xml\r\n" +
"SERVER: FreeRTOS/6.0.5, UPnP/1.0, IpBridge/0.1\r\n" +
"ST: urn:schemas-upnp-org:device:basic:1\r\n" +
"USN: uuid:Socket-1_0-221438K0100073::urn:Belkin:device:**\r\n\r\n";
"USN: uuid:Socket-1_0-221438K0100073::urn:schemas-upnp-org:device:basic:1\r\n\r\n";
protected void sendUpnpResponse(DatagramSocket socket, InetAddress requester, int sourcePort) throws IOException {
String discoveryResponse = String.format(discoveryTemplate, responseAddress, httpServerPort, getRandomUUIDString());
log.debug("sndUpnpResponse: " + discoveryResponse);

View File

@@ -25,7 +25,7 @@ public class UpnpSettingsResource {
+ "<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/ha-bridge</modelURL>\n"
+ "<modelURL>http://www.bwssystems.com/apps.html</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"
@@ -44,9 +44,10 @@ public class UpnpSettingsResource {
}
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());
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) -> {
log.debug("upnp device settings requested: " + request.params(":id") + " from " + request.ip());
String portNumber = Integer.toString(request.port());
String filledTemplate = String.format(hueTemplate, theSettings.getUpnpConfigAddress(), portNumber, theSettings.getUpnpConfigAddress());
log.debug("upnp device settings response: " + filledTemplate);
@@ -56,7 +57,7 @@ public class UpnpSettingsResource {
} );
// http://ip_address:port/upnp/settings which returns the bridge configuration settings
get(UPNP_CONTEXT + "/settings", "application/xml", (request, response) -> {
get(UPNP_CONTEXT + "/settings", "application/json", (request, response) -> {
log.debug("bridge settings requested from " + request.ip());
response.status(201);

View File

@@ -57,8 +57,14 @@ public class VeraInfo {
Device theDevice = null;
while (theIterator.hasNext()) {
theDevice = theIterator.next();
theDevice.setRoom(roomMap.get(theDevice.getRoom()).getName());
theDevice.setCategory(categoryMap.get(theDevice.getCategory()).getName());
if(theDevice.getRoom() != null && roomMap.get(theDevice.getRoom()) != null)
theDevice.setRoom(roomMap.get(theDevice.getRoom()).getName());
else
theDevice.setRoom("no room");
if(theDevice.getCategory() != null && categoryMap.get(theDevice.getCategory()) != null)
theDevice.setCategory(categoryMap.get(theDevice.getCategory()).getName());
else
theDevice.setCategory("<unknown>");
}
ListIterator<Scene> theSecneIter = theSdata.getScenes().listIterator();
@@ -71,13 +77,13 @@ public class VeraInfo {
// This function executes the url against the vera
protected String doHttpGETRequest(String url) {
log.info("calling GET on URL: " + url);
log.debug("calling GET on URL: " + url);
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse response = httpClient.execute(httpGet);
String theContent = EntityUtils.toString(response.getEntity()); //read content for data
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){
return theContent;
}

View File

@@ -41,7 +41,7 @@
<ul class="dropdown-menu" aria-labelledby="dropdownMenu1">
<li><a href="http://www.bwssystems.com" target="_blank">Developed by BWS Systems</a></li>
<li><a href="http://www.amazon.com/echo" target="_blank">Amazon Echo</a></li>
<li><a href="#">HA Bridge Version 0.3.0</a></li>
<li><a href="#">HA Bridge Version 0.3.2</a></li>
</ul>
</li>
</ul>

View File

@@ -148,7 +148,7 @@ app.service('bridgeService', function ($http, BridgeSettings) {
return $http.put(putUrl, {
id: id,
name: name,
deviceType: "switch",
deviceType: type,
onUrl: onUrl,
offUrl: offUrl
}).then(
@@ -165,7 +165,7 @@ app.service('bridgeService', function ($http, BridgeSettings) {
} else {
return $http.post(this.state.base, {
name: name,
deviceType: "switch",
deviceType: type,
onUrl: onUrl,
offUrl: offUrl
}).then(
@@ -225,7 +225,7 @@ app.controller('ViewingController', function ($scope, $location, bridgeService,
app.controller('AddingController', function ($scope, bridgeService, BridgeSettings) {
$scope.device = {id: "", name: "", type: "switch", onUrl: "", offUrl: ""};
$scope.device = {id: "", name: "", deviceType: "switch", onUrl: "", offUrl: ""};
$scope.vera = {base: "", port: "3480", id: ""};
$scope.vera.base = "http://" + BridgeSettings.veraaddress;
bridgeService.device = $scope.device;
@@ -250,6 +250,7 @@ app.controller('AddingController', function ($scope, bridgeService, BridgeSettin
if ($scope.vera.base.indexOf("http") < 0) {
$scope.vera.base = "http://" + $scope.vera.base;
}
$scope.device.deviceType = "scene";
$scope.device.onUrl = $scope.vera.base + ":" + $scope.vera.port
+ "/data_request?id=action&output_format=json&serviceId=urn:micasaverde-com:serviceId:HomeAutomationGateway1&action=RunScene&SceneNum="
+ $scope.vera.id;
@@ -275,6 +276,7 @@ app.controller('AddingController', function ($scope, bridgeService, BridgeSettin
if ($scope.vera.base.indexOf("http") < 0) {
$scope.vera.base = "http://" + $scope.vera.base;
}
$scope.device.deviceType = "scene";
$scope.device.name = verascene.name;
$scope.device.onUrl = $scope.vera.base + ":" + $scope.vera.port
+ "/data_request?id=action&output_format=json&serviceId=urn:micasaverde-com:serviceId:HomeAutomationGateway1&action=RunScene&SceneNum="

View File

@@ -1,11 +1,9 @@
<h2>Configuration <a class="btn btn-primary pull-right" href="#/editor"><i
class="icon-plis-sign icon-white"></i> Manual Add</a>
<a class="btn btn-primary pull-right" href="#/verascenes"><i
class="icon-plis-sign icon-white"></i> Vera Scenes</a>
<a class="btn btn-primary pull-right" href="#/veradevices"><i
class="icon-plis-sign icon-white"></i> Vera Devices</a>
</h2>
<ul class="nav nav-pills" role="tablist">
<li role="presentation" class="active"><a href="#">Configuration</a></li>
<li role="presentation"><a href="#/veradevices">Vera Devices</a></li>
<li role="presentation"><a href="#/verascenes">Vera Scenes</a></li>
<li role="presentation"><a href="#/editor">Manual Add</a></li>
</ul>
<div class="panel panel-default bridgeServer">
<div class="panel-heading">

View File

@@ -1,7 +1,10 @@
<h2>
Device Editor <a class="btn btn-primary pull-right" href="/">
Configuration</a>
</h2>
<ul class="nav nav-pills" role="tablist">
<li role="presentation"><a href="#">Configuration</a></li>
<li role="presentation"><a href="#/veradevices">Vera Devices</a></li>
<li role="presentation"><a href="#/verascenes">Vera Scenes</a></li>
<li role="presentation"><a href="#/editor">Manual Add</a></li>
<li role="presentation" class="active"><a href="#/editdevice">Edit Device</a></li>
</ul>
<div class="panel panel-default bridgeServer" ng-if="!bridge.error">
<div class="panel-heading">

View File

@@ -1,7 +1,9 @@
<h2>
Manual Add <a class="btn btn-primary pull-right" href="/">
Configuration</a>
</h2>
<ul class="nav nav-pills" role="tablist">
<li role="presentation"><a href="#">Configuration</a></li>
<li role="presentation"><a href="#/veradevices">Vera Devices</a></li>
<li role="presentation"><a href="#/verascenes">Vera Scenes</a></li>
<li role="presentation" class="active"><a href="#/editor">Manual Add</a></li>
</ul>
<div class="panel panel-default bridgeServer" ng-if="!bridge.error">
<div class="panel-heading">

View File

@@ -1,7 +1,9 @@
<h2>
Vera Device <a class="btn btn-primary pull-right" href="/">
Configuration</a>
</h2>
<ul class="nav nav-pills" role="tablist">
<li role="presentation"><a href="#">Configuration</a></li>
<li role="presentation" class="active"><a href="#/veradevices">Vera Devices</a></li>
<li role="presentation"><a href="#/verascenes">Vera Scenes</a></li>
<li role="presentation"><a href="#/editor">Manual Add</a></li>
</ul>
<div class="panel panel-default bridgeServer" ng-if="!bridge.error">
<div class="panel-heading">

View File

@@ -1,7 +1,9 @@
<h2>
Vera Scene <a class="btn btn-primary pull-right" href="/">
Configuration</a>
</h2>
<ul class="nav nav-pills" role="tablist">
<li role="presentation"><a href="#">Configuration</a></li>
<li role="presentation"><a href="#/veradevices">Vera Devices</a></li>
<li role="presentation" class="active"><a href="#/verascenes">Vera Scenes</a></li>
<li role="presentation"><a href="#/editor">Manual Add</a></li>
</ul>
<div class="panel panel-default bridgeServer" ng-if="!bridge.error">
<div class="panel-heading">