mirror of
https://github.com/bwssytems/ha-bridge.git
synced 2025-12-21 09:13:21 +00:00
Updated persistence
This commit is contained in:
@@ -2,6 +2,9 @@ package com.bwssytems.HABridge;
|
||||
|
||||
import static spark.Spark.*;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -25,8 +28,6 @@ public class AmazonEchoBridge {
|
||||
*
|
||||
* 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) {
|
||||
@@ -35,25 +36,41 @@ public class AmazonEchoBridge {
|
||||
HueMulator theHueMulator;
|
||||
UpnpSettingsResource theSettingResponder;
|
||||
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
|
||||
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
|
||||
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
|
||||
staticFileLocation("/public");
|
||||
log.debug("Starting setup....");
|
||||
log.info("Starting setup....");
|
||||
// setup the class to handle the resource setup rest api
|
||||
theResources = new DeviceResource();
|
||||
// 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();
|
||||
theSettingResponder = new UpnpSettingsResource(upnpAddressString);
|
||||
// wait for the sparkjava initialization of the rest api classes to be complete
|
||||
awaitInitialization();
|
||||
|
||||
// start the upnp ssdp discovery listener
|
||||
theUpnpListener = new UpnpListener();
|
||||
log.debug("Done setup, application to run....");
|
||||
theUpnpListener = new UpnpListener(upnpAddressString, serverPort);
|
||||
log.info("Done setup, application to run....");
|
||||
theUpnpListener.startListening();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
package com.bwssytems.HABridge.dao;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
|
||||
import java.io.IOException;
|
||||
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.HashMap;
|
||||
import java.util.Map;
|
||||
@@ -14,7 +16,6 @@ import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.bwssytems.HABridge.JsonTransformer;
|
||||
import com.bwssytems.HABridge.dao.DeviceDescriptor;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
|
||||
import java.util.List;
|
||||
@@ -25,12 +26,13 @@ import java.util.ListIterator;
|
||||
*/
|
||||
public class DeviceRepository {
|
||||
Map<String, DeviceDescriptor> devices;
|
||||
Path repositoryPath;
|
||||
final Random random = new Random();
|
||||
final String repositoryPath = "device.db";
|
||||
final Logger log = LoggerFactory.getLogger(DeviceRepository.class);
|
||||
|
||||
public DeviceRepository() {
|
||||
super();
|
||||
repositoryPath = Paths.get(System.getProperty("upnp.device.db", "data/device.db"));
|
||||
String jsonContent = repositoryReader(repositoryPath);
|
||||
devices = new HashMap<String, DeviceDescriptor>();
|
||||
if(jsonContent != null)
|
||||
@@ -85,45 +87,42 @@ public class DeviceRepository {
|
||||
|
||||
}
|
||||
|
||||
private void repositoryWriter(String content, String filePath) {
|
||||
FileWriter writer = null;
|
||||
private void repositoryWriter(String content, Path filePath) {
|
||||
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 {
|
||||
writer = new FileWriter(filePath, false);
|
||||
writer.write(content);
|
||||
Files.write(filePath, content.getBytes(), StandardOpenOption.CREATE);
|
||||
} catch (IOException 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) {
|
||||
FileReader reader = null;
|
||||
BufferedReader br = null;
|
||||
private String repositoryReader(Path filePath) {
|
||||
|
||||
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 {
|
||||
reader = new FileReader(filePath);
|
||||
br = new BufferedReader(reader);
|
||||
content = br.readLine();
|
||||
content = new String(Files.readAllBytes(filePath));
|
||||
} catch (IOException 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;
|
||||
}
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@ public class DeviceResource {
|
||||
|
||||
private void setupEndpoints() {
|
||||
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());
|
||||
DeviceDescriptor device = new Gson().fromJson(request.body(), DeviceDescriptor.class);
|
||||
DeviceDescriptor deviceEntry = new DeviceDescriptor();
|
||||
@@ -75,7 +75,7 @@ public class DeviceResource {
|
||||
return deviceEntry;
|
||||
}, new JsonTransformer());
|
||||
|
||||
get (API_CONTEXT + "/", "application/json", (request, response) -> {
|
||||
get (API_CONTEXT, "application/json", (request, response) -> {
|
||||
List<DeviceDescriptor> deviceList = deviceRepository.findAll();
|
||||
log.debug("Get all devices");
|
||||
JsonTransformer aRenderer = new JsonTransformer();
|
||||
|
||||
@@ -21,11 +21,11 @@ public class UpnpListener {
|
||||
|
||||
private String responseAddress;
|
||||
|
||||
public UpnpListener() {
|
||||
public UpnpListener(String upnpAddress, String upnpServerPort) {
|
||||
super();
|
||||
upnpResponsePort = Integer.valueOf(System.getProperty("upnp.response.port", "50000"));
|
||||
httpServerPort = Integer.valueOf(System.getProperty("server.port", "8080"));
|
||||
responseAddress = System.getProperty("upnp.config.address", "192.168.14.136");
|
||||
httpServerPort = Integer.valueOf(upnpServerPort);
|
||||
responseAddress = upnpAddress;
|
||||
}
|
||||
|
||||
public void startListening(){
|
||||
|
||||
@@ -35,16 +35,15 @@ public class UpnpSettingsResource {
|
||||
+ "<depth>24</depth>\n" + "<url>hue_logo_3.png</url>\n" + "</icon>\n" + "</iconList>\n" + "</device>\n"
|
||||
+ "</root>\n";
|
||||
|
||||
public UpnpSettingsResource() {
|
||||
public UpnpSettingsResource(String upnpAddress) {
|
||||
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
|
||||
get(UPNP_CONTEXT + "/:id/setup.xml", "application/xml", (request, response) -> {
|
||||
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 filledTemplate = String.format(hueTemplate, hostName, portNumber, hostName);
|
||||
log.debug("upnp device settings response: " + filledTemplate);
|
||||
@@ -52,5 +51,13 @@ public class UpnpSettingsResource {
|
||||
|
||||
return filledTemplate;
|
||||
} );
|
||||
|
||||
get(UPNP_CONTEXT + "/configaddress", "application/xml", (request, response) -> {
|
||||
log.info("upnp config address requested: from " + request.ip());
|
||||
|
||||
response.status(201);
|
||||
|
||||
return hostName;
|
||||
} );
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user