Updated persistence

This commit is contained in:
Admin
2015-08-05 16:21:20 -05:00
parent cf92620617
commit 561d3cfed5
8 changed files with 120 additions and 65 deletions

View File

@@ -9,19 +9,23 @@ mvn install
``` ```
Then locate the jar and start the server with: Then locate the jar and start the server with:
``` ```
java -jar -Dupnp.config.address=192.168.1.Z amazon-echo-bridge-compact-0.X.Y.jar java -jar amazon-echo-bridge-compact-0.X.Y.jar
``` ```
replace the -Dupnp.config.address value with the server ipv4 address. The server defaults to the first available address on the host. Replace the -Dupnp.config.address=<ip address> value with the server ipv4 address you would like to use.
The server defaults to running on port 8080. If you're already running a server (like openHAB) on 8080, -Dserver.port=XXXX on the command line. The server defaults to running on port 8080. If you're already running a server (like openHAB) on 8080, -Dserver.port=<port> on the command line.
Then configure by going to the url: The default location for the db to contain the devices as they are added is "data/devices.db". If you would like a different filename or directory, specify -Dupnp.devices.db=<directory>/<filename> or <filename> if it is the same directory.
The default upnp response port will be 50000 otherwise it can be set with -Dupnp.response.port=<port>.
Then configure by going to the url for the host you are running on or localhost:
``` ```
http://192.168.1.240:8080 http://192.168.1.240:8080
``` ```
or Register a device, via REST by binding some sort of on/off (vera style) url or Register a device, via REST by binding some sort of on/off (vera style) url
``` ```
POST http://host:8080/api/devices/ POST http://host:8080/api/devices
{ {
"name" : "bedroom light", "name" : "bedroom light",
"deviceType" : "switch", "deviceType" : "switch",

View File

@@ -2,6 +2,9 @@ package com.bwssytems.HABridge;
import static spark.Spark.*; import static spark.Spark.*;
import java.net.InetAddress;
import java.net.UnknownHostException;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -25,8 +28,6 @@ public class AmazonEchoBridge {
* *
* There is a custom upnp listener that is started to handle discovery. * There is a custom upnp listener that is started to handle discovery.
* *
* This application does not store the lights configuration persistently.
*
* *
*/ */
public static void main(String[] args) { public static void main(String[] args) {
@@ -35,25 +36,41 @@ public class AmazonEchoBridge {
HueMulator theHueMulator; HueMulator theHueMulator;
UpnpSettingsResource theSettingResponder; UpnpSettingsResource theSettingResponder;
UpnpListener theUpnpListener; UpnpListener theUpnpListener;
InetAddress address;
String addressString;
String upnpAddressString;
String serverPort;
//get ip address for upnp requests
try {
address = InetAddress.getLocalHost();
addressString = address.getHostAddress();
} catch (UnknownHostException e) {
log.error("Cannot get ip address of this host, Exiting with message: " + e.getMessage(), e);
return;
}
upnpAddressString = System.getProperty("upnp.config.address", addressString);
// sparkjava config directive to set ip address for the web server to listen on // sparkjava config directive to set ip address for the web server to listen on
ipAddress(System.getProperty("upnp.config.address", "0.0.0.0")); // ipAddress("0.0.0.0"); // not used
// sparkjava config directive to set port for the web server to listen on // sparkjava config directive to set port for the web server to listen on
port(Integer.valueOf(System.getProperty("server.port", "8080"))); serverPort = System.getProperty("server.port", "8080");
port(Integer.valueOf(serverPort));
// sparkjava config directive to set html static file location for Jetty // sparkjava config directive to set html static file location for Jetty
staticFileLocation("/public"); staticFileLocation("/public");
log.debug("Starting setup...."); log.info("Starting setup....");
// setup the class to handle the resource setup rest api // setup the class to handle the resource setup rest api
theResources = new DeviceResource(); theResources = new DeviceResource();
// setup the class to handle the hue emulator rest api // setup the class to handle the hue emulator rest api
theHueMulator = new HueMulator(theResources.getDeviceRepository()); theHueMulator = new HueMulator(theResources.getDeviceRepository());
// setup the class to handle the upnp response rest api // setup the class to handle the upnp response rest api
theSettingResponder = new UpnpSettingsResource(); theSettingResponder = new UpnpSettingsResource(upnpAddressString);
// wait for the sparkjava initialization of the rest api classes to be complete // wait for the sparkjava initialization of the rest api classes to be complete
awaitInitialization(); awaitInitialization();
// start the upnp ssdp discovery listener // start the upnp ssdp discovery listener
theUpnpListener = new UpnpListener(); theUpnpListener = new UpnpListener(upnpAddressString, serverPort);
log.debug("Done setup, application to run...."); log.info("Done setup, application to run....");
theUpnpListener.startListening(); theUpnpListener.startListening();
} }
} }

View File

@@ -1,10 +1,12 @@
package com.bwssytems.HABridge.dao; package com.bwssytems.HABridge.dao;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.StringReader; import java.io.StringReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@@ -14,7 +16,6 @@ import org.slf4j.LoggerFactory;
import com.bwssytems.HABridge.JsonTransformer; import com.bwssytems.HABridge.JsonTransformer;
import com.bwssytems.HABridge.dao.DeviceDescriptor; import com.bwssytems.HABridge.dao.DeviceDescriptor;
import com.google.gson.Gson;
import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonReader;
import java.util.List; import java.util.List;
@@ -25,12 +26,13 @@ import java.util.ListIterator;
*/ */
public class DeviceRepository { public class DeviceRepository {
Map<String, DeviceDescriptor> devices; Map<String, DeviceDescriptor> devices;
Path repositoryPath;
final Random random = new Random(); final Random random = new Random();
final String repositoryPath = "device.db";
final Logger log = LoggerFactory.getLogger(DeviceRepository.class); final Logger log = LoggerFactory.getLogger(DeviceRepository.class);
public DeviceRepository() { public DeviceRepository() {
super(); super();
repositoryPath = Paths.get(System.getProperty("upnp.device.db", "data/device.db"));
String jsonContent = repositoryReader(repositoryPath); String jsonContent = repositoryReader(repositoryPath);
devices = new HashMap<String, DeviceDescriptor>(); devices = new HashMap<String, DeviceDescriptor>();
if(jsonContent != null) if(jsonContent != null)
@@ -85,45 +87,42 @@ public class DeviceRepository {
} }
private void repositoryWriter(String content, String filePath) { private void repositoryWriter(String content, Path filePath) {
FileWriter writer = null; if(Files.exists(filePath) && !Files.isWritable(filePath)){
log.error("Error file is not writable: " + filePath);
return;
}
if(Files.notExists(filePath.getParent())) {
try {
Files.createDirectories(filePath.getParent());
} catch (IOException e) {
log.error("Error creating the directory: " + filePath + " message: " + e.getMessage(), e);
}
}
try { try {
writer = new FileWriter(filePath, false); Files.write(filePath, content.getBytes(), StandardOpenOption.CREATE);
writer.write(content);
} catch (IOException e) { } catch (IOException e) {
log.error("Error writing the file: " + filePath + " message: " + e.getMessage(), e); log.error("Error writing the file: " + filePath + " message: " + e.getMessage(), e);
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
log.error("Error closing the file (w): " + filePath + " message: " + e.getMessage(), e);
}
}
} }
} }
private String repositoryReader(String filePath) { private String repositoryReader(Path filePath) {
FileReader reader = null;
BufferedReader br = null;
String content = null; String content = null;
if(Files.notExists(filePath) || !Files.isReadable(filePath)){
log.error("Error reading the file: " + filePath + " - Does not exist or is not readable. ");
return null;
}
try { try {
reader = new FileReader(filePath); content = new String(Files.readAllBytes(filePath));
br = new BufferedReader(reader);
content = br.readLine();
} catch (IOException e) { } catch (IOException e) {
log.error("Error reading the file: " + filePath + " message: " + e.getMessage(), e); log.error("Error reading the file: " + filePath + " message: " + e.getMessage(), e);
} finally {
if (reader != null) {
try {
br.close();
reader.close();
} catch (IOException e) {
log.error("Error closing the file (r): " + filePath + " message: " + e.getMessage(), e);
}
}
} }
return content; return content;
} }

View File

@@ -38,7 +38,7 @@ public class DeviceResource {
private void setupEndpoints() { private void setupEndpoints() {
log.debug("Setting up endpoints"); log.debug("Setting up endpoints");
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);
DeviceDescriptor deviceEntry = new DeviceDescriptor(); DeviceDescriptor deviceEntry = new DeviceDescriptor();
@@ -75,7 +75,7 @@ public class DeviceResource {
return deviceEntry; return deviceEntry;
}, new JsonTransformer()); }, new JsonTransformer());
get (API_CONTEXT + "/", "application/json", (request, response) -> { get (API_CONTEXT, "application/json", (request, response) -> {
List<DeviceDescriptor> deviceList = deviceRepository.findAll(); List<DeviceDescriptor> deviceList = deviceRepository.findAll();
log.debug("Get all devices"); log.debug("Get all devices");
JsonTransformer aRenderer = new JsonTransformer(); JsonTransformer aRenderer = new JsonTransformer();

View File

@@ -21,11 +21,11 @@ public class UpnpListener {
private String responseAddress; private String responseAddress;
public UpnpListener() { public UpnpListener(String upnpAddress, String upnpServerPort) {
super(); super();
upnpResponsePort = Integer.valueOf(System.getProperty("upnp.response.port", "50000")); upnpResponsePort = Integer.valueOf(System.getProperty("upnp.response.port", "50000"));
httpServerPort = Integer.valueOf(System.getProperty("server.port", "8080")); httpServerPort = Integer.valueOf(upnpServerPort);
responseAddress = System.getProperty("upnp.config.address", "192.168.14.136"); responseAddress = upnpAddress;
} }
public void startListening(){ public void startListening(){

View File

@@ -35,16 +35,15 @@ public class UpnpSettingsResource {
+ "<depth>24</depth>\n" + "<url>hue_logo_3.png</url>\n" + "</icon>\n" + "</iconList>\n" + "</device>\n" + "<depth>24</depth>\n" + "<url>hue_logo_3.png</url>\n" + "</icon>\n" + "</iconList>\n" + "</device>\n"
+ "</root>\n"; + "</root>\n";
public UpnpSettingsResource() { public UpnpSettingsResource(String upnpAddress) {
super(); super();
setupListener(); setupListener(upnpAddress);
} }
private void setupListener () { private void setupListener (String hostName) {
// http://ip_address:port/upnp/:id/setup.xml which returns the xml configuration for the location of the hue emulator // 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) -> { get(UPNP_CONTEXT + "/:id/setup.xml", "application/xml", (request, response) -> {
log.info("upnp device settings requested: " + request.params(":id") + " from " + request.ip()); log.info("upnp device settings requested: " + request.params(":id") + " from " + request.ip());
String hostName = System.getProperty("upnp.config.address", "192.168.1.1");
String portNumber = Integer.toString(request.port()); String portNumber = Integer.toString(request.port());
String filledTemplate = String.format(hueTemplate, hostName, portNumber, hostName); String filledTemplate = String.format(hueTemplate, hostName, portNumber, hostName);
log.debug("upnp device settings response: " + filledTemplate); log.debug("upnp device settings response: " + filledTemplate);
@@ -52,5 +51,13 @@ public class UpnpSettingsResource {
return filledTemplate; return filledTemplate;
} ); } );
get(UPNP_CONTEXT + "/configaddress", "application/xml", (request, response) -> {
log.info("upnp config address requested: from " + request.ip());
response.status(201);
return hostName;
} );
} }
} }

View File

@@ -40,6 +40,7 @@
<div class="panel-body"> <div class="panel-body">
<form class="form-horizontal"> <form class="form-horizontal">
<div class="form-group">
<label class="col-xs-12 col-sm-3 control-label" for="bridge-base">Bridge server</label> <label class="col-xs-12 col-sm-3 control-label" for="bridge-base">Bridge server</label>
<div class="col-xs-8 col-sm-7"> <div class="col-xs-8 col-sm-7">
@@ -51,6 +52,14 @@
</button> </button>
<button type="submit" class="col-xs-2 col-sm-1 btn btn-primary" ng-click="testUrl(bridge.base)">Go <button type="submit" class="col-xs-2 col-sm-1 btn btn-primary" ng-click="testUrl(bridge.base)">Go
</button> </button>
</div>
<div class="form-group">
<label class="col-xs-12 col-sm-3 control-label" for="bridge-base">upnp.config.address</label>
<div class="col-xs-8 col-sm-7">
<input id="bridge-base" class="form-control" type="text" ng-model="bridge.upnpconfigaddress"
placeholder="upnp config address setting" readonly>
</div>
</div>
</form> </form>
</div> </div>
</div> </div>

View File

@@ -1,7 +1,7 @@
angular.module('amazonechobridge', []) angular.module('amazonechobridge', [])
.service('bridgeService', ["$http", function ($http) { .service('bridgeService', ["$http", function ($http) {
var self = this; var self = this;
this.state = {base: window.location.origin + "/api/devices/", devices: [], error: ""}; this.state = {base: window.location.origin + "/api/devices", upnpbase: window.location.origin + "/upnp/configaddress", devices: [], error: ""};
this.viewDevices = function () { this.viewDevices = function () {
this.state.error = ""; this.state.error = "";
@@ -21,6 +21,24 @@ angular.module('amazonechobridge', [])
); );
}; };
this.viewConfigAddress = function () {
this.state.error = "";
return $http.get(this.state.upnpbase).then(
function (response) {
self.state.upnpconfigaddress = response.data;
},
function (error) {
if (error.data) {
self.state.error = error.data.message;
} else {
self.state.error = "If you're not seeing any address, you may be running into problems with CORS. " +
"You can work around this by running a fresh launch of Chrome with the --disable-web-security flag.";
}
console.log(error);
}
);
};
this.addDevice = function (id, name, type, onUrl, offUrl) { this.addDevice = function (id, name, type, onUrl, offUrl) {
this.state.error = ""; this.state.error = "";
if (id) { if (id) {
@@ -87,6 +105,7 @@ angular.module('amazonechobridge', [])
.controller('ViewingController', ["$scope", "bridgeService", function ($scope, bridgeService) { .controller('ViewingController', ["$scope", "bridgeService", function ($scope, bridgeService) {
bridgeService.viewDevices(); bridgeService.viewDevices();
bridgeService.viewConfigAddress();
$scope.bridge = bridgeService.state; $scope.bridge = bridgeService.state;
$scope.deleteDevice = function (device) { $scope.deleteDevice = function (device) {
bridgeService.deleteDevice(device.id); bridgeService.deleteDevice(device.id);