Add lock id to device, adding download of backups

This commit is contained in:
BWS Systems
2019-06-10 16:35:10 -05:00
parent a05b933e43
commit e86b700e93
8 changed files with 174 additions and 102 deletions

View File

@@ -86,7 +86,10 @@ public class DeviceDescriptor{
@SerializedName("onWhenDimPresent")
@Expose
private boolean onWhenDimPresent;
@SerializedName("lockDeviceId")
@Expose
private boolean lockDeviceId;
public String getName() {
return name;
}
@@ -333,4 +336,12 @@ public class DeviceDescriptor{
}
return color;
}
public boolean isLockDeviceId() {
return lockDeviceId;
}
public void setLockDeviceId(boolean lockDeviceId) {
this.lockDeviceId = lockDeviceId;
}
}

View File

@@ -143,7 +143,8 @@ public class DeviceRepository extends BackupHandler {
if (light.getOnUrl() != null)
callItems = aGsonBuilder.fromJson(light.getOnUrl(), CallItem[].class);
} catch (JsonSyntaxException e) {
log.warn("Could not decode Json for url items to get Hue state for device: {}", light.getName());
log.warn("Could not decode Json for url items to get Hue state for device: {}",
light.getName());
callItems = null;
}
@@ -201,7 +202,7 @@ public class DeviceRepository extends BackupHandler {
List<DeviceDescriptor> list = new ArrayList<DeviceDescriptor>(devices.values());
Iterator<DeviceDescriptor> deviceIterator = list.iterator();
Map<String, DeviceDescriptor> newdevices = new HashMap<String, DeviceDescriptor>();
;
nextId = seedId;
String hexValue;
Integer newValue;
@@ -209,17 +210,19 @@ public class DeviceRepository extends BackupHandler {
log.debug("Renumber devices with seed: {}", seedId);
while (deviceIterator.hasNext()) {
theDevice = deviceIterator.next();
theDevice.setId(String.valueOf(nextId));
newValue = nextId % 256;
if (newValue <= 0)
newValue = 1;
else if (newValue > 255)
newValue = 255;
hexValue = HexLibrary.encodeUsingBigIntegerToString(newValue.toString());
if (!theDevice.isLockDeviceId()) {
theDevice.setId(String.valueOf(nextId));
newValue = nextId % 256;
if (newValue <= 0)
newValue = 1;
else if (newValue > 255)
newValue = 255;
hexValue = HexLibrary.encodeUsingBigIntegerToString(newValue.toString());
theDevice.setUniqueid("00:17:88:5E:D3:" + hexValue + "-" + hexValue);
theDevice.setUniqueid("00:17:88:5E:D3:" + hexValue + "-" + hexValue);
nextId++;
}
newdevices.put(theDevice.getId(), theDevice);
nextId++;
}
devices = newdevices;
String jsonValue = gson.toJson(findAll());

View File

@@ -385,6 +385,22 @@ public class DeviceResource {
return deviceRepository.getBackups();
}, new JsonTransformer());
// http://ip_address:port/api/devices/backup/download CORS request
options(API_CONTEXT + "/backup/download", "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/download", "application/json", (request, response) -> {
log.debug("Create download: {}", request.body());
BackupFilename aFilename = new Gson().fromJson(request.body(), BackupFilename.class);
String backupContent = deviceRepository.downloadBackup(aFilename.getFilename());
return backupContent;
}, 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);

View File

@@ -92,4 +92,21 @@ public abstract class BackupHandler {
return theFilenames;
}
public String downloadBackup(String aFilename) {
Path filePath = FileSystems.getDefault().getPath(repositoryPath.getParent().toString(), aFilename);
String content = null;
if (Files.notExists(filePath) || !Files.isReadable(filePath)) {
log.warn("Error reading the file: {} - Does not exist or is not readable. continuing...", aFilename);
return null;
}
try {
content = new String(Files.readAllBytes(filePath));
} catch (IOException e) {
log.error("Error reading the file: {} message: {}", aFilename, e.getMessage(), e);
}
return content;
}
}