diff --git a/.gitignore b/.gitignore index 75efa76..72a8c26 100644 --- a/.gitignore +++ b/.gitignore @@ -43,3 +43,4 @@ buildNumber.properties hs_err_pid* replay_pid* +/db-password.txt diff --git a/src/main/java/info/peper/vz/rest/ReadDbMain.java b/src/main/java/info/peper/vz/rest/ReadDbMain.java index 1dd42bb..83168c2 100644 --- a/src/main/java/info/peper/vz/rest/ReadDbMain.java +++ b/src/main/java/info/peper/vz/rest/ReadDbMain.java @@ -1,16 +1,92 @@ 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.OffsetDateTime; +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()); + public static void main(String[] args) throws Exception { - final DateTimeFormatter dtf = DateTimeFormatter.ISO_LOCAL_DATE_TIME; - System.out.println(dtf.format(OffsetDateTime.now())); - final TemporalAccessor test = dtf.parse("2025-02-22T00:00:00"); + try (final Connection con = DriverManager.getConnection( + "jdbc:mariadb://mariadb.fritz.box/volkszaehler", + "vz", + getPassword())) { + long startTimeStamp = getTimestamp("2023-01-01T00:00:00"); + long endTimeStamp = startTimeStamp + (24*60*60*1000); + final long finalEndTimeStamp = getTimestamp("2025-02-23T00:00:00"); + while (endTimeStamp < finalEndTimeStamp) { + final long[] zaehler = getValues(con, startTimeStamp, endTimeStamp, 1); + final long[] solar = getValues(con, startTimeStamp, endTimeStamp, 4); + System.out.println(DTF.format(Instant.ofEpochMilli(startTimeStamp)) + + "\t" + + DTF.format(Instant.ofEpochMilli(endTimeStamp)) + + "\t" + + zaehler[0] + + "\t" + + zaehler[1] + + "\t" + + solar[0] + ); + startTimeStamp += 24*60*60*1000; + endTimeStamp += 24*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 long[] getValues(final Connection con, + final long startTimestamp, + final long endTimestamp, + final int channelId) throws SQLException { + try (final PreparedStatement stmt = con.prepareStatement("SELECT * FROM volkszaehler.data WHERE channel_id=? AND timestamp>? AND timestamp<=? ORDER BY timestamp;")) { + long currentTimestamp = startTimestamp; + long wattMillisecondsPos = 0; + long wattMillisecondsNeg = 0; + stmt.setInt(1, channelId); + stmt.setLong(2, startTimestamp); + stmt.setLong(3, endTimestamp + 1*60*1000); + 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/vz-tabelle.ods b/vz-tabelle.ods new file mode 100644 index 0000000..7abff27 Binary files /dev/null and b/vz-tabelle.ods differ