Skip to content

Commit

Permalink
Clean up properties
Browse files Browse the repository at this point in the history
  • Loading branch information
zeekoe committed Jan 29, 2024
1 parent 24ded4b commit 959579a
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 88 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>bluebird</groupId>
<artifactId>bluebird</artifactId>
<version>0.2-SNAPSHOT</version>
<version>0.3-SNAPSHOT</version>
<build>
<plugins>
<plugin>
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/com/github/zeekoe/bluebird/Const.java

This file was deleted.

40 changes: 7 additions & 33 deletions src/main/java/com/github/zeekoe/bluebird/heatpump/Auth.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,22 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.zeekoe.bluebird.heatpump.model.Token;
import com.github.zeekoe.bluebird.infrastructure.MyHttpClient;
import com.github.zeekoe.bluebird.Const;

import java.io.FileInputStream;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Map;
import java.util.Properties;

public class Auth {
static {
Properties properties = new Properties();
try {
properties.load(new FileInputStream(Const.CONFIG_FILE_PATH));
USERNAME = properties.getProperty("bluebird.username");
PASSWORD = properties.getProperty("bluebird.password");
API_KEY = properties.getProperty("bluebird.apikey");
LOG_URL = properties.getProperty("bluebird.logurl");
TOKEN_URL = "https://auth.weheat.nl/auth/realms/Weheat/protocol/openid-connect/token";

} catch (IOException e) {
e.printStackTrace();
}
}
import static com.github.zeekoe.bluebird.infrastructure.BluebirdProperties.property;
import static com.github.zeekoe.bluebird.infrastructure.BluebirdProperty.WEHEAT_PASSWORD;
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 String USERNAME;
private static String PASSWORD;
private static String API_KEY;
private static String LOG_URL;
private static String TOKEN_URL;
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 getApikey() {
return API_KEY;
}

public String getLogurl() {
return LOG_URL;
}

public String getToken() throws IOException, InterruptedException {
if (token == null) {
System.out.println("Retrieving token");
Expand Down Expand Up @@ -77,8 +51,8 @@ private String doLogin() throws IOException, InterruptedException {
"grant_type", "password",
"scope", "openid",
"client_id", "WeheatCommunityAPI",
"username", USERNAME,
"password", PASSWORD
"username", property(WEHEAT_USERNAME),
"password", property(WEHEAT_PASSWORD)
));
}
}
21 changes: 16 additions & 5 deletions src/main/java/com/github/zeekoe/bluebird/heatpump/Heatpump.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.github.zeekoe.bluebird.heatpump.model.HeatpumpLog;
import com.github.zeekoe.bluebird.influx.Influx;
import com.github.zeekoe.bluebird.influx.InfluxConnection;
import com.github.zeekoe.bluebird.influx.RealInfluxConnection;
import com.github.zeekoe.bluebird.infrastructure.MyHttpClient;
import org.influxdb.dto.Point;

Expand All @@ -12,16 +13,27 @@
import java.time.ZoneId;
import java.util.concurrent.TimeUnit;

import static com.github.zeekoe.bluebird.infrastructure.BluebirdProperties.property;
import static com.github.zeekoe.bluebird.infrastructure.BluebirdProperty.INFLUXDB_MEASUREMENT;
import static com.github.zeekoe.bluebird.infrastructure.BluebirdProperty.WEHEAT_LOG_URL;

