diff --git a/src/se/koc/hal/PowerChallenge.java b/src/se/koc/hal/PowerChallenge.java index 9a3b1cad..964dd416 100755 --- a/src/se/koc/hal/PowerChallenge.java +++ b/src/se/koc/hal/PowerChallenge.java @@ -5,6 +5,7 @@ import se.koc.hal.deamon.DataAggregatorDaemon; import se.koc.hal.deamon.DataSynchronizationClient; import se.koc.hal.deamon.DataSynchronizationDaemon; import se.koc.hal.deamon.HalDaemon; +import se.koc.hal.page.HalHttpPage; import se.koc.hal.page.PCConfigureHttpPage; import se.koc.hal.page.PCHeatMapHttpPage; import se.koc.hal.page.PCOverviewHttpPage; @@ -24,6 +25,7 @@ public class PowerChallenge { private static HalDaemon[] daemons; + private static HalHttpPage[] pages; public static void main(String[] args) throws Exception { // init logging @@ -47,12 +49,18 @@ public class PowerChallenge { for(HalDaemon daemon : daemons){ daemon.initiate(daemonTimer); } - + + pages = new HalHttpPage[]{ + new PCOverviewHttpPage(), + new PCHeatMapHttpPage(), + new PCConfigureHttpPage() + }; HttpServer http = new HttpServer(HalContext.getIntegerProperty("http_port")); http.setDefaultPage(new HttpFilePage(FileUtil.find("web-resource/"))); - http.setPage("/", new PCOverviewHttpPage()); - http.setPage("/configure", new PCConfigureHttpPage()); - http.setPage("/heatmap", new PCHeatMapHttpPage()); + http.setPage("/", pages[0]); + for(HalHttpPage page : pages){ + http.setPage(page.getURL(), page); + } http.start(); } } diff --git a/src/se/koc/hal/page/HalHttpPage.java b/src/se/koc/hal/page/HalHttpPage.java new file mode 100755 index 00000000..553594d0 --- /dev/null +++ b/src/se/koc/hal/page/HalHttpPage.java @@ -0,0 +1,70 @@ +package se.koc.hal.page; + +import se.koc.hal.HalContext; +import se.koc.hal.struct.Sensor; +import se.koc.hal.struct.User; +import zutil.db.DBConnection; +import zutil.io.file.FileUtil; +import zutil.net.http.HttpHeaderParser; +import zutil.net.http.HttpPage; +import zutil.net.http.HttpPrintStream; +import zutil.parser.Templator; + +import java.io.IOException; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Map; + +/** + * Created by Ziver on 2015-12-10. + */ +public abstract class HalHttpPage implements HttpPage{ + + private static ArrayList pages = new ArrayList<>(); + + private final String name; + private final String id; + + public HalHttpPage(String name, String id){ + this.name = name; + this.id = id; + pages.add(this); + } + + public String getName(){ + return name; + } + public String getId(){ + return id; + } + public String getURL(){ + return "/" + this.id; + } + + + @Override + public void respond(HttpPrintStream out, HttpHeaderParser client_info, + Map session, Map cookie, + Map request) throws IOException { + + try { + DBConnection db = HalContext.getDB(); + + Templator tmpl = new Templator(FileUtil.find("web-resource/index.tmpl")); + tmpl.set("user", User.getLocalUser(db)); + tmpl.set("navigation", pages); + tmpl.set("content", httpRespond(session, cookie, request)); + out.print(tmpl.compile()); + + } catch (Exception e) { + throw new IOException(e); + } + } + + + public abstract Templator httpRespond( + Map session, + Map cookie, + Map request) + throws Exception; +} diff --git a/src/se/koc/hal/page/PCConfigureHttpPage.java b/src/se/koc/hal/page/PCConfigureHttpPage.java index 4342d558..dc9d2ad3 100755 --- a/src/se/koc/hal/page/PCConfigureHttpPage.java +++ b/src/se/koc/hal/page/PCConfigureHttpPage.java @@ -14,26 +14,30 @@ import zutil.net.http.HttpPage; import zutil.net.http.HttpPrintStream; import zutil.parser.Templator; -public class PCConfigureHttpPage implements HttpPage { +public class PCConfigureHttpPage extends HalHttpPage { + + public PCConfigureHttpPage() { + super("Configuration", "config"); + } + + @Override + public Templator httpRespond( + Map session, + Map cookie, + Map request) + throws Exception{ - @Override - public void respond(HttpPrintStream out, HttpHeaderParser client_info, - Map session, Map cookie, - Map request) throws IOException { - try { DBConnection db = HalContext.getDB(); - Templator tmpl = new Templator(FileUtil.find("web-resource/configure.html")); + Templator tmpl = new Templator(FileUtil.find("web-resource/configure.tmpl")); tmpl.set("user", User.getLocalUser(db)); tmpl.set("localSensor", Sensor.getLocalSensors(db)); tmpl.set("extUsers", User.getExternalUsers(db)); tmpl.set("extSensor", Sensor.getExternalSensors(db)); - out.print(tmpl.compile()); - } catch (SQLException e) { - throw new IOException(e); - } + return tmpl; + } } diff --git a/src/se/koc/hal/page/PCHeatMapHttpPage.java b/src/se/koc/hal/page/PCHeatMapHttpPage.java index 77b002b1..3a1bdd6e 100755 --- a/src/se/koc/hal/page/PCHeatMapHttpPage.java +++ b/src/se/koc/hal/page/PCHeatMapHttpPage.java @@ -9,15 +9,21 @@ import zutil.net.http.HttpPage; import zutil.net.http.HttpPrintStream; import zutil.parser.Templator; -public class PCHeatMapHttpPage implements HttpPage { +public class PCHeatMapHttpPage extends HalHttpPage { + + public PCHeatMapHttpPage() { + super("Heatmap", "map"); + } @Override - public void respond(HttpPrintStream out, HttpHeaderParser client_info, - Map session, Map cookie, - Map request) throws IOException { + public Templator httpRespond( + Map session, + Map cookie, + Map request) + throws Exception{ - Templator tmpl = new Templator(FileUtil.find("web-resource/heatmap.html")); - out.print(tmpl.compile()); + Templator tmpl = new Templator(FileUtil.find("web-resource/heatmap.tmpl")); + return tmpl; } } diff --git a/src/se/koc/hal/page/PCOverviewHttpPage.java b/src/se/koc/hal/page/PCOverviewHttpPage.java index 79f8e5bb..22f8dc9a 100755 --- a/src/se/koc/hal/page/PCOverviewHttpPage.java +++ b/src/se/koc/hal/page/PCOverviewHttpPage.java @@ -18,75 +18,80 @@ import zutil.net.http.HttpPage; import zutil.net.http.HttpPrintStream; import zutil.parser.Templator; -public class PCOverviewHttpPage implements HttpPage { +public class PCOverviewHttpPage extends HalHttpPage { + + public PCOverviewHttpPage() { + super("Overview", "overview"); + } + + @Override + public Templator httpRespond( + Map session, + Map cookie, + Map request) + throws Exception{ + + + DBConnection db = HalContext.getDB(); + + PreparedStatement stmt = db.getPreparedStatement( + "SELECT user.username as username," + + " sensor_data_aggr.timestamp_start as timestamp_start," + + " sensor_data_aggr.timestamp_end as timestamp_end," + + " sensor_data_aggr.data as data," + + " sensor_data_aggr.confidence as confidence," + + DataAggregatorDaemon.FIVE_MINUTES_IN_MS + " as period_length" + + " FROM sensor_data_aggr, user, sensor" + + " WHERE sensor.id = sensor_data_aggr.sensor_id" + + " AND user.id = sensor.user_id" + + " AND timestamp_end-timestamp_start == ?" + + " AND timestamp_start > ?" + + " ORDER BY timestamp_start ASC"); + stmt.setLong(1, DataAggregatorDaemon.FIVE_MINUTES_IN_MS-1); + stmt.setLong(2, (System.currentTimeMillis() - DataAggregatorDaemon.DAY_IN_MS) ); + ArrayList minDataList = DBConnection.exec(stmt , new SQLPowerDataBuilder()); + + stmt = db.getPreparedStatement( + "SELECT user.username as username," + + " sensor_data_aggr.timestamp_start as timestamp_start," + + " sensor_data_aggr.timestamp_end as timestamp_end," + + " sensor_data_aggr.data as data," + + " sensor_data_aggr.confidence as confidence," + + DataAggregatorDaemon.HOUR_IN_MS + " as period_length" + + " FROM sensor_data_aggr, user, sensor" + + " WHERE sensor.id = sensor_data_aggr.sensor_id" + + " AND user.id = sensor.user_id" + + " AND timestamp_end-timestamp_start == ?" + + " AND timestamp_start > ?" + + " ORDER BY timestamp_start ASC"); + stmt.setLong(1, DataAggregatorDaemon.HOUR_IN_MS-1); + stmt.setLong(2, (System.currentTimeMillis() - 3*DataAggregatorDaemon.DAY_IN_MS) ); + ArrayList hourDataList = DBConnection.exec(stmt, new SQLPowerDataBuilder()); + + stmt = db.getPreparedStatement( + "SELECT user.username as username," + + " sensor_data_aggr.timestamp_start as timestamp_start," + + " sensor_data_aggr.timestamp_end as timestamp_end," + + " sensor_data_aggr.data as data," + + " sensor_data_aggr.confidence as confidence," + + DataAggregatorDaemon.DAY_IN_MS + " as period_length" + + " FROM sensor_data_aggr, user, sensor" + + " WHERE sensor.id = sensor_data_aggr.sensor_id" + + " AND user.id = sensor.user_id" + + " AND timestamp_end-timestamp_start == ?" + + " ORDER BY timestamp_start ASC"); + stmt.setLong(1, DataAggregatorDaemon.DAY_IN_MS-1); + ArrayList dayDataList = DBConnection.exec(stmt, new SQLPowerDataBuilder()); + + + Templator tmpl = new Templator(FileUtil.find("web-resource/overview.tmpl")); + tmpl.set("minData", minDataList); + tmpl.set("hourData", hourDataList); + tmpl.set("dayData", dayDataList); + tmpl.set("username", new String[]{"Ziver", "Daniel"}); + + return tmpl; - @Override - public void respond(HttpPrintStream out, HttpHeaderParser client_info, Map session, Map cookie, Map request) throws IOException { - - try { - DBConnection db = HalContext.getDB(); - - PreparedStatement stmt = db.getPreparedStatement( - "SELECT user.username as username," - + " sensor_data_aggr.timestamp_start as timestamp_start," - + " sensor_data_aggr.timestamp_end as timestamp_end," - + " sensor_data_aggr.data as data," - + " sensor_data_aggr.confidence as confidence," - + DataAggregatorDaemon.FIVE_MINUTES_IN_MS + " as period_length" - + " FROM sensor_data_aggr, user, sensor" - + " WHERE sensor.id = sensor_data_aggr.sensor_id" - + " AND user.id = sensor.user_id" - + " AND timestamp_end-timestamp_start == ?" - + " AND timestamp_start > ?" - + " ORDER BY timestamp_start ASC"); - stmt.setLong(1, DataAggregatorDaemon.FIVE_MINUTES_IN_MS-1); - stmt.setLong(2, (System.currentTimeMillis() - DataAggregatorDaemon.DAY_IN_MS) ); - ArrayList minDataList = DBConnection.exec(stmt , new SQLPowerDataBuilder()); - - stmt = db.getPreparedStatement( - "SELECT user.username as username," - + " sensor_data_aggr.timestamp_start as timestamp_start," - + " sensor_data_aggr.timestamp_end as timestamp_end," - + " sensor_data_aggr.data as data," - + " sensor_data_aggr.confidence as confidence," - + DataAggregatorDaemon.HOUR_IN_MS + " as period_length" - + " FROM sensor_data_aggr, user, sensor" - + " WHERE sensor.id = sensor_data_aggr.sensor_id" - + " AND user.id = sensor.user_id" - + " AND timestamp_end-timestamp_start == ?" - + " AND timestamp_start > ?" - + " ORDER BY timestamp_start ASC"); - stmt.setLong(1, DataAggregatorDaemon.HOUR_IN_MS-1); - stmt.setLong(2, (System.currentTimeMillis() - 3*DataAggregatorDaemon.DAY_IN_MS) ); - ArrayList hourDataList = DBConnection.exec(stmt, new SQLPowerDataBuilder()); - - stmt = db.getPreparedStatement( - "SELECT user.username as username," - + " sensor_data_aggr.timestamp_start as timestamp_start," - + " sensor_data_aggr.timestamp_end as timestamp_end," - + " sensor_data_aggr.data as data," - + " sensor_data_aggr.confidence as confidence," - + DataAggregatorDaemon.DAY_IN_MS + " as period_length" - + " FROM sensor_data_aggr, user, sensor" - + " WHERE sensor.id = sensor_data_aggr.sensor_id" - + " AND user.id = sensor.user_id" - + " AND timestamp_end-timestamp_start == ?" - + " ORDER BY timestamp_start ASC"); - stmt.setLong(1, DataAggregatorDaemon.DAY_IN_MS-1); - ArrayList dayDataList = DBConnection.exec(stmt, new SQLPowerDataBuilder()); - - - Templator tmpl = new Templator(FileUtil.find("web-resource/index.html")); - tmpl.set("minData", minDataList); - tmpl.set("hourData", hourDataList); - tmpl.set("dayData", dayDataList); - tmpl.set("username", new String[]{"Ziver", "Daniel"}); - - out.print(tmpl.compile()); - - } catch (SQLException e) { - throw new IOException(e); - } } public static class PowerData{ diff --git a/web-resource/configure.html b/web-resource/configure.html deleted file mode 100644 index 6a3101ce..00000000 --- a/web-resource/configure.html +++ /dev/null @@ -1,136 +0,0 @@ - - - - - - - - - Power;Challenge - - - - - - - - - - - - - - -
-
- -
-

Configuration

- -
-
Profile Information
-
-
- -
- - -
-
- - -
-
- -
- -
-
- -
-
Local Sensors
-
- This is a local list of sensors connected to this node. -
- - - - - - - {{#localSensor}} - - - - - - {{/localSensor}} -
NameTypeConfiguration
{{.name}}{{.type}}{{.config}}
-
- -
-
External Users
-
- Add or remove users that you want to synchronized data with. -
- - - - - - - - {{#extUsers}} - - - - - - - {{/extUsers}} -
UsernameAddressHostnamePort
{{.username}}{{.address}}{{.hostname}}{{.port}}
-
- -
-
External Sensors
-
- This is a read only list of synchronized sensors from external users. -
- - - - - - - {{#extSensor}} - - - - - - {{/extSensor}} -
NameTypeConfiguration
{{.name}}{{.type}}{{.config}}
-
-
-
-
- - diff --git a/web-resource/configure.tmpl b/web-resource/configure.tmpl new file mode 100755 index 00000000..2fe428cd --- /dev/null +++ b/web-resource/configure.tmpl @@ -0,0 +1,86 @@ +

Configuration

+ +
+
Profile Information
+
+
+ +
+ + +
+
+ + +
+
+ +
+ +
+
+ +
+
Local Sensors
+
+ This is a local list of sensors connected to this node. +
+ + + + + + + {{#localSensor}} + + + + + + {{/localSensor}} +
NameTypeConfiguration
{{.name}}{{.type}}{{.config}}
+
+ +
+
External Users
+
+ Add or remove users that you want to synchronized data with. +
+ + + + + + + + {{#extUsers}} + + + + + + + {{/extUsers}} +
UsernameAddressHostnamePort
{{.username}}{{.address}}{{.hostname}}{{.port}}
+
+ +
+
External Sensors
+
+ This is a read only list of synchronized sensors from external users. +
+ + + + + + + {{#extSensor}} + + + + + + {{/extSensor}} +
NameTypeConfiguration
{{.name}}{{.type}}{{.config}}
+
diff --git a/web-resource/heatmap.html b/web-resource/heatmap.html deleted file mode 100644 index 6fc22a6b..00000000 --- a/web-resource/heatmap.html +++ /dev/null @@ -1,88 +0,0 @@ - - - - - - - - - Power;Challenge - - - - - - - - - - - - - - - - -
-
- -
-

Heat Map

- -
- -
-
-
- - diff --git a/web-resource/heatmap.tmpl b/web-resource/heatmap.tmpl new file mode 100755 index 00000000..2f78c85c --- /dev/null +++ b/web-resource/heatmap.tmpl @@ -0,0 +1,39 @@ +

Heat Map

+ +
+ + + + \ No newline at end of file diff --git a/web-resource/index.html b/web-resource/index.html deleted file mode 100644 index 75d5d3f4..00000000 --- a/web-resource/index.html +++ /dev/null @@ -1,111 +0,0 @@ - - - - - - - - - Power;Challenge - - - - - - - - - - - - - - - - - - -
-
- -
-

Overview

- -
-

Last 24 hours (kWh/5min)

-
-
-
-

Previous two days (kWh/h)

-
-
-
-

Long term (kWh/day)

-
-
- -
-
-
- - diff --git a/web-resource/index.tmpl b/web-resource/index.tmpl new file mode 100755 index 00000000..53a933e3 --- /dev/null +++ b/web-resource/index.tmpl @@ -0,0 +1,51 @@ + + + + + + + + + Power;Challenge + + + + + + + + + + + + +
+
+ + +
+ {{content}} +
+ +
+
+ + diff --git a/web-resource/overview.tmpl b/web-resource/overview.tmpl new file mode 100755 index 00000000..da6909c5 --- /dev/null +++ b/web-resource/overview.tmpl @@ -0,0 +1,63 @@ +

Overview

+ +
+

Last 24 hours (kWh/5min)

+
+
+
+

Previous two days (kWh/h)

+
+
+
+

Long term (kWh/day)

+
+
+ + + + + + +