diff --git a/src/main/java/info/peper/vz/rest/FillAggregateTableMain.java b/src/main/java/info/peper/vz/rest/FillAggregateTableMain.java index 1c3c686..4c6f7cf 100644 --- a/src/main/java/info/peper/vz/rest/FillAggregateTableMain.java +++ b/src/main/java/info/peper/vz/rest/FillAggregateTableMain.java @@ -8,40 +8,79 @@ import java.io.InputStreamReader; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; -import java.sql.ResultSet; import java.sql.SQLException; import java.time.Instant; import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoUnit; import java.time.temporal.TemporalAccessor; +import java.util.List; + +import info.peper.vz.rest.bo.db.Aggregate2; public class FillAggregateTableMain { private static final DateTimeFormatter DTF = DateTimeFormatter.ISO_LOCAL_DATE_TIME.withZone(ZoneId.systemDefault()); - private static final int[] CHANNELS_TO_USE = new int[] {1, 4, }; public static void main(String[] args) throws Exception { try (final Connection con = DriverManager.getConnection( "jdbc:mariadb://mariadb.fritz.box/volkszaehler", "vz", getPassword())) { - long startTimeStamp = getTimestamp("2022-06-01T00:00:00"); - final long finalEndTimeStamp = getTimestamp("2025-03-01T00:00:00"); + processKerpen(con); + processRoki(con); + } + } + + private static void processKerpen(final Connection con) throws SQLException { + final long startTimeStamp = getTimestamp("2022-05-20T09:00:00"); + final long finalEndTimeStamp = getTimestamp("2025-03-09T15:00:00"); + + final List aggregates = Calculator.processHouseMulti( + 1, + startTimeStamp, + finalEndTimeStamp, + 60*1000, + new int[] {4}, + new int[] {1}, + con); + saveValues(con, aggregates, 1); + } + + private static void processRoki(final Connection con) throws SQLException { + final long startTimeStamp = getTimestamp("2024-06-23T17:00:00"); + final long finalEndTimeStamp = getTimestamp("2025-03-09T15:00:00"); + + final List aggregates = Calculator.processHouseMulti( + 2, + startTimeStamp, + finalEndTimeStamp, + 60*1000, + new int[] {24}, + new int[] {18, 19, 20}, + con); + saveValues(con, aggregates, 2); + } - long endTimeStamp = startTimeStamp + (60*60*1000); - while (endTimeStamp < finalEndTimeStamp) { - if (startTimeStamp % 86400000 == 0) { - System.out.println(DTF.format(Instant.ofEpochMilli(startTimeStamp))); + private static void saveValues(final Connection con, final List aggregates, final int houseId) throws SQLException { + System.out.println("Saving entries... : " + aggregates.size()); + try (final PreparedStatement stmt = con.prepareStatement("INSERT INTO tobias_aggregate2 (house_id, timestamp_start, timestamp_end, produced_energy, obtained_energy, injected_energy) VALUES (?, ?, ?, ?, ?, ?)")) { + int i = 0; + for (Aggregate2 ats : aggregates) { + stmt.setInt(1, houseId); + stmt.setLong(2, ats.getTimestampStart()); + stmt.setLong(3, ats.getTimestampEnd()); + stmt.setLong(4, ats.getProducedEnergy()); + stmt.setLong(5, ats.getObtainedEnergy()); + stmt.setLong(6, ats.getInjectedEnergy()); + stmt.addBatch(); + i++; + if (i % 100000 == 0) { + System.out.println(i); + stmt.executeBatch(); } - for (int channelId : CHANNELS_TO_USE) { -// System.out.print(DTF.format(Instant.ofEpochMilli(startTimeStamp)) + ": " + channelId + ": "); - final long[] values = getValues(con, startTimeStamp, endTimeStamp, channelId); - saveValues(con, startTimeStamp, endTimeStamp, channelId, values); - } - startTimeStamp += 60*60*1000; - endTimeStamp += 60*60*1000; } + stmt.executeBatch(); } } @@ -58,57 +97,4 @@ public class FillAggregateTableMain { return Instant.EPOCH.until(instant, ChronoUnit.MILLIS); } - private static void saveValues(final Connection con, - final long startTimestamp, - final long endTimestamp, - final int channelId, - final long[] values) throws SQLException { - try (final PreparedStatement stmt = con.prepareStatement("INSERT INTO tobias_aggregate (channel_id, timestamp_start, timestamp_end, sum_positive, sum_negative) VALUES (?, ?, ?, ?, ?)")) { - stmt.setInt(1, channelId); - stmt.setLong(2, startTimestamp); - stmt.setLong(3, endTimestamp); - stmt.setLong(4, values[0]); - stmt.setLong(5, values[1]); - stmt.execute(); - } - } - - private static long[] getValues(final Connection con, - final long startTimestamp, - final long endTimestamp, - final int channelId) throws SQLException { - long currentTimestamp = startTimestamp; - long wattMillisecondsPos = 0; - long wattMillisecondsNeg = 0; - - int numberOfRows = 0; - while (currentTimestamp < endTimestamp) { - try (final PreparedStatement stmt = con.prepareStatement("SELECT * FROM volkszaehler.data WHERE channel_id=? AND timestamp>? ORDER BY timestamp;")) { -// System.out.print("#"); - stmt.setMaxRows(500); - stmt.setFetchSize(100); - stmt.setInt(1, channelId); - stmt.setLong(2, currentTimestamp); -// stmt.setLong(3, currentTimestamp + (864000000)); - try (final ResultSet rs = stmt.executeQuery()) { - while (rs.next() && currentTimestamp <= endTimestamp) { - numberOfRows++; - final long rsTimestamp = rs.getLong("timestamp"); - final long rsValue = rs.getLong("value"); - final long tsDiff = Math.min(rsTimestamp - currentTimestamp, endTimestamp - currentTimestamp); - currentTimestamp = rsTimestamp; - if (rsValue > 0) { - wattMillisecondsPos += (rsValue * tsDiff); - } else if (rsValue < 0) { - wattMillisecondsNeg += (-rsValue * tsDiff); - } - } - } - } - } -// System.out.println(" " + numberOfRows); - return new long[] {wattMillisecondsPos/3600, wattMillisecondsNeg/3600}; - - } - } diff --git a/src/main/java/info/peper/vz/rest/FillAggregateTableMain2.java b/src/main/java/info/peper/vz/rest/FillAggregateTableMain2.java deleted file mode 100644 index afb70ec..0000000 --- a/src/main/java/info/peper/vz/rest/FillAggregateTableMain2.java +++ /dev/null @@ -1,198 +0,0 @@ -package info.peper.vz.rest; - -import java.io.BufferedReader; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.time.Instant; -import java.time.ZoneId; -import java.time.format.DateTimeFormatter; -import java.time.temporal.ChronoUnit; -import java.time.temporal.TemporalAccessor; -import java.util.LinkedList; -import java.util.List; - -public class FillAggregateTableMain2 { - - private static final DateTimeFormatter DTF = DateTimeFormatter.ISO_LOCAL_DATE_TIME.withZone(ZoneId.systemDefault()); - private static final int[] CHANNELS_TO_USE = new int[] {1,/* 4,*/ }; - private static final List SAVELIST = new LinkedList(); - - private static class AggregateToSave { - private final long startTs; - private final long endTs; - private final long valuePos; - private final long valueNeg; - private final int channelId; - - private AggregateToSave(long startTs, long endTs, long valuePos, long valueNeg, int channelId) { - super(); - this.startTs = startTs; - this.endTs = endTs; - this.valuePos = valuePos; - this.valueNeg = valueNeg; - this.channelId = channelId; - } - - private long getStartTs() { - return startTs; - } - - private long getEndTs() { - return endTs; - } - - private long getValuePos() { - return valuePos; - } - - private long getValueNeg() { - return valueNeg; - } - - private int getChannelId() { - return channelId; - } - } - - public static void main(String[] args) throws Exception { - try (final Connection con = DriverManager.getConnection( - "jdbc:mariadb://mariadb.fritz.box/volkszaehler", - "vz", - getPassword())) { -// long startTimeStamp = getTimestamp("2025-01-01T09:00:00"); - final long startTimeStamp = getTimestamp("2022-05-20T09:00:00"); -// final long finalEndTimeStamp = getTimestamp("2022-05-20T09:02:00"); - final long finalEndTimeStamp = getTimestamp("2025-03-01T00:00:00"); - for (int channelId : CHANNELS_TO_USE) { - processChannel(startTimeStamp, finalEndTimeStamp, 60*1000, channelId, con); - } - System.out.println("Saving... " + SAVELIST.size()); - saveValues(con); -// for (AggregateToSave ats : SAVELIST) { -// saveValues(con, ats.startTs, ats.endTs, ats.channelId, new long[] {ats.valuePos, ats.valueNeg}); -// System.out.println("####"); -// } - } - } - - private static void processChannel(final long startTimestamp, - final long endTimestamp, - final long interval, - final int channelId, - final Connection con) throws SQLException { - long currentTimestamp = startTimestamp; - long intervalStartTimestamp = startTimestamp; - long intervalEndTimestamp = startTimestamp + interval; - long intervalPos = 0; - long intervalNeg = 0; - try (final PreparedStatement stmt = con.prepareStatement("SELECT * FROM volkszaehler.data WHERE channel_id=? AND timestamp>? ORDER BY timestamp;")) { - stmt.setInt(1, channelId); - stmt.setLong(2, startTimestamp); - stmt.setFetchSize(1000); - boolean noNextRecord = false; - try (final ResultSet rs = stmt.executeQuery()) { - long sumDiff = 0; - while (noNextRecord || (currentTimestamp <= endTimestamp && rs.next())) { - noNextRecord = false; - final long timestamp = rs.getLong("timestamp"); - final double value; - if (rs.getDouble("value") >= 0 && rs.getDouble("value") < 1) { - value = 0.0; - } else { - value = rs.getDouble("value"); - } - final long tsDiff; - if (timestamp < intervalEndTimestamp) { - tsDiff = timestamp - currentTimestamp; - } else { - tsDiff = intervalEndTimestamp - currentTimestamp; - } -// final long tsDiff = Math.min(timestamp - currentTimestamp, intervalEndTimestamp - currentTimestamp); - sumDiff += tsDiff; - currentTimestamp = timestamp; - if (value > 0) { - intervalPos += Math.round(value * tsDiff); - } else if (value < 0) { - intervalNeg += Math.round(-value * tsDiff); - } -// System.out.println("ts: " + currentTimestamp + " / diff: " + tsDiff + " / value: " + value + " / intervalPos: " + intervalPos); - if (timestamp >= intervalEndTimestamp) { - if (sumDiff != interval) { - System.err.println("sumDiff: " + sumDiff + " / interval: " + interval); - } - sumDiff = 0; - if (intervalStartTimestamp % (86400000*10) == 0) { - System.out.println( - DTF.format(Instant.ofEpochMilli(intervalStartTimestamp)) + " - " + - DTF.format(Instant.ofEpochMilli(intervalEndTimestamp)) + " / " + - (intervalPos/3600) + " / " + - (intervalNeg/3600)); - } - SAVELIST.add(new AggregateToSave(intervalStartTimestamp, intervalEndTimestamp, Math.round(intervalPos), Math.round(intervalNeg), channelId)); - intervalStartTimestamp += interval; - intervalEndTimestamp += interval; - currentTimestamp = intervalStartTimestamp; - intervalPos = 0; - intervalNeg = 0; - noNextRecord = true; - } - } - } - } - } - - private static String getPassword() throws IOException { - try (final InputStream is = new FileInputStream("db-password.txt")) { - final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is)); - return bufferedReader.readLine(); - } - } - - private static long getTimestamp(final String dateTime) { - final TemporalAccessor tempAccessor = DTF.parse(dateTime); - final Instant instant = Instant.from(tempAccessor); - return Instant.EPOCH.until(instant, ChronoUnit.MILLIS); - } - - private static void saveValues(final Connection con, - final long startTimestamp, - final long endTimestamp, - final int channelId, - final long[] values) throws SQLException { - try (final PreparedStatement stmt = con.prepareStatement("INSERT INTO tobias_aggregate (channel_id, timestamp_start, timestamp_end, sum_positive, sum_negative) VALUES (?, ?, ?, ?, ?)")) { - stmt.setInt(1, channelId); - stmt.setLong(2, startTimestamp); - stmt.setLong(3, endTimestamp); - stmt.setLong(4, values[0]); - stmt.setLong(5, values[1]); - stmt.execute(); - } - } - - private static void saveValues(final Connection con) throws SQLException { - try (final PreparedStatement stmt = con.prepareStatement("INSERT INTO tobias_aggregate (channel_id, timestamp_start, timestamp_end, sum_positive, sum_negative) VALUES (?, ?, ?, ?, ?)")) { - int i = 0; - for (AggregateToSave ats : SAVELIST) { - stmt.setInt(1, ats.channelId); - stmt.setLong(2, ats.startTs); - stmt.setLong(3, ats.endTs); - stmt.setDouble(4, ats.valuePos); - stmt.setDouble(5, ats.valueNeg); - stmt.addBatch(); - i++; - if (i % 100000 == 0) { - System.out.println(i); - stmt.executeBatch(); - } - } - stmt.executeBatch(); - } - } -} diff --git a/src/main/java/info/peper/vz/rest/FillAggregateTableMain3.java b/src/main/java/info/peper/vz/rest/FillAggregateTableMain3.java deleted file mode 100644 index e8602e9..0000000 --- a/src/main/java/info/peper/vz/rest/FillAggregateTableMain3.java +++ /dev/null @@ -1,100 +0,0 @@ -package info.peper.vz.rest; - -import java.io.BufferedReader; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.PreparedStatement; -import java.sql.SQLException; -import java.time.Instant; -import java.time.ZoneId; -import java.time.format.DateTimeFormatter; -import java.time.temporal.ChronoUnit; -import java.time.temporal.TemporalAccessor; -import java.util.List; - -import info.peper.vz.rest.bo.db.Aggregate2; - -public class FillAggregateTableMain3 { - - private static final DateTimeFormatter DTF = DateTimeFormatter.ISO_LOCAL_DATE_TIME.withZone(ZoneId.systemDefault()); - - public static void main(String[] args) throws Exception { - try (final Connection con = DriverManager.getConnection( - "jdbc:mariadb://mariadb.fritz.box/volkszaehler", - "vz", - getPassword())) { - processKerpen(con); - processRoki(con); - } - } - - private static void processKerpen(final Connection con) throws SQLException { - final long startTimeStamp = getTimestamp("2022-05-20T09:00:00"); - final long finalEndTimeStamp = getTimestamp("2025-03-09T15:00:00"); - - final List aggregates = Calculator.processHouseMulti( - 1, - startTimeStamp, - finalEndTimeStamp, - 60*1000, - new int[] {4}, - new int[] {1}, - con); - saveValues(con, aggregates, 1); - } - - private static void processRoki(final Connection con) throws SQLException { - final long startTimeStamp = getTimestamp("2024-06-23T17:00:00"); - final long finalEndTimeStamp = getTimestamp("2025-03-09T15:00:00"); - - final List aggregates = Calculator.processHouseMulti( - 2, - startTimeStamp, - finalEndTimeStamp, - 60*1000, - new int[] {24}, - new int[] {18, 19, 20}, - con); - saveValues(con, aggregates, 2); - } - - private static void saveValues(final Connection con, final List aggregates, final int houseId) throws SQLException { - System.out.println("Saving entries... : " + aggregates.size()); - try (final PreparedStatement stmt = con.prepareStatement("INSERT INTO tobias_aggregate2 (house_id, timestamp_start, timestamp_end, produced_energy, obtained_energy, injected_energy) VALUES (?, ?, ?, ?, ?, ?)")) { - int i = 0; - for (Aggregate2 ats : aggregates) { - stmt.setInt(1, houseId); - stmt.setLong(2, ats.getTimestampStart()); - stmt.setLong(3, ats.getTimestampEnd()); - stmt.setLong(4, ats.getProducedEnergy()); - stmt.setLong(5, ats.getObtainedEnergy()); - stmt.setLong(6, ats.getInjectedEnergy()); - stmt.addBatch(); - i++; - if (i % 100000 == 0) { - System.out.println(i); - stmt.executeBatch(); - } - } - stmt.executeBatch(); - } - } - - private static String getPassword() throws IOException { - try (final InputStream is = new FileInputStream("db-password.txt")) { - final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is)); - return bufferedReader.readLine(); - } - } - - private static long getTimestamp(final String dateTime) { - final TemporalAccessor tempAccessor = DTF.parse(dateTime); - final Instant instant = Instant.from(tempAccessor); - return Instant.EPOCH.until(instant, ChronoUnit.MILLIS); - } - -} diff --git a/src/main/java/info/peper/vz/rest/ReadDbMain.java b/src/main/java/info/peper/vz/rest/ReadDbMain.java deleted file mode 100644 index b61f42f..0000000 --- a/src/main/java/info/peper/vz/rest/ReadDbMain.java +++ /dev/null @@ -1,105 +0,0 @@ -package info.peper.vz.rest; - -import java.io.BufferedReader; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; -import java.time.Instant; -import java.time.ZoneId; -import java.time.format.DateTimeFormatter; -import java.time.temporal.ChronoUnit; -import java.time.temporal.TemporalAccessor; - -public class ReadDbMain { - - private static final DateTimeFormatter DTF = DateTimeFormatter.ISO_LOCAL_DATE_TIME.withZone(ZoneId.systemDefault()); - private static final int[] CHANNELS_TO_USE = new int[] {4}; - - public static void main(String[] args) throws Exception { - try (final Connection con = DriverManager.getConnection( - "jdbc:mariadb://mariadb.fritz.box/volkszaehler", - "vz", - getPassword())) { - long startTimeStamp = getTimestamp("2025-02-28T18:00:00"); - long endTimeStamp = startTimeStamp + (60*60*1000); - final long finalEndTimeStamp = getTimestamp("2025-03-01T18:00:00"); - while (endTimeStamp < finalEndTimeStamp) { - for (int channelId : CHANNELS_TO_USE) { - final long[] values = getValues(con, startTimeStamp, endTimeStamp, channelId); -// saveValues(con, startTimeStamp, endTimeStamp, channelId, values); - System.out.println("{x: " + startTimeStamp + ", y: " + (values[0]*60/1000) + "},"); - } - startTimeStamp += 60*60*1000; - endTimeStamp += 60*60*1000; - } - } - } - - private static String getPassword() throws IOException { - try (final InputStream is = new FileInputStream("db-password.txt")) { - final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is)); - return bufferedReader.readLine(); - } - } - - private static long getTimestamp(final String dateTime) { - final TemporalAccessor tempAccessor = DTF.parse(dateTime); - final Instant instant = Instant.from(tempAccessor); - return Instant.EPOCH.until(instant, ChronoUnit.MILLIS); - } - - private static void saveValues(final Connection con, - final long startTimestamp, - final long endTimestamp, - final int channelId, - final long[] values) throws SQLException { - try (final PreparedStatement stmt = con.prepareStatement("INSERT INTO tobias_aggregate (channel_id, timestamp_start, timestamp_end, sum_positive, sum_negative) VALUES (?, ?, ?, ?, ?)")) { - stmt.setInt(1, channelId); - stmt.setLong(2, startTimestamp); - stmt.setLong(3, endTimestamp); - stmt.setLong(4, values[0]); - stmt.setLong(5, values[1]); - stmt.execute(); - } - } - - private static long[] getValues(final Connection con, - final long startTimestamp, - final long endTimestamp, - final int channelId) throws SQLException { - long currentTimestamp = startTimestamp; - long wattMillisecondsPos = 0; - long wattMillisecondsNeg = 0; - - while (currentTimestamp < endTimestamp) { - try (final PreparedStatement stmt = con.prepareStatement("SELECT * FROM volkszaehler.data WHERE channel_id=? AND timestamp>? ORDER BY timestamp;")) { - stmt.setMaxRows(1000); - stmt.setInt(1, channelId); - stmt.setLong(2, currentTimestamp); - try (final ResultSet rs = stmt.executeQuery()) { - while (rs.next() && currentTimestamp <= endTimestamp) { - final long rsTimestamp = rs.getLong("timestamp"); - final long rsValue = rs.getLong("value"); - final long tsDiff = rsTimestamp - Math.min(endTimestamp, currentTimestamp); - currentTimestamp = rsTimestamp; - if (rsValue > 0) { - wattMillisecondsPos += (rsValue * tsDiff); - } else if (rsValue < 0) { - wattMillisecondsNeg += (-rsValue * tsDiff); - } - } - } - } - } - return new long[] {wattMillisecondsPos/3600, wattMillisecondsNeg/3600}; - - } - -} diff --git a/src/main/java/info/peper/vz/rest/ReadDbMain2.java b/src/main/java/info/peper/vz/rest/ReadDbMain2.java deleted file mode 100644 index 8d5a469..0000000 --- a/src/main/java/info/peper/vz/rest/ReadDbMain2.java +++ /dev/null @@ -1,107 +0,0 @@ -package info.peper.vz.rest; - -import java.io.BufferedReader; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; -import java.time.Instant; -import java.time.ZoneId; -import java.time.format.DateTimeFormatter; -import java.time.temporal.ChronoUnit; -import java.time.temporal.TemporalAccessor; - -public class ReadDbMain2 { - - private static final DateTimeFormatter DTF = DateTimeFormatter.ISO_LOCAL_DATE_TIME.withZone(ZoneId.systemDefault()); - private static final int[] CHANNELS_TO_USE = new int[] {4}; - - public static void main(String[] args) throws Exception { - try (final Connection con = DriverManager.getConnection( - "jdbc:mariadb://mariadb.fritz.box/volkszaehler", - "vz", - getPassword())) { - try (final PreparedStatement stmt = con.prepareStatement("SELECT timestamp FROM volkszaehler.data WHERE channel_id=4 AND timestamp >= 1740697200000 ORDER BY timestamp"); final ResultSet rs = stmt.executeQuery()) { - long lastTime = 0; - long longestDiff = 0; - while (rs.next()) { - if (lastTime > 0) { - long diff = rs.getLong("timestamp") - lastTime; - if (diff > longestDiff) { - longestDiff = diff; - } - } - lastTime = rs.getLong("timestamp"); - } - System.out.println(longestDiff); - } - } - } - - private static String getPassword() throws IOException { - try (final InputStream is = new FileInputStream("db-password.txt")) { - final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is)); - return bufferedReader.readLine(); - } - } - - private static long getTimestamp(final String dateTime) { - final TemporalAccessor tempAccessor = DTF.parse(dateTime); - final Instant instant = Instant.from(tempAccessor); - return Instant.EPOCH.until(instant, ChronoUnit.MILLIS); - } - - private static void saveValues(final Connection con, - final long startTimestamp, - final long endTimestamp, - final int channelId, - final long[] values) throws SQLException { - try (final PreparedStatement stmt = con.prepareStatement("INSERT INTO tobias_aggregate (channel_id, timestamp_start, timestamp_end, sum_positive, sum_negative) VALUES (?, ?, ?, ?, ?)")) { - stmt.setInt(1, channelId); - stmt.setLong(2, startTimestamp); - stmt.setLong(3, endTimestamp); - stmt.setLong(4, values[0]); - stmt.setLong(5, values[1]); - stmt.execute(); - } - } - - private static long[] getValues(final Connection con, - final long startTimestamp, - final long endTimestamp, - final int channelId) throws SQLException { - long currentTimestamp = startTimestamp; - long wattMillisecondsPos = 0; - long wattMillisecondsNeg = 0; - - while (currentTimestamp < endTimestamp) { - try (final PreparedStatement stmt = con.prepareStatement("SELECT * FROM volkszaehler.data WHERE channel_id=? AND timestamp>? ORDER BY timestamp;")) { - stmt.setMaxRows(1000); - stmt.setInt(1, channelId); - stmt.setLong(2, currentTimestamp); - try (final ResultSet rs = stmt.executeQuery()) { - while (rs.next() && currentTimestamp <= endTimestamp) { - final long rsTimestamp = rs.getLong("timestamp"); - final long rsValue = rs.getLong("value"); - final long tsDiff = rsTimestamp - Math.min(endTimestamp, currentTimestamp); - currentTimestamp = rsTimestamp; - if (rsValue > 0) { - wattMillisecondsPos += (rsValue * tsDiff); - } else if (rsValue < 0) { - wattMillisecondsNeg += (-rsValue * tsDiff); - } - } - } - } - } - return new long[] {wattMillisecondsPos/3600, wattMillisecondsNeg/3600}; - - } - -} diff --git a/src/main/java/info/peper/vz/rest/ScheduledTasks.java b/src/main/java/info/peper/vz/rest/ScheduledTasks.java new file mode 100644 index 0000000..475422d --- /dev/null +++ b/src/main/java/info/peper/vz/rest/ScheduledTasks.java @@ -0,0 +1,37 @@ +package info.peper.vz.rest; + +import java.util.List; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +@Component +public class ScheduledTasks { + + private static final Logger LOG = LoggerFactory.getLogger(ScheduledTasks.class); + + @Autowired + private JdbcTemplate jdbcTemplate; + + @Scheduled(fixedRate = 5000) + public void printSomething() { + final List houseIds = jdbcTemplate.query("SELECT house_id FROM tobias_house GROUP BY house_id ORDER BY house_id;", + (rs, rowNum) -> rs.getInt("house_id")); + for (int houseId : houseIds) { + this.processHouse(houseId); + } + } + + private void processHouse(final int houseId) { + LOG.info("Processing house " + houseId + "..."); + final long lastTimestamp = jdbcTemplate.queryForObject("SELECT MAX(timestamp_end) FROM tobias_aggregate2 WHERE house_id=?", Long.class, houseId); + LOG.info("- last timestamp: " + lastTimestamp); + long currentTimeRound = (System.currentTimeMillis()/60000)*60000; +// LOG.info(System.currentTimeMillis() + ""); +// LOG.info(currentTimeRound + ""); + } +} diff --git a/src/main/java/info/peper/vz/rest/VzRestApplication.java b/src/main/java/info/peper/vz/rest/VzRestApplication.java index e2c7988..af7f559 100644 --- a/src/main/java/info/peper/vz/rest/VzRestApplication.java +++ b/src/main/java/info/peper/vz/rest/VzRestApplication.java @@ -2,8 +2,10 @@ package info.peper.vz.rest; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication +@EnableScheduling public class VzRestApplication { public static void main(String... args) { diff --git a/src/main/java/info/peper/vz/rest/bo/db/EnergyPrice.java b/src/main/java/info/peper/vz/rest/bo/db/EnergyPrice.java index cc5dcbb..a6c00a1 100644 --- a/src/main/java/info/peper/vz/rest/bo/db/EnergyPrice.java +++ b/src/main/java/info/peper/vz/rest/bo/db/EnergyPrice.java @@ -12,14 +12,14 @@ import jakarta.persistence.IdClass; public class EnergyPrice { public static class CompositeKey implements Serializable { private static final long serialVersionUID = 3097284483123288289L; - private int contractId; + private int houseId; private long timestampStart; private long timestampEnd; @Override public int hashCode() { final int prime = 31; int result = 1; - result = prime * result + contractId; + result = prime * result + houseId; result = prime * result + (int) (timestampEnd ^ (timestampEnd >>> 32)); result = prime * result + (int) (timestampStart ^ (timestampStart >>> 32)); return result; @@ -33,7 +33,7 @@ public class EnergyPrice { if (getClass() != obj.getClass()) return false; CompositeKey other = (CompositeKey) obj; - if (contractId != other.contractId) + if (houseId != other.houseId) return false; if (timestampEnd != other.timestampEnd) return false; @@ -48,16 +48,16 @@ public class EnergyPrice { super(); } - public EnergyPrice(int contractId, long timestampStart, long timestampEnd, float price) { + public EnergyPrice(int houseId, long timestampStart, long timestampEnd, float price) { super(); - this.contractId = contractId; + this.houseId = houseId; this.timestampStart = timestampStart; this.timestampEnd = timestampEnd; this.price = price; } @Id - @Column(name="contract_id") - private int contractId; + @Column(name="house_id") + private int houseId; @Id @Column(name="timestamp_start") private long timestampStart; @@ -67,12 +67,12 @@ public class EnergyPrice { @Column(name="price") private float price; - public int getContractId() { - return contractId; + public int getHouseId() { + return houseId; } - public void setContractId(int contractId) { - this.contractId = contractId; + public void setHouseId(int houseId) { + this.houseId = houseId; } public long getTimestampStart() {