diff --git a/src/se/hal/struct/Event.java b/src/se/hal/struct/Event.java index 5fb82de0..9043097f 100755 --- a/src/se/hal/struct/Event.java +++ b/src/se/hal/struct/Event.java @@ -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 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 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() { diff --git a/src/se/hal/struct/Sensor.java b/src/se/hal/struct/Sensor.java index 7317178e..a90fbe6d 100755 --- a/src/se/hal/struct/Sensor.java +++ b/src/se/hal/struct/Sensor.java @@ -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 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 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() { diff --git a/web-resource/pc_overview.tmpl b/web-resource/pc_overview.tmpl index b13f9096..4da333be 100755 --- a/web-resource/pc_overview.tmpl +++ b/web-resource/pc_overview.tmpl @@ -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,