create DatabaseConnector and test it
This commit is contained in:
parent
cdc5dddb80
commit
7e9ae2e1f1
@ -1,4 +1,3 @@
|
||||
URL = https://influx.sdi.hevs.ch
|
||||
ORG = SIn15
|
||||
BUCKET = SIn15
|
||||
TOKEN = ${SECRET_TOKEN}
|
@ -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
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user