Finished adding dim versus on, utf-8 and track state and return on api

calls.
This commit is contained in:
Admin
2016-03-18 16:38:18 -05:00
parent ad820a68c9
commit 926a7f50dc
15 changed files with 225 additions and 198 deletions

View File

@@ -5,7 +5,7 @@
<groupId>com.bwssystems.HABridge</groupId> <groupId>com.bwssystems.HABridge</groupId>
<artifactId>ha-bridge</artifactId> <artifactId>ha-bridge</artifactId>
<version>1.4.1c</version> <version>1.4.1d</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>HA Bridge</name> <name>HA Bridge</name>

View File

@@ -1,5 +1,7 @@
package com.bwssystems.HABridge.api.hue; package com.bwssystems.HABridge.api.hue;
import com.bwssystems.HABridge.dao.DeviceDescriptor;
/** /**
* Created by arm on 4/14/15. * Created by arm on 4/14/15.
*/ */
@@ -68,18 +70,18 @@ public class DeviceResponse {
this.swversion = swversion; this.swversion = swversion;
} }
public static DeviceResponse createResponse(String name, String id){ public static DeviceResponse createResponse(DeviceDescriptor device){
DeviceState deviceState = new DeviceState(); DeviceState deviceState = new DeviceState();
DeviceResponse response = new DeviceResponse(); DeviceResponse response = new DeviceResponse();
response.setState(deviceState); response.setState(deviceState);
deviceState.setOn(false); deviceState.setOn(device.getDeviceState());
deviceState.setReachable(true); deviceState.setReachable(true);
deviceState.setEffect("none"); deviceState.setEffect("none");
deviceState.setAlert("none"); deviceState.setAlert("none");
deviceState.setBri(254); deviceState.setBri(device.getDeviceSetValue());
response.setName(name); response.setName(device.getName());
response.setUniqueid(id); response.setUniqueid(device.getId());
response.setManufacturername("Philips"); response.setManufacturername("Philips");
response.setType("Dimmable light"); response.setType("Dimmable light");
response.setModelid("LWB004"); response.setModelid("LWB004");

View File

@@ -6,7 +6,7 @@ package com.bwssystems.HABridge.api.hue;
*/ */
public class DeviceState { public class DeviceState {
private boolean on; private boolean on;
private int bri = 255; private int bri = 0;
private String effect; private String effect;
private String alert; private String alert;
private boolean reachable; private boolean reachable;

View File

@@ -1,21 +1,55 @@
package com.bwssystems.HABridge.dao; package com.bwssystems.HABridge.dao;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
/* /*
* Object to handle the device configuration * Object to handle the device configuration
*/ */
public class DeviceDescriptor{ public class DeviceDescriptor{
private String id; @SerializedName("id")
@Expose
private String id;
@SerializedName("name")
@Expose
private String name; private String name;
@SerializedName("mapId")
@Expose
private String mapId; private String mapId;
@SerializedName("mapType")
@Expose
private String mapType; private String mapType;
@SerializedName("deviceType")
@Expose
private String deviceType; private String deviceType;
@SerializedName("targetDevice")
@Expose
private String targetDevice; private String targetDevice;
@SerializedName("offUrl")
@Expose
private String offUrl; private String offUrl;
@SerializedName("dimUrl")
@Expose
private String dimUrl;
@SerializedName("onUrl")
@Expose
private String onUrl; private String onUrl;
@SerializedName("httpVerb")
@Expose
private String httpVerb; private String httpVerb;
@SerializedName("contentType")
@Expose
private String contentType; private String contentType;
@SerializedName("contentBody")
@Expose
private String contentBody; private String contentBody;
@SerializedName("contentBodyOff")
@Expose
private String contentBodyOff; private String contentBodyOff;
private boolean deviceState;
private int deviceSetValue;
public String getName() { public String getName() {
return name; return name;
} }
@@ -64,7 +98,15 @@ public class DeviceDescriptor{
this.offUrl = offUrl; this.offUrl = offUrl;
} }
public String getOnUrl() { public String getDimUrl() {
return dimUrl;
}
public void setDimUrl(String dimUrl) {
this.dimUrl = dimUrl;
}
public String getOnUrl() {
return onUrl; return onUrl;
} }
@@ -111,6 +153,22 @@ public class DeviceDescriptor{
public void setContentBodyOff(String contentBodyOff) { public void setContentBodyOff(String contentBodyOff) {
this.contentBodyOff = contentBodyOff; this.contentBodyOff = contentBodyOff;
} }
public boolean getDeviceState() {
return deviceState;
}
public void setDeviceState(boolean deviceState) {
this.deviceState = deviceState;
}
public int getDeviceSetValue() {
return deviceSetValue;
}
public void setDeviceSetValue(int deviceSetValue) {
this.deviceSetValue = deviceSetValue;
}
} }

View File

@@ -2,7 +2,6 @@ package com.bwssystems.HABridge.dao;
import java.io.IOException; import java.io.IOException;
import java.io.StringReader;
import java.nio.file.FileSystems; import java.nio.file.FileSystems;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
@@ -18,22 +17,26 @@ import org.slf4j.LoggerFactory;
import com.bwssystems.HABridge.dao.DeviceDescriptor; import com.bwssystems.HABridge.dao.DeviceDescriptor;
import com.bwssystems.util.BackupHandler; import com.bwssystems.util.BackupHandler;
import com.bwssystems.util.JsonTransformer; import com.bwssystems.util.JsonTransformer;
import com.google.gson.stream.JsonReader; import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.util.List; import java.util.List;
import java.util.ListIterator;
/* /*
* This is an in memory list to manage the configured devices and saves the list as a JSON string to a file for later * This is an in memory list to manage the configured devices and saves the list as a JSON string to a file for later
* loading. * loading.
*/ */
public class DeviceRepository extends BackupHandler { public class DeviceRepository extends BackupHandler {
Map<String, DeviceDescriptor> devices; private Map<String, DeviceDescriptor> devices;
Path repositoryPath; private Path repositoryPath;
final Random random = new Random(); private Gson gson;
final Logger log = LoggerFactory.getLogger(DeviceRepository.class); final private Random random = new Random();
private Logger log = LoggerFactory.getLogger(DeviceRepository.class);
public DeviceRepository(String deviceDb) { public DeviceRepository(String deviceDb) {
super(); super();
gson =
new GsonBuilder()
.create();
repositoryPath = null; repositoryPath = null;
repositoryPath = Paths.get(deviceDb); repositoryPath = Paths.get(deviceDb);
setupParams(repositoryPath, ".bk", "device.db-"); setupParams(repositoryPath, ".bk", "device.db-");
@@ -50,15 +53,11 @@ public class DeviceRepository extends BackupHandler {
if(jsonContent != null) if(jsonContent != null)
{ {
List<DeviceDescriptor> list = readJsonStream(jsonContent); DeviceDescriptor list[] = gson.fromJson(jsonContent, DeviceDescriptor[].class);
ListIterator<DeviceDescriptor> theIterator = list.listIterator(); for(int i = 0; i < list.length; i++) {
DeviceDescriptor theDevice = null; put(list[i].getId(), list[i]);
while (theIterator.hasNext()) {
theDevice = theIterator.next();
put(theDevice.getId(), theDevice);
} }
} }
} }
public List<DeviceDescriptor> findAll() { public List<DeviceDescriptor> findAll() {
@@ -89,8 +88,7 @@ public class DeviceRepository extends BackupHandler {
put(descriptors[i].getId(), descriptors[i]); put(descriptors[i].getId(), descriptors[i]);
theNames = theNames + " " + descriptors[i].getName() + ", "; theNames = theNames + " " + descriptors[i].getName() + ", ";
} }
JsonTransformer aRenderer = new JsonTransformer(); String jsonValue = gson.toJson(findAll());
String jsonValue = aRenderer.render(findAll());
repositoryWriter(jsonValue, repositoryPath); repositoryWriter(jsonValue, repositoryPath);
log.debug("Save device(s): " + theNames); log.debug("Save device(s): " + theNames);
} }
@@ -153,82 +151,4 @@ public class DeviceRepository extends BackupHandler {
return content; return content;
} }
}
private List<DeviceDescriptor> readJsonStream(String context) {
JsonReader reader = new JsonReader(new StringReader(context));
List<DeviceDescriptor> theDescriptors = null;
try {
theDescriptors = readDescriptorArray(reader);
} catch (IOException e) {
log.error("Error reading json array: " + context + " message: " + e.getMessage(), e);
} finally {
try {
reader.close();
} catch (IOException e) {
log.error("Error closing json reader: " + context + " message: " + e.getMessage(), e);
}
}
return theDescriptors;
}
public List<DeviceDescriptor> readDescriptorArray(JsonReader reader) throws IOException {
List<DeviceDescriptor> descriptors = new ArrayList<DeviceDescriptor>();
reader.beginArray();
while (reader.hasNext()) {
descriptors.add(readDescriptor(reader));
}
reader.endArray();
return descriptors;
}
public DeviceDescriptor readDescriptor(JsonReader reader) throws IOException {
DeviceDescriptor deviceEntry = new DeviceDescriptor();
reader.beginObject();
while (reader.hasNext()) {
String name = reader.nextName();
if (name.equals("id")) {
deviceEntry.setId(reader.nextString());
log.debug("Read a Device - device json id: " + deviceEntry.getId());
} else if (name.equals("name")) {
deviceEntry.setName(reader.nextString());
log.debug("Read a Device - device json name: " + deviceEntry.getName());
} else if (name.equals("mapType")) {
deviceEntry.setMapType(reader.nextString());
log.debug("Read a Device - device json name: " + deviceEntry.getMapType());
} else if (name.equals("mapId")) {
deviceEntry.setMapId(reader.nextString());
log.debug("Read a Device - device json name: " + deviceEntry.getMapId());
} else if (name.equals("deviceType")) {
deviceEntry.setDeviceType(reader.nextString());
log.debug("Read a Device - device json type:" + deviceEntry.getDeviceType());
} else if (name.equals("targetDevice")) {
deviceEntry.setTargetDevice(reader.nextString());
log.debug("Read a Device - device json type:" + deviceEntry.getTargetDevice());
} else if (name.equals("offUrl")) {
deviceEntry.setOffUrl(reader.nextString());
log.debug("Read a Device - device json off URL:" + deviceEntry.getOffUrl());
} else if (name.equals("onUrl")) {
deviceEntry.setOnUrl(reader.nextString());
log.debug("Read a Device - device json on URL:" + deviceEntry.getOnUrl());
} else if (name.equals("httpVerb")) {
deviceEntry.setHttpVerb(reader.nextString());
log.debug("Read a Device - device json httpVerb:" + deviceEntry.getHttpVerb());
} else if (name.equals("contentType")) {
deviceEntry.setContentType(reader.nextString());
log.debug("Read a Device - device json contentType:" + deviceEntry.getContentType());
} else if (name.equals("contentBody")) {
deviceEntry.setContentBody(reader.nextString());
log.debug("Read a Device - device json contentBody:" + deviceEntry.getContentBody());
} else if (name.equals("contentBodyOff")) {
deviceEntry.setContentBodyOff(reader.nextString());
log.debug("Read a Device - device json contentBodyOff:" + deviceEntry.getContentBodyOff());
} else {
reader.skipValue();
}
}
reader.endObject();
return deviceEntry;
}
}

View File

@@ -115,35 +115,24 @@ public class DeviceResource {
put (API_CONTEXT + "/:id", "application/json", (request, response) -> { put (API_CONTEXT + "/:id", "application/json", (request, response) -> {
log.debug("Edit a Device - request body: " + request.body()); log.debug("Edit a Device - request body: " + request.body());
DeviceDescriptor device = new Gson().fromJson(request.body(), DeviceDescriptor.class); DeviceDescriptor device = new Gson().fromJson(request.body(), DeviceDescriptor.class);
DeviceDescriptor deviceEntry = deviceRepository.findOne(request.params(":id")); if(deviceRepository.findOne(request.params(":id")) == null){
if(deviceEntry == null){ log.debug("Could not save an edited device, Device Id not found: " + request.params(":id"));
log.debug("Could not save an edited Device Id: " + request.params(":id"));
response.status(HttpStatus.SC_BAD_REQUEST); response.status(HttpStatus.SC_BAD_REQUEST);
return new ErrorMessage("Could not save an edited Device Id: " + request.params(":id") + " "); return new ErrorMessage("Could not save an edited device, Device Id not found: " + request.params(":id") + " ");
} }
else else
{ {
log.debug("Saving an edited Device: " + deviceEntry.getName()); log.debug("Saving an edited Device: " + device.getName());
deviceEntry.setName(device.getName());
if (device.getDeviceType() != null) if (device.getDeviceType() != null)
deviceEntry.setDeviceType(device.getDeviceType()); device.setDeviceType(device.getDeviceType());
deviceEntry.setMapId(device.getMapId());
deviceEntry.setMapType(device.getMapType());
deviceEntry.setTargetDevice(device.getTargetDevice());
deviceEntry.setOnUrl(device.getOnUrl());
deviceEntry.setOffUrl(device.getOffUrl());
deviceEntry.setHttpVerb(device.getHttpVerb());
deviceEntry.setContentType(device.getContentType());
deviceEntry.setContentBody(device.getContentBody());
deviceEntry.setContentBodyOff(device.getContentBodyOff());
DeviceDescriptor[] theDevices = new DeviceDescriptor[1]; DeviceDescriptor[] theDevices = new DeviceDescriptor[1];
theDevices[0] = deviceEntry; theDevices[0] = device;
deviceRepository.save(theDevices); deviceRepository.save(theDevices);
response.status(HttpStatus.SC_OK); response.status(HttpStatus.SC_OK);
} }
return deviceEntry; return device;
}, new JsonTransformer()); }, new JsonTransformer());
get (API_CONTEXT, "application/json", (request, response) -> { get (API_CONTEXT, "application/json", (request, response) -> {

View File

@@ -101,7 +101,7 @@ public class HueMulator {
List<DeviceDescriptor> deviceList = repository.findAll(); List<DeviceDescriptor> deviceList = repository.findAll();
Map<String, DeviceResponse> deviceResponseMap = new HashMap<>(); Map<String, DeviceResponse> deviceResponseMap = new HashMap<>();
for (DeviceDescriptor device : deviceList) { for (DeviceDescriptor device : deviceList) {
DeviceResponse deviceResponse = DeviceResponse.createResponse(device.getName(), device.getId()); DeviceResponse deviceResponse = DeviceResponse.createResponse(device);
deviceResponseMap.put(device.getId(), deviceResponse); deviceResponseMap.put(device.getId(), deviceResponse);
} }
response.type("application/json; charset=utf-8"); response.type("application/json; charset=utf-8");
@@ -225,7 +225,7 @@ public class HueMulator {
Map<String, DeviceResponse> deviceList = new HashMap<>(); Map<String, DeviceResponse> deviceList = new HashMap<>();
descriptorList.forEach(descriptor -> { descriptorList.forEach(descriptor -> {
DeviceResponse deviceResponse = DeviceResponse.createResponse(descriptor.getName(), descriptor.getId()); DeviceResponse deviceResponse = DeviceResponse.createResponse(descriptor);
deviceList.put(descriptor.getId(), deviceResponse); deviceList.put(descriptor.getId(), deviceResponse);
} }
); );
@@ -249,7 +249,7 @@ public class HueMulator {
} else { } else {
log.debug("found device named: " + device.getName()); log.debug("found device named: " + device.getName());
} }
DeviceResponse lightResponse = DeviceResponse.createResponse(device.getName(), device.getId()); DeviceResponse lightResponse = DeviceResponse.createResponse(device);
response.type("application/json; charset=utf-8"); response.type("application/json; charset=utf-8");
response.status(HttpStatus.SC_OK); response.status(HttpStatus.SC_OK);
@@ -296,28 +296,34 @@ public class HueMulator {
return responseString; return responseString;
} }
if (state.isOn()) { responseString = "[{\"success\":{\"/lights/" + lightId + "/state/on\":";
responseString = "[{\"success\":{\"/lights/" + lightId + "/state/on\":true}}";
url = device.getOnUrl();
} else if (request.body().contains("false")) {
responseString = "[{\"success\":{\"/lights/" + lightId + "/state/on\":false}}";
url = device.getOffUrl();
}
if(request.body().contains("bri")) if(request.body().contains("bri"))
{ {
if(url == null) url = device.getDimUrl();
{
url = device.getOnUrl();
responseString = "[";
}
else
responseString = responseString + ",";
responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/bri\":" + state.getBri() + "}}]"; if(url == null || url.length() == 0)
url = device.getOnUrl();
responseString = responseString + "true}},{\"success\":{\"/lights/" + lightId + "/state/bri\":" + state.getBri() + "}}]";
} }
else else
responseString = responseString + "]"; {
if (state.isOn()) {
responseString = responseString + "true}}]";
url = device.getOnUrl();
state.setBri(255);
} else if (request.body().contains("false")) {
responseString = responseString + "false}}]";
url = device.getOffUrl();
state.setBri(0);
}
}
if (url == null) {
log.warn("Could not find url: " + lightId + " for hue state change request: " + userId + " from " + request.ip() + " body: " + request.body());
responseString = "[{\"error\":{\"type\": 3, \"address\": \"/lights/" + lightId + "\",\"description\": \"Could not find url\", \"resource\": \"/lights/" + lightId + "\"}}]";
return responseString;
}
if(device.getDeviceType().toLowerCase().contains("activity") || (device.getMapType() != null && device.getMapType().equalsIgnoreCase("harmonyActivity"))) if(device.getDeviceType().toLowerCase().contains("activity") || (device.getMapType() != null && device.getMapType().equalsIgnoreCase("harmonyActivity")))
{ {
@@ -337,7 +343,6 @@ public class HueMulator {
else { else {
log.warn("Should not get here, no harmony configured"); log.warn("Should not get here, no harmony configured");
responseString = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId + "\",\"description\": \"Should not get here, no harmony configured\", \"parameter\": \"/lights/" + lightId + "state\"}}]"; responseString = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId + "\",\"description\": \"Should not get here, no harmony configured\", \"parameter\": \"/lights/" + lightId + "state\"}}]";
} }
} }
else if(device.getDeviceType().toLowerCase().contains("button") || (device.getMapType() != null && device.getMapType().equalsIgnoreCase("harmonyButton"))) else if(device.getDeviceType().toLowerCase().contains("button") || (device.getMapType() != null && device.getMapType().equalsIgnoreCase("harmonyButton")))
@@ -456,7 +461,11 @@ public class HueMulator {
responseString = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId + "\",\"description\": \"Error on calling url to change device state\", \"parameter\": \"/lights/" + lightId + "state\"}}]"; responseString = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId + "\",\"description\": \"Error on calling url to change device state\", \"parameter\": \"/lights/" + lightId + "state\"}}]";
} }
} }
if(!responseString.contains("[{\"error\":")) {
device.setDeviceSetValue(state.getBri());
device.setDeviceState(state.isOn());
}
return responseString; return responseString;
}); });
} }

