Skip to content

Commit

Permalink
Finished adding dim versus on, utf-8 and track state and return on api
Browse files Browse the repository at this point in the history
calls.
  • Loading branch information
bwssytems committed Mar 18, 2016
1 parent ad820a6 commit 926a7f5
Show file tree
Hide file tree
Showing 15 changed files with 224 additions and 197 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.4.1c</version>
<version>1.4.1d</version>
<packaging>jar</packaging>

<name>HA Bridge</name>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.bwssystems.HABridge.api.hue;

import com.bwssystems.HABridge.dao.DeviceDescriptor;

/**
* Created by arm on 4/14/15.
*/
Expand Down Expand Up @@ -68,18 +70,18 @@ public void setSwversion(String swversion) {
this.swversion = swversion;
}

public static DeviceResponse createResponse(String name, String id){
public static DeviceResponse createResponse(DeviceDescriptor device){
DeviceState deviceState = new DeviceState();
DeviceResponse response = new DeviceResponse();
response.setState(deviceState);
deviceState.setOn(false);
deviceState.setOn(device.getDeviceState());
deviceState.setReachable(true);
deviceState.setEffect("none");
deviceState.setAlert("none");
deviceState.setBri(254);
deviceState.setBri(device.getDeviceSetValue());

response.setName(name);
response.setUniqueid(id);
response.setName(device.getName());
response.setUniqueid(device.getId());
response.setManufacturername("Philips");
response.setType("Dimmable light");
response.setModelid("LWB004");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
public class DeviceState {
private boolean on;
private int bri = 255;
private int bri = 0;
private String effect;
private String alert;
private boolean reachable;
Expand Down
62 changes: 60 additions & 2 deletions src/main/java/com/bwssystems/HABridge/dao/DeviceDescriptor.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,55 @@
package com.bwssystems.HABridge.dao;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

/*
* Object to handle the device configuration
*/
public class DeviceDescriptor{
private String id;
@SerializedName("id")
@Expose
private String id;
@SerializedName("name")
@Expose
private String name;
@SerializedName("mapId")
@Expose
private String mapId;
@SerializedName("mapType")
@Expose
private String mapType;
@SerializedName("deviceType")
@Expose
private String deviceType;
@SerializedName("targetDevice")
@Expose
private String targetDevice;
@SerializedName("offUrl")
@Expose
private String offUrl;
@SerializedName("dimUrl")
@Expose
private String dimUrl;
@SerializedName("onUrl")
@Expose
private String onUrl;
@SerializedName("httpVerb")
@Expose
private String httpVerb;
@SerializedName("contentType")
@Expose
private String contentType;
@SerializedName("contentBody")
@Expose
private String contentBody;
@SerializedName("contentBodyOff")
@Expose
private String contentBodyOff;

private boolean deviceState;
private int deviceSetValue;

public String getName() {
return name;
}
Expand Down Expand Up @@ -64,7 +98,15 @@ public void setOffUrl(String 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;
}

Expand Down Expand Up @@ -111,6 +153,22 @@ public String getContentBodyOff() {
public void setContentBodyOff(String 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;
}


}
112 changes: 16 additions & 96 deletions src/main/java/com/bwssystems/HABridge/dao/DeviceRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@


import java.io.IOException;
import java.io.StringReader;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -18,22 +17,26 @@
import com.bwssystems.HABridge.dao.DeviceDescriptor;
import com.bwssystems.util.BackupHandler;
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.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
* loading.
*/
public class DeviceRepository extends BackupHandler {
Map<String, DeviceDescriptor> devices;
Path repositoryPath;
final Random random = new Random();
final Logger log = LoggerFactory.getLogger(DeviceRepository.class);
private Map<String, DeviceDescriptor> devices;
private Path repositoryPath;
private Gson gson;
final private Random random = new Random();
private Logger log = LoggerFactory.getLogger(DeviceRepository.class);

public DeviceRepository(String deviceDb) {
super();
gson =
new GsonBuilder()
.create();
repositoryPath = null;
repositoryPath = Paths.get(deviceDb);
setupParams(repositoryPath, ".bk", "device.db-");
Expand All @@ -50,15 +53,11 @@ private void _loadRepository(Path aPath){

if(jsonContent != null)
{
List<DeviceDescriptor> list = readJsonStream(jsonContent);
ListIterator<DeviceDescriptor> theIterator = list.listIterator();
DeviceDescriptor theDevice = null;
while (theIterator.hasNext()) {
theDevice = theIterator.next();
put(theDevice.getId(), theDevice);
DeviceDescriptor list[] = gson.fromJson(jsonContent, DeviceDescriptor[].class);
for(int i = 0; i < list.length; i++) {
put(list[i].getId(), list[i]);
}
}

}
}

public List<DeviceDescriptor> findAll() {
Expand Down Expand Up @@ -89,8 +88,7 @@ public void save(DeviceDescriptor[] descriptors) {
put(descriptors[i].getId(), descriptors[i]);
theNames = theNames + " " + descriptors[i].getName() + ", ";
}
JsonTransformer aRenderer = new JsonTransformer();
String jsonValue = aRenderer.render(findAll());
String jsonValue = gson.toJson(findAll());
repositoryWriter(jsonValue, repositoryPath);
log.debug("Save device(s): " + theNames);
}
Expand Down Expand Up @@ -153,82 +151,4 @@ private String repositoryReader(Path filePath) {

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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,35 +115,24 @@ private void setupEndpoints() {
put (API_CONTEXT + "/:id", "application/json", (request, response) -> {
log.debug("Edit a Device - request body: " + request.body());
DeviceDescriptor device = new Gson().fromJson(request.body(), DeviceDescriptor.class);
DeviceDescriptor deviceEntry = deviceRepository.findOne(request.params(":id"));
if(deviceEntry == null){
log.debug("Could not save an edited Device Id: " + request.params(":id"));
if(deviceRepository.findOne(request.params(":id")) == null){
log.debug("Could not save an edited device, Device Id not found: " + request.params(":id"));
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
{
log.debug("Saving an edited Device: " + deviceEntry.getName());
log.debug("Saving an edited Device: " + device.getName());

deviceEntry.setName(device.getName());
if (device.getDeviceType() != null)
deviceEntry.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());
device.setDeviceType(device.getDeviceType());

DeviceDescriptor[] theDevices = new DeviceDescriptor[1];
theDevices[0] = deviceEntry;
theDevices[0] = device;
deviceRepository.save(theDevices);
response.status(HttpStatus.SC_OK);
}
return deviceEntry;
return device;
}, new JsonTransformer());

get (API_CONTEXT, "application/json", (request, response) -> {
Expand Down
Loading

0 comments on commit 926a7f5

Please sign in to comment.