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

View file

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

View file

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