View File

@@ -1,5 +1,7 @@
package com.bwssystems.NestBridge; package com.bwssystems.NestBridge;
import java.io.UnsupportedEncodingException;
public class NestItem { public class NestItem {
private String name; private String name;
private String id; private String id;
@@ -9,7 +11,14 @@ public class NestItem {
return name; return name;
} }
public void setName(String name) { public void setName(String name) {
this.name = name; byte ptext[];
String theLabel = new String(name);
try {
ptext = theLabel.getBytes("ISO-8859-1");
this.name = new String(ptext, "UTF-8");
} catch (UnsupportedEncodingException e) {
this.name = theLabel;
}
} }
public String getId() { public String getId() {
return id; return id;

View File

@@ -1,5 +1,7 @@
package com.bwssystems.harmony; package com.bwssystems.harmony;
import java.io.UnsupportedEncodingException;
import net.whistlingfish.harmony.config.Activity; import net.whistlingfish.harmony.config.Activity;
public class HarmonyActivity { public class HarmonyActivity {
@@ -15,6 +17,14 @@ public class HarmonyActivity {
return activity; return activity;
} }
public void setActivity(Activity activity) { public void setActivity(Activity activity) {
byte ptext[];
String theLabel = activity.getLabel();
try {
ptext = theLabel.getBytes("ISO-8859-1");
activity.setLabel(new String(ptext, "UTF-8"));
} catch (UnsupportedEncodingException e) {
activity.setLabel(theLabel);
}
this.activity = activity; this.activity = activity;
} }

View File

@@ -1,5 +1,7 @@
package com.bwssystems.harmony; package com.bwssystems.harmony;
import java.io.UnsupportedEncodingException;
import net.whistlingfish.harmony.config.Device; import net.whistlingfish.harmony.config.Device;
public class HarmonyDevice { public class HarmonyDevice {
@@ -9,6 +11,14 @@ public class HarmonyDevice {
return device; return device;
} }
public void setDevice(Device device) { public void setDevice(Device device) {
byte ptext[];
String theLabel = device.getLabel();
try {
ptext = theLabel.getBytes("ISO-8859-1");
device.setLabel(new String(ptext, "UTF-8"));
} catch (UnsupportedEncodingException e) {
device.setLabel(theLabel);
}
this.device = device; this.device = device;
} }
public String getHub() { public String getHub() {

View File

@@ -47,27 +47,25 @@ app.service('bridgeService', function ($http, $window, ngToast) {
this.state = {base: window.location.origin + "/api/devices", bridgelocation: window.location.origin, systemsbase: window.location.origin + "/system", huebase: window.location.origin + "/api", configs: [], backups: [], devices: [], device: [], type: "", settings: [], myToastMsg: [], logMsgs: [], loggerInfo: [], olddevicename: "", logShowAll: false, isInControl: false, showVera: false, showHarmony: false, showNest: false, habridgeversion: ""}; this.state = {base: window.location.origin + "/api/devices", bridgelocation: window.location.origin, systemsbase: window.location.origin + "/system", huebase: window.location.origin + "/api", configs: [], backups: [], devices: [], device: [], type: "", settings: [], myToastMsg: [], logMsgs: [], loggerInfo: [], olddevicename: "", logShowAll: false, isInControl: false, showVera: false, showHarmony: false, showNest: false, habridgeversion: ""};
this.displayWarn = function(errorTitle, error) { this.displayWarn = function(errorTitle, error) {
if(error == null || typeof(error) != 'undefined') { var toastContent = errorTitle;
error = {status: 200, statusText: "OK", data: []}; if(error != null && typeof(error) != 'undefined')
error.data = {message: "success"}; toastContent = toastContent + " " + error.data.message + " with status: " + error.statusText + " - " + error.status;
}
ngToast.create({ ngToast.create({
className: "warning", className: "warning",
dismissButton: true, dismissButton: true,
dismissOnTimeout: false, dismissOnTimeout: false,
content: errorTitle + error.data.message + " with status: " + error.statusText + " - " + error.status}); content: toastContent});
}; };
this.displayError = function(errorTitle, error) { this.displayError = function(errorTitle, error) {
if(error == null || typeof(error) != 'undefined') { var toastContent = errorTitle;
error = {status: 200, statusText: "OK", data: []}; if(error != null && typeof(error) != 'undefined')
error.data = {message: "success"}; toastContent = toastContent + " " + error.data.message + " with status: " + error.statusText + " - " + error.status;
}
ngToast.create({ ngToast.create({
className: "danger", className: "danger",
dismissButton: true, dismissButton: true,
dismissOnTimeout: false, dismissOnTimeout: false,
content: errorTitle + error.data.message + " with status: " + error.statusText + " - " + error.status}); content: toastContent});
}; };
this.displayErrorMessage = function(errorTitle, errorMessage) { this.displayErrorMessage = function(errorTitle, errorMessage) {
@@ -101,6 +99,7 @@ app.service('bridgeService', function ($http, $window, ngToast) {
self.state.device.mapId = null; self.state.device.mapId = null;
self.state.device.name = ""; self.state.device.name = "";
self.state.device.onUrl = ""; self.state.device.onUrl = "";
self.state.device.dimUrl = "";
self.state.device.deviceType = "custom"; self.state.device.deviceType = "custom";
self.state.device.targetDevice = null; self.state.device.targetDevice = null;
self.state.device.offUrl = ""; self.state.device.offUrl = "";
@@ -108,7 +107,7 @@ app.service('bridgeService', function ($http, $window, ngToast) {
self.state.device.contentType = null; self.state.device.contentType = null;
self.state.device.contentBody = null; self.state.device.contentBody = null;
self.state.device.contentBodyOff = null; self.state.device.contentBodyOff = null;
self.state.bridge.olddevicename = ""; self.state.olddevicename = "";
}; };
this.getHABridgeVersion = function () { this.getHABridgeVersion = function () {
@@ -303,27 +302,16 @@ app.service('bridgeService', function ($http, $window, ngToast) {
); );
}; };
this.addDevice = function (device) { this.addDevice = function (aDevice) {
var device = {};
angular.extend(device, aDevice );
if(device.httpVerb != null && device.httpVerb != "") if(device.httpVerb != null && device.httpVerb != "")
device.deviceType = "custom"; device.deviceType = "custom";
if(device.targetDevice == null || device.targetDevice == "") if(device.targetDevice == null || device.targetDevice == "")
device.targetDevice = "Encapsulated"; device.targetDevice = "Encapsulated";
if (device.id) { if (device.id) {
var putUrl = this.state.base + "/" + device.id; var putUrl = this.state.base + "/" + device.id;
return $http.put(putUrl, { return $http.put(putUrl, device).then(
id: device.id,
name: device.name,
mapId: device.mapId,
mapType: device.mapType,
deviceType: device.deviceType,
targetDevice: device.targetDevice,
onUrl: device.onUrl,
offUrl: device.offUrl,
httpVerb: device.httpVerb,
contentType: device.contentType,
contentBody: device.contentBody,
contentBodyOff: device.contentBodyOff
}).then(
function (response) { function (response) {
}, },
function (error) { function (error) {
@@ -333,19 +321,7 @@ app.service('bridgeService', function ($http, $window, ngToast) {
} else { } else {
if(device.deviceType == null || device.deviceType == "") if(device.deviceType == null || device.deviceType == "")
device.deviceType = "custom"; device.deviceType = "custom";
return $http.post(this.state.base, { return $http.post(this.state.base, device).then(
name: device.name,
mapId: device.mapId,
mapType: device.mapType,
deviceType: device.deviceType,
targetDevice: device.targetDevice,
onUrl: device.onUrl,
offUrl: device.offUrl,
httpVerb: device.httpVerb,
contentType: device.contentType,
contentBody: device.contentBody,
contentBodyOff: device.contentBodyOff
}).then(
function (response) { function (response) {
}, },
function (error) { function (error) {
@@ -523,11 +499,11 @@ app.service('bridgeService', function ($http, $window, ngToast) {
var msgDescription = "unknown"; var msgDescription = "unknown";
var testUrl = this.state.huebase + "/test/lights/" + device.id + "/state"; var testUrl = this.state.huebase + "/test/lights/" + device.id + "/state";
var testBody = "{\"on\":"; var testBody = "{\"on\":";
if(type == "on") { if(type == "off") {
testBody = testBody + "true"; testBody = testBody + "false";
} }
else { else {
testBody = testBody + "false"; testBody = testBody + "true";
} }
if(value) { if(value) {
testBody = testBody + ",\"bri\":" + value; testBody = testBody + ",\"bri\":" + value;
@@ -683,12 +659,15 @@ app.controller('ViewingController', function ($scope, $location, $http, $window,
$scope.imgBkUrl = "glyphicon glyphicon-plus"; $scope.imgBkUrl = "glyphicon glyphicon-plus";
$scope.testUrl = function (device, type) { $scope.testUrl = function (device, type) {
var dialogNeeded = false; var dialogNeeded = false;
if((type == "on" && (bridgeService.aContainsB(device.onUrl, "${intensity..byte}") || if((type == "on" && (bridgeService.aContainsB(device.onUrl, "${intensity.byte}") ||
bridgeService.aContainsB(device.onUrl, "${intensity.percent}") || bridgeService.aContainsB(device.onUrl, "${intensity.percent}") ||
bridgeService.aContainsB(device.onUrl, "${intensity.math("))) || bridgeService.aContainsB(device.onUrl, "${intensity.math("))) ||
(type == "off" && (bridgeService.aContainsB(device.offUrl, "${intensity..byte}") || (type == "off" && (bridgeService.aContainsB(device.offUrl, "${intensity.byte}") ||
bridgeService.aContainsB(device.offUrl, "${intensity.percent}") || bridgeService.aContainsB(device.offUrl, "${intensity.percent}") ||
bridgeService.aContainsB(device.offUrl, "${intensity.math(")))) { bridgeService.aContainsB(device.offUrl, "${intensity.math("))) ||
(type == "dim" && (bridgeService.aContainsB(device.dimUrl, "${intensity.byte}") ||
bridgeService.aContainsB(device.dimUrl, "${intensity.percent}") ||
bridgeService.aContainsB(device.dimUrl, "${intensity.math(")))) {
$scope.bridge.device = device; $scope.bridge.device = device;
$scope.bridge.type = type; $scope.bridge.type = type;
ngDialog.open({ ngDialog.open({
@@ -792,15 +771,18 @@ app.controller('VeraController', function ($scope, $location, $http, bridgeServi
$scope.device.mapType = "veraDevice"; $scope.device.mapType = "veraDevice";
$scope.device.mapId = veradevice.id; $scope.device.mapId = veradevice.id;
if(dim_control.indexOf("byte") >= 0 || dim_control.indexOf("percent") >= 0 || dim_control.indexOf("math") >= 0) if(dim_control.indexOf("byte") >= 0 || dim_control.indexOf("percent") >= 0 || dim_control.indexOf("math") >= 0)
$scope.device.onUrl = "http://" + veradevice.veraaddress + ":" + $scope.vera.port $scope.device.dimUrl = "http://" + veradevice.veraaddress + ":" + $scope.vera.port
+ "/data_request?id=action&output_format=json&DeviceNum=" + "/data_request?id=action&output_format=json&DeviceNum="
+ veradevice.id + veradevice.id
+ "&serviceId=urn:upnp-org:serviceId:Dimming1&action=SetLoadLevelTarget&newLoadlevelTarget=" + "&serviceId=urn:upnp-org:serviceId:Dimming1&action=SetLoadLevelTarget&newLoadlevelTarget="
+ dim_control; + dim_control;
else else
$scope.device.onUrl = "http://" + veradevice.veraaddress + ":" + $scope.vera.port $scope.device.dimUrl = "http://" + veradevice.veraaddress + ":" + $scope.vera.port
+ "/data_request?id=action&output_format=json&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=1&DeviceNum=" + "/data_request?id=action&output_format=json&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=1&DeviceNum="
+ veradevice.id; + veradevice.id;
$scope.device.onUrl = "http://" + veradevice.veraaddress + ":" + $scope.vera.port
+ "/data_request?id=action&output_format=json&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=1&DeviceNum="
+ veradevice.id;
$scope.device.offUrl = "http://" + veradevice.veraaddress + ":" + $scope.vera.port $scope.device.offUrl = "http://" + veradevice.veraaddress + ":" + $scope.vera.port
+ "/data_request?id=action&output_format=json&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=0&DeviceNum=" + "/data_request?id=action&output_format=json&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=0&DeviceNum="
+ veradevice.id; + veradevice.id;
@@ -999,8 +981,8 @@ app.controller('NestController', function ($scope, $location, $http, bridgeServi
$scope.device.targetDevice = nestitem.location; $scope.device.targetDevice = nestitem.location;
$scope.device.mapType = "nestThermoSet"; $scope.device.mapType = "nestThermoSet";
$scope.device.mapId = nestitem.id + "-SetTemp"; $scope.device.mapId = nestitem.id + "-SetTemp";
$scope.device.onUrl = "{\"name\":\"" + nestitem.id + "\",\"control\":\"temp\",\"temp\":\"${intensity.percent}\"}"; $scope.device.dimUrl = "{\"name\":\"" + nestitem.id + "\",\"control\":\"temp\",\"temp\":\"${intensity.percent}\"}";
$scope.device.offUrl = "{\"name\":\"" + nestitem.id + "\",\"control\":\"temp\",\"temp\":\"${intensity.percent}\"}"; $scope.device.offUrl = "{\"name\":\"" + nestitem.id + "\",\"control\":\"off\"}";
}; };
$scope.buildNestHeatUrls = function (nestitem) { $scope.buildNestHeatUrls = function (nestitem) {
@@ -1090,7 +1072,7 @@ app.controller('EditController', function ($scope, $location, $http, bridgeServi
$scope.device_dim_control = ""; $scope.device_dim_control = "";
$scope.bulk = { devices: [] }; $scope.bulk = { devices: [] };
var veraList = angular.fromJson($scope.bridge.settings.veraaddress); var veraList = angular.fromJson($scope.bridge.settings.veraaddress);
if(veraList != null) if(veraList != null && veraList.length > 0)
$scope.vera = {base: "http://" + veraList.devices[0].ip, port: "3480", id: ""}; $scope.vera = {base: "http://" + veraList.devices[0].ip, port: "3480", id: ""};
else else
$scope.vera = {base: "http://", port: "3480", id: ""}; $scope.vera = {base: "http://", port: "3480", id: ""};
@@ -1110,15 +1092,18 @@ app.controller('EditController', function ($scope, $location, $http, bridgeServi
$scope.device.mapType = "veraDevice"; $scope.device.mapType = "veraDevice";
$scope.device.mapId = $scope.vera.id; $scope.device.mapId = $scope.vera.id;
if(dim_control.indexOf("byte") >= 0 || dim_control.indexOf("percent") >= 0 || dim_control.indexOf("math") >= 0) if(dim_control.indexOf("byte") >= 0 || dim_control.indexOf("percent") >= 0 || dim_control.indexOf("math") >= 0)
$scope.device.onUrl = $scope.vera.base + ":" + $scope.vera.port $scope.device.dimUrl = $scope.vera.base + ":" + $scope.vera.port
+ "/data_request?id=action&output_format=json&DeviceNum=" + "/data_request?id=action&output_format=json&DeviceNum="
+ $scope.vera.id + $scope.vera.id
+ "&serviceId=urn:upnp-org:serviceId:Dimming1&action=SetLoadLevelTarget&newLoadlevelTarget=" + "&serviceId=urn:upnp-org:serviceId:Dimming1&action=SetLoadLevelTarget&newLoadlevelTarget="
+ dim_control; + dim_control;
else else
$scope.device.onUrl = $scope.vera.base + ":" + $scope.vera.port $scope.device.dimUrl = $scope.vera.base + ":" + $scope.vera.port
+ "/data_request?id=action&output_format=json&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=1&DeviceNum=" + "/data_request?id=action&output_format=json&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=1&DeviceNum="
+ $scope.vera.id; + $scope.vera.id;
$scope.device.onUrl = $scope.vera.base + ":" + $scope.vera.port
+ "/data_request?id=action&output_format=json&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=1&DeviceNum="
+ $scope.vera.id;
$scope.device.offUrl = $scope.vera.base + ":" + $scope.vera.port $scope.device.offUrl = $scope.vera.base + ":" + $scope.vera.port
+ "/data_request?id=action&output_format=json&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=0&DeviceNum=" + "/data_request?id=action&output_format=json&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=0&DeviceNum="
+ $scope.vera.id; + $scope.vera.id;

View File

@@ -36,6 +36,8 @@
<p> <p>
<button class="btn btn-info" type="submit" <button class="btn btn-info" type="submit"
ng-click="testUrl(device, 'on')">Test ON</button> ng-click="testUrl(device, 'on')">Test ON</button>
<button class="btn btn-info" type="submit"
ng-click="testUrl(device, 'dim')">Test Dim</button>
<button class="btn btn-info" type="submit" <button class="btn btn-info" type="submit"
ng-click="testUrl(device, 'off')">Test OFF</button> ng-click="testUrl(device, 'off')">Test OFF</button>
<button class="btn btn-warning" type="submit" <button class="btn btn-warning" type="submit"

View File

@@ -83,6 +83,17 @@
</div> </div>
</div> </div>
</div> </div>
<div class="form-group">
<div class="row">
<label class="col-xs-12 col-sm-2 control-label" for="device-dim-url">Dim
URL </label>
<div class="col-xs-8 col-sm-7">
<textarea rows="3" class="form-control" id="device-dim-url"
ng-model="device.dimUrl" placeholder="URL to dim device"></textarea>
</div>
</div>
</div>
<div class="form-group"> <div class="form-group">
<div class="row"> <div class="row">
<label class="col-xs-12 col-sm-2 control-label" <label class="col-xs-12 col-sm-2 control-label"

View File

@@ -107,6 +107,17 @@
Clear Device</button> Clear Device</button>
</div> </div>
</div> </div>
<div class="form-group">
<div class="row">
<label class="col-xs-12 col-sm-2 control-label" for="device-dim-url">Dim
URL </label>
<div class="col-xs-8 col-sm-7">
<textarea rows="3" class="form-control" id="device-dim-url"
ng-model="device.dimUrl" placeholder="URL to dim device"></textarea>
</div>
</div>
</div>
<div class="form-group"> <div class="form-group">
<div class="row"> <div class="row">
<label class="col-xs-12 col-sm-2 control-label" <label class="col-xs-12 col-sm-2 control-label"

View File

@@ -130,6 +130,17 @@
<button class="btn btn-danger" ng-click="clearDevice()"> <button class="btn btn-danger" ng-click="clearDevice()">
Clear Device</button> Clear Device</button>
</div> </div>
<div class="form-group">
<div class="row">
<label class="col-xs-12 col-sm-2 control-label" for="device-dim-url">Dim
URL </label>
<div class="col-xs-8 col-sm-7">
<textarea rows="3" class="form-control" id="device-dim-url"
ng-model="device.dimUrl" placeholder="URL to dim device"></textarea>
</div>
</div>
</div>
<div class="form-group"> <div class="form-group">
<div class="row"> <div class="row">
<label class="col-xs-12 col-sm-2 control-label" <label class="col-xs-12 col-sm-2 control-label"