mirror of
https://github.com/bwssytems/ha-bridge.git
synced 2025-12-22 09:22:23 +00:00
Finished config up/down load impl and Finished startup action implementation
This commit is contained in:
@@ -437,6 +437,41 @@ public class SystemControl {
|
||||
return stop();
|
||||
});
|
||||
|
||||
// http://ip_address:port/system/devices/backup/download CORS request
|
||||
options(SYSTEM_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 (SYSTEM_CONTEXT + "/backup/download", "application/json", (request, response) -> {
|
||||
log.debug("Create download: {}", request.body());
|
||||
BackupFilename aFilename = new Gson().fromJson(request.body(), BackupFilename.class);
|
||||
String backupContent = bridgeSettings.downloadBackup(aFilename.getFilename());
|
||||
return backupContent;
|
||||
}, new JsonTransformer());
|
||||
|
||||
// http://ip_address:port/system/devices/backup/upload CORS request
|
||||
options(SYSTEM_CONTEXT + "/backup/upload/:filename", "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 (SYSTEM_CONTEXT + "/backup/upload/:filename", "application/json", (request, response) -> {
|
||||
log.debug("Create upload: {} - {}", request.params(":filename"), request.body());
|
||||
String theSuccess = bridgeSettings.uploadBackup(request.params(":filename"), request.body());
|
||||
if(theSuccess.contains("Error:"))
|
||||
response.status(HttpStatus.SC_METHOD_FAILURE);
|
||||
else
|
||||
response.status(HttpStatus.SC_OK);
|
||||
return theSuccess;
|
||||
}, new JsonTransformer());
|
||||
|
||||
// http://ip_address:port/system/backup/available returns a list of config backup filenames
|
||||
get (SYSTEM_CONTEXT + "/backup/available", (request, response) -> {
|
||||
log.debug("Get backup filenames");
|
||||
|
||||
@@ -89,6 +89,9 @@ public class DeviceDescriptor{
|
||||
@SerializedName("lockDeviceId")
|
||||
@Expose
|
||||
private boolean lockDeviceId;
|
||||
@SerializedName("startupActions")
|
||||
@Expose
|
||||
private String startupActions;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
@@ -344,4 +347,12 @@ public class DeviceDescriptor{
|
||||
public void setLockDeviceId(boolean lockDeviceId) {
|
||||
this.lockDeviceId = lockDeviceId;
|
||||
}
|
||||
|
||||
public String getStartupActions() {
|
||||
return startupActions;
|
||||
}
|
||||
|
||||
public void setStartupActions(String startupActions) {
|
||||
this.startupActions = startupActions;
|
||||
}
|
||||
}
|
||||
@@ -202,15 +202,33 @@ 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;
|
||||
List<String> lockedIds = new ArrayList<String>();
|
||||
String hexValue;
|
||||
Integer newValue;
|
||||
DeviceDescriptor theDevice;
|
||||
log.debug("Renumber devices with seed: {}", seedId);
|
||||
boolean findNext = true;
|
||||
|
||||
|
||||
nextId = seedId;
|
||||
while(deviceIterator.hasNext()) {
|
||||
theDevice = deviceIterator.next();
|
||||
if(theDevice.isLockDeviceId()) {
|
||||
lockedIds.add(theDevice.getId());
|
||||
}
|
||||
}
|
||||
log.debug("Renumber devices starting with: {}", nextId);
|
||||
deviceIterator = list.iterator();
|
||||
while (deviceIterator.hasNext()) {
|
||||
theDevice = deviceIterator.next();
|
||||
if (!theDevice.isLockDeviceId()) {
|
||||
findNext = true;
|
||||
while(findNext) {
|
||||
if(lockedIds.contains(String.valueOf(nextId))) {
|
||||
nextId++;
|
||||
} else {
|
||||
findNext = false;
|
||||
}
|
||||
}
|
||||
theDevice.setId(String.valueOf(nextId));
|
||||
newValue = nextId % 256;
|
||||
if (newValue <= 0)
|
||||
|
||||
@@ -41,6 +41,7 @@ import org.slf4j.LoggerFactory;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.awt.Color;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
@@ -77,6 +78,7 @@ public class HueMulator {
|
||||
// This function sets up the sparkjava rest calls for the hue api
|
||||
public void setupServer() {
|
||||
log.info("Hue emulator service started....");
|
||||
startupDeviceCall();
|
||||
before(HUE_CONTEXT + "/*", (request, response) -> {
|
||||
// This currently causes an error with Spark replies
|
||||
// String path = request.pathInfo();
|
||||
@@ -1671,4 +1673,50 @@ public class HueMulator {
|
||||
|
||||
return responseString;
|
||||
}
|
||||
|
||||
private void startupDeviceCall() {
|
||||
String aUserId = bridgeSettingMaster.getBridgeSecurity().createWhitelistUser("test_ha_bridge");
|
||||
List<DeviceDescriptor> deviceList = repository.findAll();
|
||||
String aChangeBody;
|
||||
String[] components;
|
||||
boolean comma = false;
|
||||
|
||||
for (DeviceDescriptor aDevice : deviceList) {
|
||||
if(aDevice.getStartupActions() != null && !aDevice.getStartupActions().isEmpty()) {
|
||||
log.info("Startup call for {} with startupActions {}", aDevice.getName(), aDevice.getStartupActions());
|
||||
aChangeBody = "{";
|
||||
components = aDevice.getStartupActions().split(":");
|
||||
if(components.length > 0 && components[0] != null && components[0].length() > 0) {
|
||||
if(components[0].equals("On")) {
|
||||
aChangeBody = aChangeBody + "\"on\":true";
|
||||
}
|
||||
else {
|
||||
aChangeBody = aChangeBody + "\"on\":false";
|
||||
}
|
||||
comma = true;
|
||||
}
|
||||
if(components.length > 1 && components[1] != null && components[1].length() > 0 && !(components.length > 2 && components[2] != null && components[2].length() > 0)) {
|
||||
if(comma)
|
||||
aChangeBody = aChangeBody + ",";
|
||||
aChangeBody = aChangeBody + "\"bri\":" + components[1];
|
||||
comma = true;
|
||||
}
|
||||
if(components.length > 2 && components[2] != null && components[2].length() > 0) {
|
||||
if(comma)
|
||||
aChangeBody = aChangeBody + ",";
|
||||
String theRGB = components[2].substring(components[2].indexOf('(') + 1, components[2].indexOf(')'));
|
||||
String[] RGB = theRGB.split(",");
|
||||
float[] hsb = new float[3];
|
||||
Color.RGBtoHSB(Integer.parseInt(RGB[0]), Integer.parseInt(RGB[1]), Integer.parseInt(RGB[2]), hsb);
|
||||
float hue = hsb[0] * (float) 360.0;
|
||||
float sat = hsb[1] * (float) 100.0;
|
||||
float bright = hsb[2] * (float) 100.0;
|
||||
aChangeBody = String.format("%s\"hue\":%.2f,\"sat\":%.2f,\"bri\":%d", aChangeBody, hue, sat, Math.round(bright));
|
||||
}
|
||||
aChangeBody = aChangeBody + "}";
|
||||
log.info("Startup call to set state for {} with body {}", aDevice.getName(), aChangeBody);
|
||||
changeState(aUserId, aDevice.getId(), aChangeBody, "localhost", true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user