115 lines
4.1 KiB
Java
115 lines
4.1 KiB
Java
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;
|
|
|
|
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[] {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");
|
|
long endTimeStamp = startTimeStamp + (24*60*60*1000);
|
|
final long finalEndTimeStamp = getTimestamp("2025-02-24T00: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(DTF.format(Instant.ofEpochMilli(startTimeStamp)) + ": " + channelId);
|
|
}
|
|
// 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 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 {
|
|
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 + 24*60*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};
|
|
}
|
|
|
|
}
|
|
|
|
}
|