mirror of
https://github.com/bwssytems/ha-bridge.git
synced 2025-12-16 18:24:36 +00:00
Added persistence for configuration
This commit is contained in:
5
pom.xml
5
pom.xml
@@ -43,6 +43,11 @@
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>2.6.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.cedarsoftware</groupId>
|
||||
<artifactId>json-io</artifactId>
|
||||
<version>4.1.6</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -1,24 +1,48 @@
|
||||
package com.bwssytems.HABridge.dao;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.bwssytems.HABridge.JsonTransformer;
|
||||
import com.bwssytems.HABridge.dao.DeviceDescriptor;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
/*
|
||||
* This is an in memory list to manage the configured devices.
|
||||
*
|
||||
* 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 {
|
||||
Map<String, DeviceDescriptor> devices;
|
||||
final Random random = new Random();
|
||||
final String repositoryPath = "device.db";
|
||||
final Logger log = LoggerFactory.getLogger(DeviceRepository.class);
|
||||
|
||||
public DeviceRepository() {
|
||||
super();
|
||||
String jsonContent = repositoryReader(repositoryPath);
|
||||
devices = new HashMap<String, DeviceDescriptor>();
|
||||
if(jsonContent != null)
|
||||
{
|
||||
List<DeviceDescriptor> list = readJsonStream(jsonContent);
|
||||
ListIterator<DeviceDescriptor> theIterator = list.listIterator();
|
||||
DeviceDescriptor theDevice = null;
|
||||
while (theIterator.hasNext()) {
|
||||
theDevice = theIterator.next();
|
||||
put(Integer.parseInt(theDevice.getId()), theDevice);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public List<DeviceDescriptor> findAll() {
|
||||
@@ -32,23 +56,131 @@ public class DeviceRepository {
|
||||
}
|
||||
|
||||
public DeviceDescriptor findOne(String id) {
|
||||
return devices.get(id);
|
||||
|
||||
return devices.get(id);
|
||||
}
|
||||
|
||||
private void put(int id, DeviceDescriptor aDescriptor) {
|
||||
devices.put(String.valueOf(id),aDescriptor);
|
||||
}
|
||||
|
||||
public void save(DeviceDescriptor aDescriptor) {
|
||||
int id = random.nextInt(Integer.MAX_VALUE);
|
||||
aDescriptor.setId(String.valueOf(id));
|
||||
devices.put(String.valueOf(id),aDescriptor);
|
||||
put(id, aDescriptor);
|
||||
JsonTransformer aRenderer = new JsonTransformer();
|
||||
String jsonValue = aRenderer.render(findAll());
|
||||
repositoryWriter(jsonValue, repositoryPath);
|
||||
}
|
||||
|
||||
public String delete(DeviceDescriptor aDescriptor) {
|
||||
if (aDescriptor != null) {
|
||||
devices.remove(aDescriptor.getId());
|
||||
JsonTransformer aRenderer = new JsonTransformer();
|
||||
String jsonValue = aRenderer.render(findAll());
|
||||
repositoryWriter(jsonValue, repositoryPath);
|
||||
return "Device with id '" + aDescriptor.getId() + "' deleted";
|
||||
} else {
|
||||
return "Device not found";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void repositoryWriter(String content, String filePath) {
|
||||
FileWriter writer = null;
|
||||
|
||||
try {
|
||||
writer = new FileWriter(filePath, false);
|
||||
writer.write(content);
|
||||
} catch (IOException e) {
|
||||
log.error("Error writing the file: " + filePath + " message: " + e.getMessage(), e);
|
||||
} finally {
|
||||
if (writer != null) {
|
||||
try {
|
||||
writer.close();
|
||||
} catch (IOException e) {
|
||||
log.error("Error closing the file (w): " + filePath + " message: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String repositoryReader(String filePath) {
|
||||
FileReader reader = null;
|
||||
BufferedReader br = null;
|
||||
String content = null;
|
||||
try {
|
||||
reader = new FileReader(filePath);
|
||||
br = new BufferedReader(reader);
|
||||
content = br.readLine();
|
||||
} catch (IOException e) {
|
||||
log.error("Error reading the file: " + filePath + " message: " + e.getMessage(), e);
|
||||
} finally {
|
||||
if (reader != null) {
|
||||
try {
|
||||
br.close();
|
||||
reader.close();
|
||||
} catch (IOException e) {
|
||||
log.error("Error closing the file (r): " + filePath + " message: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
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("deviceType")) {
|
||||
deviceEntry.setDeviceType(reader.nextString());
|
||||
log.debug("Read a Device - device json type:" + deviceEntry.getDeviceType());
|
||||
} 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 {
|
||||
reader.skipValue();
|
||||
}
|
||||
}
|
||||
reader.endObject();
|
||||
return deviceEntry;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user