Skip to content

Commit

Permalink
Added backup and restore for device db functionality. Testing multi
Browse files Browse the repository at this point in the history
button press for harmony.
  • Loading branch information
bwssytems committed Jan 26, 2016
1 parent 315fd31 commit 4bc91be
Show file tree
Hide file tree
Showing 7 changed files with 280 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.bwssystems.HABridge</groupId>
<artifactId>ha-bridge</artifactId>
<version>1.3.1a</version>
<version>1.3.1b</version>
<packaging>jar</packaging>

<name>HA Bridge</name>
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/bwssystems/HABridge/dao/BackupFilename.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.bwssystems.HABridge.dao;

public class BackupFilename {
private String filename;

public String getFilename() {
return filename;
}

public void setFilename(String filename) {
this.filename = filename;
}
}
80 changes: 77 additions & 3 deletions src/main/java/com/bwssystems/HABridge/dao/DeviceRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@

import java.io.IOException;
import java.io.StringReader;
import java.nio.file.DirectoryStream;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
Expand All @@ -33,8 +38,16 @@ public class DeviceRepository {

public DeviceRepository(String deviceDb) {
super();
repositoryPath = Paths.get(deviceDb);
String jsonContent = repositoryReader(repositoryPath);
_loadRepository(deviceDb);
}

private void _loadRepository(String aFilePath){
repositoryPath = Paths.get(aFilePath);
_loadRepository(repositoryPath);
}

private void _loadRepository(Path aPath){
String jsonContent = repositoryReader(aPath);
devices = new HashMap<String, DeviceDescriptor>();

if(jsonContent != null)
Expand All @@ -47,7 +60,8 @@ public DeviceRepository(String deviceDb) {
put(theDevice.getId(), theDevice);
}
}
}

}

