forked from zeekoe/bluebird
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
zeekoe
committed
Oct 21, 2023
1 parent
231880d
commit 8af65bf
Showing
11 changed files
with
827 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
### IntelliJ IDEA ### | ||
/target/ | ||
/.idea/ | ||
out/ | ||
!**/src/main/**/out/ | ||
!**/src/test/**/out/ | ||
|
||
### Eclipse ### | ||
.apt_generated | ||
.classpath | ||
.factorypath | ||
.project | ||
.settings | ||
.springBeans | ||
.sts4-cache | ||
bin/ | ||
!**/src/main/**/bin/ | ||
!**/src/test/**/bin/ | ||
|
||
### NetBeans ### | ||
/nbproject/private/ | ||
/nbbuild/ | ||
/dist/ | ||
/nbdist/ | ||
/.nb-gradle/ | ||
|
||
### VS Code ### | ||
.vscode/ | ||
|
||
### Mac OS ### | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,27 @@ | ||
# bluebird | ||
# Bluebird | ||
|
||
Tool to put information from a Blackbird into InfluxDB. | ||
|
||
## Building | ||
|
||
`mvn clean install` | ||
|
||
## Configuring | ||
|
||
Create a config file `/etc/bluebird.config` and fill the following: | ||
``` | ||
[email protected] | ||
bluebird.password=somesecretpassword | ||
bluebird.apikey=a-very-long-key | ||
bluebird.tokenurl=https://some-url-ending-with/token | ||
bluebird.logurl=https://some-url-ending-with/heatpump_logs?select=*&heatpump_serial_number=eq.<serialnumber>&order=timestamp.desc&limit=1 | ||
influxdb.bluebird.measurement=bluebird | ||
influxdb.url=http:https://your-influx-server:8086 | ||
influxdb.username=username | ||
influxdb.password=password | ||
``` | ||
|
||
## Running | ||
|
||
`java -jar target/bluebird-0.1-SNAPSHOT-jar-with-dependencies.jar` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http:https://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http:https://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http:https://maven.apache.org/POM/4.0.0 http:https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>bluebird</groupId> | ||
<artifactId>bluebird</artifactId> | ||
<version>0.1-SNAPSHOT</version> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-assembly-plugin</artifactId> | ||
<executions> | ||
<execution> | ||
<phase>package</phase> | ||
<goals> | ||
<goal>single</goal> | ||
</goals> | ||
<configuration> | ||
<archive> | ||
<manifest> | ||
<mainClass> | ||
com.github.zeekoe.bluebird.Bluebird | ||
</mainClass> | ||
</manifest> | ||
</archive> | ||
<descriptorRefs> | ||
<descriptorRef>jar-with-dependencies</descriptorRef> | ||
</descriptorRefs> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<configuration> | ||
<source>11</source> | ||
<target>11</target> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.fasterxml.jackson.core</groupId> | ||
<artifactId>jackson-databind</artifactId> | ||
<version>2.15.3</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.fasterxml.jackson.datatype</groupId> | ||
<artifactId>jackson-datatype-jsr310</artifactId> | ||
<version>2.15.3</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.influxdb</groupId> | ||
<artifactId>influxdb-java</artifactId> | ||
<version>2.23</version> | ||
</dependency> | ||
|
||
<!-- <dependency>--> | ||
<!-- <groupId>org.junit.jupiter</groupId>--> | ||
<!-- <artifactId>junit-jupiter</artifactId>--> | ||
<!-- <version>5.8.2</version>--> | ||
<!-- </dependency>--> | ||
|
||
<!-- <!– https://mvnrepository.com/artifact/org.mockito/mockito-junit-jupiter –>--> | ||
<!-- <dependency>--> | ||
<!-- <groupId>org.mockito</groupId>--> | ||
<!-- <artifactId>mockito-junit-jupiter</artifactId>--> | ||
<!-- <version>5.5.0</version>--> | ||
<!-- <scope>test</scope>--> | ||
<!-- </dependency>--> | ||
|
||
<!-- <!– https://mvnrepository.com/artifact/org.mockito/mockito-core –>--> | ||
<!-- <dependency>--> | ||
<!-- <groupId>org.mockito</groupId>--> | ||
<!-- <artifactId>mockito-core</artifactId>--> | ||
<!-- <version>5.5.0</version>--> | ||
<!-- <scope>test</scope>--> | ||
<!-- </dependency>--> | ||
|
||
</dependencies> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.github.zeekoe.bluebird; | ||
|
||
import com.github.zeekoe.bluebird.heatpump.Heatpump; | ||
|
||
public class Bluebird { | ||
public static void main(String[] args) { | ||
new Retryer<>(Heatpump.class).startRunning(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.github.zeekoe.bluebird; | ||
|
||
public class Retryer<T extends Runnable> { | ||
private static final int MAX_RETRY_COUNT = 3; | ||
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 void startRunning() { | ||
while (retryCount <= MAX_RETRY_COUNT) { | ||
try { | ||
runnable.run(); | ||
retryCount = 1; | ||
} catch (Exception e) { | ||
retryCount++; | ||
System.out.println("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); | ||
} | ||
} | ||
} | ||
|
||
|
95 changes: 95 additions & 0 deletions
95
src/main/java/com/github/zeekoe/bluebird/heatpump/Auth.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package com.github.zeekoe.bluebird.heatpump; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.github.zeekoe.bluebird.heatpump.model.Token; | ||
|
||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
import java.time.Duration; | ||
import java.time.LocalDateTime; | ||
import java.util.Properties; | ||
|
||
public class Auth { | ||
static { | ||
Properties properties = new Properties(); | ||
try { | ||
properties.load(new FileInputStream("/etc/bluebird.config")); | ||
USERNAME = properties.getProperty("bluebird.username"); | ||
PASSWORD = properties.getProperty("bluebird.password"); | ||
API_KEY = properties.getProperty("bluebird.apikey"); | ||
LOG_URL = properties.getProperty("bluebird.logurl"); | ||
TOKEN_URL = properties.getProperty("bluebird.tokenurl"); | ||
|
||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
private static final HttpClient httpClient = HttpClient.newBuilder() | ||
.version(HttpClient.Version.HTTP_2) | ||
.connectTimeout(Duration.ofSeconds(10)) | ||
.build(); | ||
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 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"); | ||
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 { | ||
String url = TOKEN_URL + "?grant_type=refresh_token"; | ||
String request = "{\"refresh_token\":\"" + token.getRefresh_token() + "\"}"; | ||
return doRequest(request, url); | ||
} | ||
|
||
private String doLogin() throws IOException, InterruptedException { | ||
String url = TOKEN_URL + "?grant_type=password"; | ||
String request = "{\"email\":\"" + USERNAME + "\",\"password\":\"" + PASSWORD + "\",\"gotrue_meta_security\":{}}"; | ||
System.out.print(request); | ||
return doRequest(request, url); | ||
} | ||
|
||
private static String doRequest(String requestPayload, String loginUrl) throws IOException, InterruptedException { | ||
HttpRequest request = HttpRequest.newBuilder() | ||
.POST(HttpRequest.BodyPublishers.ofString(requestPayload)) | ||
.uri(URI.create(loginUrl)) | ||
.setHeader("User-Agent", "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/116.0") // add request header | ||
.setHeader("Content-Type", "application/json;charset=UTF-8") | ||
.setHeader("apikey", API_KEY) | ||
.build(); | ||
|
||
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString()); | ||
|
||
if(response.statusCode() != 200) { | ||
throw new RuntimeException("Incorrect status code in token retrieval " + response); | ||
} | ||
|
||
return response.body(); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/com/github/zeekoe/bluebird/heatpump/Executable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.github.zeekoe.bluebird.heatpump; | ||
|
||
@FunctionalInterface | ||
public interface Executable { | ||
void execute() throws Exception; | ||
} |
Oops, something went wrong.