Skip to content

Commit

Permalink
Indentation, cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
zeekoe committed Jan 29, 2024
1 parent 7719778 commit 883c2b2
Show file tree
Hide file tree
Showing 16 changed files with 737 additions and 741 deletions.
6 changes: 3 additions & 3 deletions src/main/java/com/github/zeekoe/bluebird/Bluebird.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.github.zeekoe.bluebird.heatpump.Heatpump;

public class Bluebird {
public static void main(String[] args) {
new Retryer<>(Heatpump.class).startRunning();
}
public static void main(String[] args) {
new Retryer<>(Heatpump.class).startRunning();
}
}
60 changes: 30 additions & 30 deletions src/main/java/com/github/zeekoe/bluebird/Retryer.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,42 @@
import java.time.LocalDateTime;

public class Retryer<T extends Runnable> {
private static final int MAX_RETRY_COUNT = 5;
private final T runnable;
private int retryCount;
private static final int MAX_RETRY_COUNT = 5;
private final T runnable;
private int retryCount;

public Retryer(Class<T> klazz) {
try {
this.runnable = klazz.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new RuntimeException(e);
}
public Retryer(Class<T> klazz) {
try {
this.runnable = klazz.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}

public void startRunning() {
while (retryCount <= MAX_RETRY_COUNT) {
try {
runnable.run();
retryCount = 1;
} catch (Exception e) {
retryCount++;
System.out.println(LocalDateTime.now() + ": Exception, retry count: " + retryCount);
System.out.println(e.getMessage());
}
if (retryCount <= MAX_RETRY_COUNT) {
sleep(30_000L * retryCount * retryCount);
}
}
System.out.println("Giving up.");
public void startRunning() {
while (retryCount <= MAX_RETRY_COUNT) {
try {
runnable.run();
retryCount = 1;
} catch (Exception e) {
retryCount++;
System.out.println(LocalDateTime.now() + ": Exception, retry count: " + retryCount);
System.out.println(e.getMessage());
}
if (retryCount <= MAX_RETRY_COUNT) {
sleep(30_000L * retryCount * retryCount);
}
}
System.out.println("Giving up.");
}

void sleep(long l) {
try {
Thread.sleep(l);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
void sleep(long l) {
try {
Thread.sleep(l);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}


78 changes: 39 additions & 39 deletions src/main/java/com/github/zeekoe/bluebird/heatpump/Auth.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,46 +13,46 @@
import static com.github.zeekoe.bluebird.infrastructure.BluebirdProperty.WEHEAT_USERNAME;

public class Auth {
private static final MyHttpClient httpClient = new MyHttpClient();
private Token token = null;

private static final String TOKEN_URL = "https://auth.weheat.nl/auth/realms/Weheat/protocol/openid-connect/token";;
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

public String getToken() throws IOException, InterruptedException {
if (token == null) {
System.out.println("Retrieving token");
token = OBJECT_MAPPER.readValue(doLogin(), Token.class);
}
if (token.getExpiryDateTime().minusMinutes(1).isBefore(LocalDateTime.now())) {
System.out.println("Refreshing token");
token = OBJECT_MAPPER.readValue(refreshToken(), Token.class);
}
return token.getAccess_token();
}
private static final MyHttpClient httpClient = new MyHttpClient();
private Token token = null;

private String refreshToken() throws IOException, InterruptedException {
return httpClient.post(TOKEN_URL,
Map.of("Content-Type", "application/x-www-form-urlencoded",
"Accept", "application/json"),
Map.of(
"grant_type", "refresh_token",
"scope", "openid",
"client_id", "WeheatCommunityAPI",
"refresh_token", token.getRefresh_token()
));
}
private static final String TOKEN_URL = "https://auth.weheat.nl/auth/realms/Weheat/protocol/openid-connect/token";
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

private String doLogin() throws IOException, InterruptedException {
return httpClient.post(TOKEN_URL,
Map.of("Content-Type", "application/x-www-form-urlencoded",
"Accept", "application/json"),
Map.of(
"grant_type", "password",
"scope", "openid",
"client_id", "WeheatCommunityAPI",
"username", property(WEHEAT_USERNAME),
"password", property(WEHEAT_PASSWORD)
));
public String getToken() throws IOException, InterruptedException {
if (token == null) {
System.out.println("Retrieving token");
token = OBJECT_MAPPER.readValue(doLogin(), Token.class);
}
if (token.getExpiryDateTime().minusMinutes(1).isBefore(LocalDateTime.now())) {
System.out.println("Refreshing token");
token = OBJECT_MAPPER.readValue(refreshToken(), Token.class);
}
return token.getAccess_token();
}

private String refreshToken() throws IOException, InterruptedException {
return httpClient.post(TOKEN_URL,
Map.of("Content-Type", "application/x-www-form-urlencoded",
"Accept", "application/json"),
Map.of(
"grant_type", "refresh_token",
"scope", "openid",
"client_id", "WeheatCommunityAPI",
"refresh_token", token.getRefresh_token()
));
}

private String doLogin() throws IOException, InterruptedException {
return httpClient.post(TOKEN_URL,
Map.of("Content-Type", "application/x-www-form-urlencoded",
"Accept", "application/json"),
Map.of(
"grant_type", "password",
"scope", "openid",
"client_id", "WeheatCommunityAPI",
"username", property(WEHEAT_USERNAME),
"password", property(WEHEAT_PASSWORD)
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

@FunctionalInterface
public interface Executable {
void execute() throws Exception;
void execute() throws Exception;
}
124 changes: 62 additions & 62 deletions src/main/java/com/github/zeekoe/bluebird/heatpump/Gapfiller.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,79 +16,79 @@
import java.util.Random;

public class Gapfiller {
private ZonedDateTime gapStartTime;
private ZonedDateTime nextFillTime;
private int timeWindow;
private ZonedDateTime gapStartTime;
private ZonedDateTime nextFillTime;
private int timeWindow;

public Gapfiller() {
this.gapStartTime = ZonedDateTime.now().minusHours(2);
System.out.println("Gapfiller fresh start, set gap start time to " + gapStartTime);
nextFillTime = ZonedDateTime.now().plusSeconds(10);
setTimeWindow();
public Gapfiller() {
this.gapStartTime = ZonedDateTime.now().minusHours(2);
System.out.println("Gapfiller fresh start, set gap start time to " + gapStartTime);
nextFillTime = ZonedDateTime.now().plusSeconds(10);
setTimeWindow();

}
}

private void setTimeWindow() {
this.timeWindow = 10; // minimum of 10 minutes to be gentle for the server
try {
String timeWindowString = BluebirdProperties.property(BluebirdProperty.GAPFILLER_WINDOW_MINUTES);
int timeWindowInt = Integer.parseInt(timeWindowString);
if (timeWindowInt > 10) {
this.timeWindow = timeWindowInt;
}
} catch (Exception ignored) {
}
private void setTimeWindow() {
this.timeWindow = 10; // minimum of 10 minutes to be gentle for the server
try {
String timeWindowString = BluebirdProperties.property(BluebirdProperty.GAPFILLER_WINDOW_MINUTES);
int timeWindowInt = Integer.parseInt(timeWindowString);
if (timeWindowInt > 10) {
this.timeWindow = timeWindowInt;
}
} catch (Exception ignored) {
}
}

public void checkAndRun() {
try {
if (ZonedDateTime.now().isAfter(nextFillTime)) {
HeatpumpLog[] heatpumpLogs = new Heatpump().doHeatpumpGapRequest(gapStartTime);
fillTheGaps(heatpumpLogs);
gapStartTime = nextFillTime;
nextFillTime = ZonedDateTime.now()
.plusMinutes(timeWindow)
.plusSeconds(new Random().nextInt(20));
System.out.println("Gapfiller next window: " + gapStartTime + " - " + nextFillTime);
}
} catch (Exception e) {
System.out.println("Gapfiller failed: " + e.getMessage());
}
public void checkAndRun() {
try {
if (ZonedDateTime.now().isAfter(nextFillTime)) {
HeatpumpLog[] heatpumpLogs = new Heatpump().doHeatpumpGapRequest(gapStartTime);
fillTheGaps(heatpumpLogs);
gapStartTime = nextFillTime;
nextFillTime = ZonedDateTime.now()
.plusMinutes(timeWindow)
.plusSeconds(new Random().nextInt(20));
System.out.println("Gapfiller next window: " + gapStartTime + " - " + nextFillTime);
}
} catch (Exception e) {
System.out.println("Gapfiller failed: " + e.getMessage());
}
}

private void fillTheGaps(HeatpumpLog[] heatpumplogs) {
if (heatpumplogs.length < 2) {
System.out.println("Please call this method for a bigger period");
return;
}
List<Point> pointsTodo = new ArrayList<>();
int doneCount = 0;
private void fillTheGaps(HeatpumpLog[] heatpumplogs) {
if (heatpumplogs.length < 2) {
System.out.println("Please call this method for a bigger period");
return;
}
List<Point> pointsTodo = new ArrayList<>();
int doneCount = 0;

ZonedDateTime oldestLog = Arrays.stream(heatpumplogs).min(Comparator.comparing(HeatpumpLog::getTimestamp))
.orElseThrow().getTimestamp().truncatedTo(ChronoUnit.SECONDS);
ZonedDateTime newestLog = Arrays.stream(heatpumplogs).max(Comparator.comparing(HeatpumpLog::getTimestamp))
.orElseThrow().getTimestamp().truncatedTo(ChronoUnit.SECONDS);
ZonedDateTime oldestLog = Arrays.stream(heatpumplogs).min(Comparator.comparing(HeatpumpLog::getTimestamp))
.orElseThrow().getTimestamp().truncatedTo(ChronoUnit.SECONDS);
ZonedDateTime newestLog = Arrays.stream(heatpumplogs).max(Comparator.comparing(HeatpumpLog::getTimestamp))
.orElseThrow().getTimestamp().truncatedTo(ChronoUnit.SECONDS);

System.out.println("Filling gaps from " + oldestLog + " to " + newestLog);
System.out.println("Filling gaps from " + oldestLog + " to " + newestLog);

RealInfluxConnection influxConnection = new RealInfluxConnection();
List<ZonedDateTime> influxedTimes = influxConnection.retrieveInfluxedTimesBetween(oldestLog, newestLog);
RealInfluxConnection influxConnection = new RealInfluxConnection();
List<ZonedDateTime> influxedTimes = influxConnection.retrieveInfluxedTimesBetween(oldestLog, newestLog);

for (HeatpumpLog heatpumplog : heatpumplogs) {
if (!influxedTimes.contains(heatpumplog.getTimestamp().truncatedTo(ChronoUnit.SECONDS))) {
Point point = PointMapper.map(heatpumplog);
pointsTodo.add(point);
for (HeatpumpLog heatpumplog : heatpumplogs) {
if (!influxedTimes.contains(heatpumplog.getTimestamp().truncatedTo(ChronoUnit.SECONDS))) {
Point point = PointMapper.map(heatpumplog);
pointsTodo.add(point);

} else {
doneCount++;
}
}
System.out.println("\nWill fill gaps: " + pointsTodo.size() + ", already logged: " + doneCount);
if (doneCount > 4) { // detect errors
for (Point point : pointsTodo) {
influxConnection.writePoint(point);
}
}
System.out.println("Gapfiller done!");
} else {
doneCount++;
}
}
System.out.println("\nWill fill gaps: " + pointsTodo.size() + ", already logged: " + doneCount);
if (doneCount > 4) { // detect errors
for (Point point : pointsTodo) {
influxConnection.writePoint(point);
}
}
System.out.println("Gapfiller done!");
}
}
Loading

0 comments on commit 883c2b2

Please sign in to comment.