108 lines
3.6 KiB
Java
108 lines
3.6 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.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};
|
|
|
|
}
|
|
|
|
}
|