Switched from JSONObjectStream to Configurator

Former-commit-id: e27c6fd6acf414ad28b8c447f786d30a866060b9
This commit is contained in:
Ziver Koc 2016-01-14 20:03:36 +01:00
parent 45cf8de571
commit fa9223c653
3 changed files with 44 additions and 42 deletions

View file

@ -2,6 +2,7 @@ package se.hal.struct;
import se.hal.intf.HalEvent;
import se.hal.intf.HalEventController;
import se.hal.intf.HalSensor;
import zutil.db.DBConnection;
import zutil.db.bean.DBBean;
import zutil.db.bean.DBBeanSQLResultHandler;
@ -10,6 +11,9 @@ import zutil.io.StringOutputStream;
import zutil.log.LogUtil;
import zutil.parser.json.JSONObjectInputStream;
import zutil.parser.json.JSONObjectOutputStream;
import zutil.parser.json.JSONParser;
import zutil.parser.json.JSONWriter;
import zutil.ui.Configurator;
import java.io.IOException;
import java.sql.PreparedStatement;
@ -49,16 +53,16 @@ public class Event extends DBBean{
public void setEventData(HalEvent eventData){
this.eventData = eventData;
updateConfig();
}
public HalEvent getEventData(){
if(eventData == null) {
if(config !=null && eventData == null) {
try {
Class c = Class.forName(type);
eventData = (HalEvent) c.newInstance();
JSONObjectInputStream in = new JSONObjectInputStream(
new StringInputStream(config));
eventData = (HalEvent) in.readObject(c);
in.close();
Configurator<HalEvent> configurator = new Configurator<>(eventData);
configurator.setValues(JSONParser.read(config));
} catch (Exception e){
logger.log(Level.SEVERE, "Unable to read event data", e);
}
@ -66,22 +70,16 @@ public class Event extends DBBean{
return eventData;
}
public void save(DBConnection db) throws SQLException {
if(eventData != null) {
try {
StringOutputStream buff = new StringOutputStream();
JSONObjectOutputStream out = new JSONObjectOutputStream(buff);
out.enableMetaData(false);
out.writeObject(eventData);
out.close();
this.config = buff.toString();
} catch (IOException e){
logger.log(Level.SEVERE, "Unable to save event data", e);
}
}
if(eventData != null)
updateConfig();
else
this.config = null;
super.save(db);
}
private void updateConfig(){
Configurator<HalEvent> configurator = new Configurator<>(eventData);
this.config = JSONWriter.toString(configurator.getValuesAsNode());
}
public String getName() {
@ -94,14 +92,19 @@ public class Event extends DBBean{
return type;
}
public void setType(String type) {
this.type = type;
if( ! this.type.equals(type)) {
this.type = type;
this.eventData = null; // invalidate current sensor data object
}
}
public String getConfig() {
return config;
}
public void setConfig(String config) {
this.config = config;
this.eventData = null; // invalidate current sensor data object
if( ! this.config.equals(config)) {
this.config = config;
this.eventData = null; // invalidate current sensor data object
}
}
public User getUser() {

View file

@ -12,6 +12,9 @@ import zutil.io.StringOutputStream;
import zutil.log.LogUtil;
import zutil.parser.json.JSONObjectInputStream;
import zutil.parser.json.JSONObjectOutputStream;
import zutil.parser.json.JSONParser;
import zutil.parser.json.JSONWriter;
import zutil.ui.Configurator;
import java.io.IOException;
import java.sql.PreparedStatement;
@ -85,14 +88,13 @@ public class Sensor extends DBBean{
updateConfig();
}
public HalSensor getSensorData(){
if(sensorData == null) {
if(config !=null && sensorData == null) {
try {
Class c = Class.forName(type);
sensorData = (HalSensor) c.newInstance();
JSONObjectInputStream in = new JSONObjectInputStream(
new StringInputStream(config));
sensorData = (HalSensor) in.readObject(c);
in.close();
Configurator<HalSensor> configurator = new Configurator<>(sensorData);
configurator.setValues(JSONParser.read(config));
} catch (Exception e){
logger.log(Level.SEVERE, "Unable to read sensor data", e);
}
@ -107,16 +109,8 @@ public class Sensor extends DBBean{
super.save(db);
}
private void updateConfig(){
try {
StringOutputStream buff = new StringOutputStream();
JSONObjectOutputStream out = new JSONObjectOutputStream(buff);
out.enableMetaData(false);
out.writeObject(sensorData);
out.close();
this.config = buff.toString();
} catch (IOException e){
logger.log(Level.SEVERE, "Unable to save sensor data", e);
}
Configurator<HalSensor> configurator = new Configurator<>(sensorData);
this.config = JSONWriter.toString(configurator.getValuesAsNode());
}
@ -130,14 +124,19 @@ public class Sensor extends DBBean{
return type;
}
public void setType(String type) {
this.type = type;
if( ! this.type.equals(type)) {
this.type = type;
this.sensorData = null; // invalidate current sensor data object
}
}
public String getConfig() {
return config;
}
public void setConfig(String config) {
this.config = config;
this.sensorData = null; // invalidate current sensor data object
if( ! this.config.equals(config)) {
this.config = config;
this.sensorData = null; // invalidate current sensor data object
}
}
public User getUser() {

View file

@ -23,7 +23,7 @@
[
{ y: (Date.now()-24*60*60*1000) },
{{#minData}}
{ y: {{.timestamp}}, {{.username}}: {{.data}} },
{ y: {{.timestamp}}, "{{.username}}": {{.data}} },
{{/minData}}
{ y: Date.now() }
]
@ -32,20 +32,20 @@
[
{ y: (Date.now()-7*24*60*60*1000) },
{{#hourData}}
{ y: {{.timestamp}}, {{.username}}: {{.data}} },
{ y: {{.timestamp}}, "{{.username}}": {{.data}} },
{{/hourData}}
{ y: Date.now() }
]
);
chartData("day-power-chart",
[{{#dayData}}
{ y: {{.timestamp}}, {{.username}}: {{.data}} },
{ y: {{.timestamp}}, "{{.username}}": {{.data}} },
{{/dayData}}
{ y: Date.now() }
]
);
});
var userArray = [ {{#username}} '{{.}}', {{/username}} ];
var userArray = [ {{#username}} "{{.}}", {{/username}} ];
function chartData(elementId, data){
Morris.Line({
element: elementId,