public List<DeviceDescriptor> findAll() {
List<DeviceDescriptor> list = new ArrayList<DeviceDescriptor>(devices.values());
Expand Down Expand Up @@ -79,6 +93,66 @@ public void save(DeviceDescriptor aDescriptor) {
log.debug("Save device: " + aDescriptor.getName());
}

public String backup(String aFilename) {
if(aFilename == null || aFilename.equalsIgnoreCase("")) {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
aFilename = "devicedb-" + dateFormat.format(Calendar.getInstance().getTime()) + ".bk";
}
else
aFilename = aFilename + ".bk";
try {
Files.copy(repositoryPath, FileSystems.getDefault().getPath(repositoryPath.getParent().toString(), aFilename), StandardCopyOption.COPY_ATTRIBUTES);
} catch (IOException e) {
log.error("Could not backup to file: " + aFilename + " message: " + e.getMessage(), e);
}
log.debug("Backup repository: " + aFilename);
return aFilename;
}

public String deleteBackup(String aFilename) {
log.debug("Delete backup repository: " + aFilename);
try {
Files.delete(FileSystems.getDefault().getPath(repositoryPath.getParent().toString(), aFilename));
} catch (IOException e) {
log.error("Could not delete file: " + aFilename + " message: " + e.getMessage(), e);
}
return aFilename;
}

public String restoreBackup(String aFilename) {
log.debug("Restore backup repository: " + aFilename);
try {
Path target = null;
if(Files.exists(repositoryPath)) {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
target = FileSystems.getDefault().getPath(repositoryPath.getParent().toString(), "devicedb-" + dateFormat.format(Calendar.getInstance().getTime()) + ".bk");
Files.move(repositoryPath, target);
}
Files.copy(FileSystems.getDefault().getPath(repositoryPath.getParent().toString(), aFilename), repositoryPath, StandardCopyOption.COPY_ATTRIBUTES);
} catch (IOException e) {
log.error("Error restoring the file: " + aFilename + " message: " + e.getMessage(), e);
return null;
}
_loadRepository(repositoryPath);
return aFilename;
}

public List<String> getBackups() {
List<String> theFilenames = new ArrayList<String>();
Path dir = repositoryPath.getParent();
try (DirectoryStream<Path> stream =
Files.newDirectoryStream(dir, "*.{bk}")) {
for (Path entry: stream) {
theFilenames.add(entry.getFileName().toString());
}
} catch (IOException x) {
// IOException can never be thrown by the iteration.
// In this snippet, it can // only be thrown by newDirectoryStream.
log.error("Issue getting direcotyr listing for backups - " + x.getMessage());
}
return theFilenames;
}

public String delete(DeviceDescriptor aDescriptor) {
if (aDescriptor != null) {
devices.remove(aDescriptor.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.bwssystems.HABridge.BridgeSettings;
import com.bwssystems.HABridge.JsonTransformer;
import com.bwssystems.HABridge.Version;
import com.bwssystems.HABridge.dao.BackupFilename;
import com.bwssystems.HABridge.dao.DeviceDescriptor;
import com.bwssystems.HABridge.dao.DeviceRepository;
import com.bwssystems.NestBridge.NestHome;
Expand Down Expand Up @@ -234,5 +235,66 @@ private void setupEndpoints() {
response.status(HttpStatus.SC_OK);
return nestHome.getItems();
}, new JsonTransformer());

get (API_CONTEXT + "/backup/available", "application/json", (request, response) -> {
log.debug("Get backup filenames");
response.status(HttpStatus.SC_OK);
return deviceRepository.getBackups();
}, new JsonTransformer());

// http://ip_address:port/api/devices/backup/create CORS request
options(API_CONTEXT + "/backup/create", "application/json", (request, response) -> {
response.status(HttpStatus.SC_OK);
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.header("Access-Control-Allow-Methods", "PUT");
response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
response.header("Content-Type", "text/html; charset=utf-8");
return "";
});
put (API_CONTEXT + "/backup/create", "application/json", (request, response) -> {
log.debug("Create backup: " + request.body());
BackupFilename aFilename = new Gson().fromJson(request.body(), BackupFilename.class);
BackupFilename returnFilename = new BackupFilename();
returnFilename.setFilename(deviceRepository.backup(aFilename.getFilename()));
return returnFilename;
}, new JsonTransformer());

// http://ip_address:port/api/devices/backup/delete CORS request
options(API_CONTEXT + "/backup/delete", "application/json", (request, response) -> {
response.status(HttpStatus.SC_OK);
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.header("Access-Control-Allow-Methods", "POST");
response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
response.header("Content-Type", "text/html; charset=utf-8");
return "";
});
post (API_CONTEXT + "/backup/delete", "application/json", (request, response) -> {
log.debug("Delete backup: " + request.body());
BackupFilename aFilename = new Gson().fromJson(request.body(), BackupFilename.class);
if(aFilename != null)
deviceRepository.deleteBackup(aFilename.getFilename());
else
log.warn("No filename given for delete backup.");
return null;
}, new JsonTransformer());

// http://ip_address:port/api/devices/backup/restore CORS request
options(API_CONTEXT + "/backup/restore", "application/json", (request, response) -> {
response.status(HttpStatus.SC_OK);
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.header("Access-Control-Allow-Methods", "POST");
response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
response.header("Content-Type", "text/html; charset=utf-8");
return "";
});
post (API_CONTEXT + "/backup/restore", "application/json", (request, response) -> {
log.debug("Restore backup: " + request.body());
BackupFilename aFilename = new Gson().fromJson(request.body(), BackupFilename.class);
if(aFilename != null)
deviceRepository.restoreBackup(aFilename.getFilename());
else
log.warn("No filename given for restore backup.");
return null;
}, new JsonTransformer());
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/bwssystems/HABridge/hue/HueMulator.java
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ else if(device.getDeviceType().toLowerCase().contains("button") || (device.getMa
else {
for(int i = 0; i < deviceButtons.length; i++) {
if( i > 0)
Thread.sleep(500);
Thread.sleep(100);
log.debug("pressing button: " + deviceButtons[i].getDevice() + " - " + deviceButtons[i].getButton() + " - iteration: " + String.valueOf(i));
myHarmony.pressButton(deviceButtons[i]);
}
Expand Down
92 changes: 89 additions & 3 deletions src/main/resources/public/scripts/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ app.factory('BridgeSettings', function() {
app.service('bridgeService', function ($http, $window, BridgeSettings) {
var self = this;
self.BridgeSettings = BridgeSettings;
this.state = {base: window.location.origin + "/api/devices", upnpbase: window.location.origin + "/upnp/settings", huebase: window.location.origin + "/api", devices: [], device: [], error: "", showVera: false, showHarmony: false, showNest: false, habridgeversion: ""};
this.state = {base: window.location.origin + "/api/devices", upnpbase: window.location.origin + "/upnp/settings", huebase: window.location.origin + "/api", backups: [], devices: [], device: [], error: "", showVera: false, showHarmony: false, showNest: false, habridgeversion: ""};

this.viewDevices = function () {
this.state.error = "";
Expand Down Expand Up @@ -192,11 +192,26 @@ app.service('bridgeService', function ($http, $window, BridgeSettings) {
);
};

this.viewBackups = function () {
this.state.error = "";
return $http.get(this.state.base + "/backup/available").then(
function (response) {
self.state.backups = response.data;
},
function (error) {
if (error.data) {
$window.alert("Get Backups Error: " + error.data.message);
} else {
$window.alert("Get Backups Error: unknown");
}
}
);
};

this.viewNestItems = function () {
this.state.error = "";
if(!this.state.showNest)
return;
this.state.error = "";
return $http.get(this.state.base + "/nest/items").then(
function (response) {
self.state.nestitems = response.data;
Expand All @@ -215,7 +230,6 @@ app.service('bridgeService', function ($http, $window, BridgeSettings) {
this.state.error = "";
if(!this.state.showVera)
return;
this.state.error = "";
return $http.get(this.state.base + "/vera/devices").then(
function (response) {
self.state.veradevices = response.data;
Expand Down Expand Up @@ -361,6 +375,58 @@ app.service('bridgeService', function ($http, $window, BridgeSettings) {
}
};

this.backupDeviceDb = function (afilename) {
this.state.error = "";
return $http.put(this.state.base + "/backup/create", {
filename: afilename
}).then(
function (response) {
self.viewBackups();
},
function (error) {
if (error.data) {
self.state.error = error.data.message;
}
$window.alert("Backup Device Db Error: unknown");
}
);
};

this.restoreBackup = function (afilename) {
this.state.error = "";
return $http.post(this.state.base + "/backup/restore", {
filename: afilename
}).then(
function (response) {
self.viewBackups();
self.viewDevices();
},
function (error) {
if (error.data) {
self.state.error = error.data.message;
}
$window.alert("Backup Db Restore Error: unknown");
}
);
};

this.deleteBackup = function (afilename) {
this.state.error = "";
return $http.post(this.state.base + "/backup/delete", {
filename: afilename
}).then(
function (response) {
self.viewBackups();
},
function (error) {
if (error.data) {
self.state.error = error.data.message;
}
$window.alert("Backup Db Frlryr Error: unknown");
}
);
};

this.deleteDevice = function (id) {
this.state.error = "";
return $http.delete(this.state.base + "/" + id).then(
Expand Down Expand Up @@ -417,9 +483,13 @@ app.controller('ViewingController', function ($scope, $location, $http, $window,

$scope.BridgeSettings = bridgeService.BridgeSettings;
bridgeService.viewDevices();
bridgeService.viewBackups();
$scope.bridge = bridgeService.state;
$scope.optionalbackupname = "";
$scope.visible = false;
$scope.imgUrl = "glyphicon glyphicon-plus";
$scope.visibleBk = false;
$scope.imgBkUrl = "glyphicon glyphicon-plus";
$scope.predicate = '';
$scope.reverse = true;
$scope.order = function(predicate) {
Expand All @@ -443,13 +513,29 @@ app.controller('ViewingController', function ($scope, $location, $http, $window,
bridgeService.editDevice(device);
$location.path('/editdevice');
};
$scope.backupDeviceDb = function (optionalbackupname) {
bridgeService.backupDeviceDb(optionalbackupname);
};
$scope.restoreBackup = function (backupname) {
bridgeService.restoreBackup(backupname);
};
$scope.deleteBackup = function (backupname) {
bridgeService.deleteBackup(backupname);
};
$scope.toggle = function () {
$scope.visible = !$scope.visible;
if($scope.visible)
$scope.imgUrl = "glyphicon glyphicon-minus";
else
$scope.imgUrl = "glyphicon glyphicon-plus";
};
$scope.toggleBk = function () {
$scope.visibleBk = !$scope.visibleBk;
if($scope.visibleBk)
$scope.imgBkUrl = "glyphicon glyphicon-minus";
else
$scope.imgBkUrl = "glyphicon glyphicon-plus";
};
});

app.controller('AddingController', function ($scope, $location, $http, bridgeService, BridgeSettings) {
Expand Down
Loading

0 comments on commit 4bc91be

Please sign in to comment.