Daten auslesen und auswerten
This commit is contained in:
parent
0155131945
commit
80937edde2
1
.gitignore
vendored
1
.gitignore
vendored
@ -43,3 +43,4 @@ buildNumber.properties
|
|||||||
hs_err_pid*
|
hs_err_pid*
|
||||||
replay_pid*
|
replay_pid*
|
||||||
|
|
||||||
|
/db-password.txt
|
||||||
|
|||||||
@ -1,16 +1,92 @@
|
|||||||
package info.peper.vz.rest;
|
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.Instant;
|
||||||
import java.time.OffsetDateTime;
|
import java.time.ZoneId;
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
|
import java.time.temporal.ChronoUnit;
|
||||||
import java.time.temporal.TemporalAccessor;
|
import java.time.temporal.TemporalAccessor;
|
||||||
|
|
||||||
public class ReadDbMain {
|
public class ReadDbMain {
|
||||||
|
|
||||||
|
private static final DateTimeFormatter DTF = DateTimeFormatter.ISO_LOCAL_DATE_TIME.withZone(ZoneId.systemDefault());
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
final DateTimeFormatter dtf = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
|
try (final Connection con = DriverManager.getConnection(
|
||||||
System.out.println(dtf.format(OffsetDateTime.now()));
|
"jdbc:mariadb://mariadb.fritz.box/volkszaehler",
|
||||||
final TemporalAccessor test = dtf.parse("2025-02-22T00:00:00");
|
"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};
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
BIN
vz-tabelle.ods
Normal file
BIN
vz-tabelle.ods
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user