create DatabaseConnector and test it
This commit is contained in:
		| @@ -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 | ||||
|   | ||||
		Reference in New Issue
	
	Block a user