public class Heatpump implements Runnable {

public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper()
.registerModule(new JavaTimeModule());
private static final MyHttpClient httpClient = new MyHttpClient();

private final Auth auth;
private final InfluxConnection influxConnection;

public Heatpump() {
auth = new Auth();
influxConnection = new RealInfluxConnection();
}

public Heatpump(InfluxConnection influxConnection) {
auth = new Auth();
this.influxConnection = influxConnection;
}

@Override
Expand All @@ -40,8 +52,7 @@ public void run() {
}

private void influx(HeatpumpLog heatpumpLog) {
final Influx influx = new Influx(); // TODO reuse Influx instance
final Point point = Point.measurement(influx.getMeasurementIdentifier())
final Point point = Point.measurement(property(INFLUXDB_MEASUREMENT))
.time(LocalDateTime.now().atZone(ZoneId.systemDefault()).toEpochSecond(), TimeUnit.SECONDS)
.addField("state", heatpumpLog.getState())
.addField("t_1", heatpumpLog.getT1())
Expand Down Expand Up @@ -72,10 +83,10 @@ private void influx(HeatpumpLog heatpumpLog) {
.addField("ot_boiler_return_temperature", heatpumpLog.getOt_boiler_return_temperature())
.addField("error", heatpumpLog.getError())
.build();
influx.writePoint(point);
influxConnection.writePoint(point);
}

private String doHeatpumpRequest() throws IOException, InterruptedException {
return httpClient.get(auth.getLogurl(), auth.getToken());
return httpClient.get(property(WEHEAT_LOG_URL), auth.getToken());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.github.zeekoe.bluebird.influx;

import org.influxdb.dto.Point;

public class DummyInfluxConnection implements InfluxConnection{
@Override
public void writePoint(Point point) {
System.out.println(point);
}
}
44 changes: 0 additions & 44 deletions src/main/java/com/github/zeekoe/bluebird/influx/Influx.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.github.zeekoe.bluebird.influx;

import org.influxdb.dto.Point;

public interface InfluxConnection {
void writePoint(Point point);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.github.zeekoe.bluebird.influx;

import org.influxdb.InfluxDB;
import org.influxdb.InfluxDBFactory;
import org.influxdb.dto.Point;

import static com.github.zeekoe.bluebird.infrastructure.BluebirdProperties.property;
import static com.github.zeekoe.bluebird.infrastructure.BluebirdProperty.INFLUXDB_DATABASE;
import static com.github.zeekoe.bluebird.infrastructure.BluebirdProperty.INFLUXDB_PASSWORD;
import static com.github.zeekoe.bluebird.infrastructure.BluebirdProperty.INFLUXDB_URL;
import static com.github.zeekoe.bluebird.infrastructure.BluebirdProperty.INFLUXDB_USERNAME;

public class RealInfluxConnection implements InfluxConnection {
private final InfluxDB influxDB;

public RealInfluxConnection() {
influxDB = InfluxDBFactory.connect(
property(INFLUXDB_URL),
property(INFLUXDB_USERNAME),
property(INFLUXDB_PASSWORD));
}

public void writePoint(Point point) {
influxDB.setDatabase(property(INFLUXDB_DATABASE));
influxDB.write(point);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.github.zeekoe.bluebird.infrastructure;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class BluebirdProperties {
private static final Properties properties;

static {
final String configFile = "/etc/bluebird.config";
properties = new Properties();
try {
properties.load(new FileInputStream(configFile));
} catch (IOException e) {
throw new RuntimeException("Error in loading properties file: " + configFile, e);
}
}

public static String property(BluebirdProperty property) {
return properties.getProperty(property.getKey());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.github.zeekoe.bluebird.infrastructure;


public enum BluebirdProperty {
WEHEAT_USERNAME("bluebird.username"),
WEHEAT_PASSWORD("bluebird.password"),
WEHEAT_LOG_URL("bluebird.logurl"),

INFLUXDB_URL("influxdb.url"),
INFLUXDB_DATABASE("influxdb.database"),
INFLUXDB_USERNAME("influxdb.username"),
INFLUXDB_PASSWORD("influxdb.password"),
INFLUXDB_MEASUREMENT("influxdb.bluebird.measurement");

private final String key;

BluebirdProperty(String key) {
this.key = key;
}

public String getKey() {
return key;
}
}
13 changes: 13 additions & 0 deletions src/test/java/com/github/zeekoe/bluebird/HeatpumpTestRunner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.github.zeekoe.bluebird;

import com.github.zeekoe.bluebird.heatpump.Heatpump;
import com.github.zeekoe.bluebird.influx.DummyInfluxConnection;

public class HeatpumpTestRunner {
public static void main(String[] args) {
final Heatpump heatpump = new Heatpump(new DummyInfluxConnection());
heatpump.run();
// final LocalDateTime localDate = Instant.ofEpochSecond(1706523458).atOffset(ZoneOffset.ofHours(1)).toLocalDateTime();
// System.out.println(localDate);
}
}

0 comments on commit 959579a

Please sign in to comment.