diff --git a/pom.xml b/pom.xml index bcbaba2..b765b40 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ com.bwssystems.HABridge ha-bridge - 5.2.next_c + 5.2.next_d jar HA Bridge diff --git a/src/main/java/com/bwssystems/HABridge/dao/DeviceDescriptor.java b/src/main/java/com/bwssystems/HABridge/dao/DeviceDescriptor.java index cef95c6..63dff65 100644 --- a/src/main/java/com/bwssystems/HABridge/dao/DeviceDescriptor.java +++ b/src/main/java/com/bwssystems/HABridge/dao/DeviceDescriptor.java @@ -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; + } } \ No newline at end of file diff --git a/src/main/java/com/bwssystems/HABridge/dao/DeviceRepository.java b/src/main/java/com/bwssystems/HABridge/dao/DeviceRepository.java index 9755138..4442ade 100644 --- a/src/main/java/com/bwssystems/HABridge/dao/DeviceRepository.java +++ b/src/main/java/com/bwssystems/HABridge/dao/DeviceRepository.java @@ -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 list = new ArrayList(devices.values()); Iterator deviceIterator = list.iterator(); Map newdevices = new HashMap(); - ; + 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()); diff --git a/src/main/java/com/bwssystems/HABridge/devicemanagmeent/DeviceResource.java b/src/main/java/com/bwssystems/HABridge/devicemanagmeent/DeviceResource.java index 692925b..169312b 100644 --- a/src/main/java/com/bwssystems/HABridge/devicemanagmeent/DeviceResource.java +++ b/src/main/java/com/bwssystems/HABridge/devicemanagmeent/DeviceResource.java @@ -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); diff --git a/src/main/java/com/bwssystems/HABridge/util/BackupHandler.java b/src/main/java/com/bwssystems/HABridge/util/BackupHandler.java index 6d9b33c..f2d3f68 100644 --- a/src/main/java/com/bwssystems/HABridge/util/BackupHandler.java +++ b/src/main/java/com/bwssystems/HABridge/util/BackupHandler.java @@ -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; + } } diff --git a/src/main/resources/public/scripts/app.js b/src/main/resources/public/scripts/app.js index 0a70136..ce9e01a 100644 --- a/src/main/resources/public/scripts/app.js +++ b/src/main/resources/public/scripts/app.js @@ -1279,6 +1279,22 @@ app.service('bridgeService', function ($rootScope, $http, $base64, $location, ng ); }; + this.downloadBackup = function (afilename) { + return $http.put(this.state.base + "/backup/download", { + filename: afilename + }).then( + function (response) { + self.state.backupContent = response.data; + }, + function (error) { + if (error.status === 401) + $rootScope.$broadcast('securityReinit', 'done'); + else + self.displayWarn("Download Backup Db File Error:", error); + } + ); + }; + this.checkForBridge = function () { return $http.get(this.state.bridgelocation + "/description.xml").then( function (response) { @@ -2256,6 +2272,7 @@ app.controller('ViewingController', function ($scope, $location, bridgeService, bridgeService.viewBackups(); $scope.bridge = bridgeService.state; $scope.optionalbackupname = ""; + $scope.bridge.backupContent = undefined; $scope.visible = false; $scope.imgUrl = "glyphicon glyphicon-plus"; $scope.visibleBk = false; @@ -2320,6 +2337,9 @@ app.controller('ViewingController', function ($scope, $location, bridgeService, $scope.deleteBackup = function (backupname) { bridgeService.deleteBackup(backupname); }; + $scope.downloadBackup = function (backupname) { + bridgeService.downloadBackup(backupname); + }; $scope.toggle = function () { $scope.visible = !$scope.visible; if ($scope.visible) diff --git a/src/main/resources/public/views/configuration.html b/src/main/resources/public/views/configuration.html index d3c6f3d..4fe5520 100644 --- a/src/main/resources/public/views/configuration.html +++ b/src/main/resources/public/views/configuration.html @@ -1,4 +1,3 @@ - -
-
-
-

Current devices ({{bridge.devices.length}})

-
-
+
+
+
+

Current devices ({{bridge.devices.length}})

+
+
- + - Must contain filter + ng-model="bridge.state.filterDevicesByIpAddress" placeholder=""> + Must contain filter - +
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
RowIDNameDescriptionDevice StateTypeTargetInactiveNo StateActions
{{$index+1}}{{device.id}}{{device.name}}{{device.description}}on={{device.deviceState.on}},bri={{device.deviceState.bri}},hue={{device.deviceState.hue}},sat={{device.deviceState.sat}},effect={{device.deviceState.effect}},ct={{device.deviceState.ct}},alert={{device.deviceState.alert}},colormode={{device.deviceState.colormode}},reachable={{device.deviceState.reachable}},XYList={{device.deviceState.xy}}{{device.deviceType}}{{device.targetDevice}}{{device.inactive}}{{device.noState}} -

- - - - - - -

-
-
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
RowIDNameDescriptionDevice StateTypeTargetInactiveNo StateActions
{{$index+1}}{{device.id}}{{device.name}}{{device.description}} + on={{device.deviceState.on}},bri={{device.deviceState.bri}},hue={{device.deviceState.hue}},sat={{device.deviceState.sat}},effect={{device.deviceState.effect}},ct={{device.deviceState.ct}},alert={{device.deviceState.alert}},colormode={{device.deviceState.colormode}},reachable={{device.deviceState.reachable}},XYList={{device.deviceState.xy}} + {{device.deviceType}}{{device.targetDevice}}{{device.inactive}}{{device.noState}} +

+ + + + + + +

+
+
+
-

- Bridge Device DB Backup + Bridge Device DB Backup

@@ -123,11 +125,10 @@ File Name
- +
-
@@ -139,12 +140,10 @@ - {{backup}} + - - + + @@ -152,7 +151,7 @@
+ \ No newline at end of file diff --git a/src/main/resources/public/views/editdevice.html b/src/main/resources/public/views/editdevice.html index 34e3ea9..0edc9ae 100644 --- a/src/main/resources/public/views/editdevice.html +++ b/src/main/resources/public/views/editdevice.html @@ -78,6 +78,12 @@ + + + {{device.lockDeviceId}} +