diff --git a/src/config.properties b/src/config.properties index 0b3e573..bba40f7 100644 --- a/src/config.properties +++ b/src/config.properties @@ -1,4 +1,3 @@ URL = https://influx.sdi.hevs.ch ORG = SIn15 -BUCKET = SIn15 -TOKEN = ${SECRET_TOKEN} \ No newline at end of file +BUCKET = SIn15 \ No newline at end of file diff --git a/src/main/java/ch/hevs/isi/db/DatabaseConnector.java b/src/main/java/ch/hevs/isi/db/DatabaseConnector.java index 6c8b5df..9cf3507 100644 --- a/src/main/java/ch/hevs/isi/db/DatabaseConnector.java +++ b/src/main/java/ch/hevs/isi/db/DatabaseConnector.java @@ -3,14 +3,38 @@ package ch.hevs.isi.db; import ch.hevs.isi.core.DataPoint; import ch.hevs.isi.core.DataPointListener; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStreamWriter; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Properties; public class DatabaseConnector implements DataPointListener { - String token = "bW6-s_EkTkZ-rT-Mj0kt1uZQYOPKio5FdEK-OtwWJlxDwYQ5OkH599t1jUz4VV47SrkKonLDHB0jRJ3Eu6OUTA=="; + Properties properties = new Properties(); + String default_token = System.getenv("SECRET_TOKEN"); + String default_url; + String default_org; + String default_bucket; + String fullURL; + URL myURL; + HttpURLConnection con; + + private static DatabaseConnector mySelf = null; private DatabaseConnector (){ - + try (InputStream input = new FileInputStream("src/config.properties")) { + properties.load(input); + } catch (Exception e) { + e.printStackTrace(); + } + default_url = properties.getProperty("URL"); + default_org = properties.getProperty("ORG"); + default_bucket = properties.getProperty("BUCKET"); } public static DatabaseConnector getMySelf(){ @@ -20,11 +44,39 @@ public class DatabaseConnector implements DataPointListener { return mySelf; } public void initialize(String url){ + String org = default_org; + String bucket = default_bucket; + + fullURL = url + "/api/v2/write?org=" + org + "&bucket=" + bucket; + System.out.println(fullURL); + try{ + myURL = new URL(fullURL); + } catch (MalformedURLException e) { + throw new RuntimeException(e); + } catch (IOException e) { + throw new RuntimeException(e); + } + } private void pushToDatabase(DataPoint dp){ - System.out.println(dp.toString() + " -> Database"); + String data = "measurement " + dp.toString(); + try { + HttpURLConnection con = (HttpURLConnection) myURL.openConnection(); + con.setRequestMethod("POST"); + con.setRequestProperty("Authorization", "Token " + default_token); + con.setRequestProperty("Content-Type", "application/json"); + con.setDoOutput(true); + OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream()); + writer.write(data); + writer.flush(); + int respondCode = con.getResponseCode(); + System.out.println(dp.toString() + " -> Database. Code: " + respondCode); + con.disconnect(); + } catch (IOException e) { + e.printStackTrace(); + } } @Override diff --git a/src/test/java/Database.java b/src/test/java/Database.java index 6f2ffb7..f490f3c 100644 --- a/src/test/java/Database.java +++ b/src/test/java/Database.java @@ -1,8 +1,11 @@ +import ch.hevs.isi.core.DataPoint; +import ch.hevs.isi.core.FloatDataPoint; +import ch.hevs.isi.db.DatabaseConnector; + import java.io.FileInputStream; import java.io.InputStream; import java.util.Properties; - public class Database { public static void main(String[] args) { Properties properties = new Properties(); @@ -11,8 +14,13 @@ public class Database { } catch (Exception e) { e.printStackTrace(); } - String token = properties.getProperty("TOKEN"); + String url = properties.getProperty("URL"); + + DatabaseConnector db = DatabaseConnector.getMySelf(); + db.initialize(url); + + FloatDataPoint gridVoltage = new FloatDataPoint("GRID_U_FLOAT", true); + gridVoltage.setValue(500); - System.out.println(token); } }