From b1d5b13b04c66059dc885b716d7bf3829bb8475b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Heredero?= Date: Thu, 15 Jun 2023 17:25:39 +0200 Subject: [PATCH 1/2] add comments on Database part --- .../java/ch/hevs/isi/db/DatabaseConnector.java | 13 +++++++++---- src/test/java/Database.java | 14 +++++++++++--- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/main/java/ch/hevs/isi/db/DatabaseConnector.java b/src/main/java/ch/hevs/isi/db/DatabaseConnector.java index 5a2bbc0..f5b363b 100644 --- a/src/main/java/ch/hevs/isi/db/DatabaseConnector.java +++ b/src/main/java/ch/hevs/isi/db/DatabaseConnector.java @@ -6,7 +6,6 @@ import ch.hevs.isi.core.DataPoint; import ch.hevs.isi.core.DataPointListener; import ch.hevs.isi.core.FloatDataPoint; import ch.hevs.isi.utils.Utility; - import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWriter; @@ -19,7 +18,7 @@ import java.util.Properties; public class DatabaseConnector implements DataPointListener { - private final Properties properties = new Properties(); // Properties of the config.properties file + private final Properties properties = new Properties(); // Properties of the config.properties file private URL urlForWrite = null; // URL of the InfluxDB server private HttpURLConnection con = null; // Connection to the InfluxDB server private boolean initialized = false; // Boolean to know if the database connector is initialized @@ -124,8 +123,8 @@ public class DatabaseConnector implements DataPointListener { } /** - * Push the data point to the database - * @param dp Data point to push + * Add the timestamp to the data point + * @param dp Data point to add the timestamp */ private void pushToDatabase(DataPoint dp) { // Initialize the database connector if not already done @@ -142,6 +141,12 @@ public class DatabaseConnector implements DataPointListener { sendDataToDatabase(data); } + /** + * Create and formate the request to send to the database + * Send the request to the database + * Check if the data is correctly send to the database with the response code + * @param data Data to send to the database + */ private void sendDataToDatabase(String data){ try { // Create connection and set headers diff --git a/src/test/java/Database.java b/src/test/java/Database.java index cae80de..d5518ad 100644 --- a/src/test/java/Database.java +++ b/src/test/java/Database.java @@ -2,20 +2,28 @@ import ch.hevs.isi.MinecraftController; import ch.hevs.isi.core.BooleanDataPoint; import ch.hevs.isi.core.FloatDataPoint; import ch.hevs.isi.db.DatabaseConnector; -import ch.hevs.isi.field.FieldConnector; -import ch.hevs.isi.web.WebConnector; +/** + * Class to test the database connector + * + */ public class Database { public static void main(String[] args) { + // set the erase parameter to true MinecraftController.ERASE_PREVIOUS_DATA_INB_DB = true; + + // Create 2 data points FloatDataPoint clock = new FloatDataPoint("CLOCK_FLOAT", false); FloatDataPoint gridVoltage = new FloatDataPoint("GRID_U_FLOAT", true); - BooleanDataPoint solarPanel = new BooleanDataPoint("REMOTE_SOLAR_SW", true); + // Initialize the database connector DatabaseConnector.getMySelf().initialize(null); + // Set the time to 0 clock.setValue(0f); + // Set the grid voltage to 750 + // and make a sawtooth from 750 to 850 and back to 750 in 30 steps for (float i = 0; i < 3; i += 0.1f) { System.out.println(""); clock.setValue(i); From fe9a766ada1befdda9364f7070e02ff7165e9f26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Heredero?= Date: Thu, 15 Jun 2023 17:26:33 +0200 Subject: [PATCH 2/2] really small polish --- src/main/java/ch/hevs/isi/db/DatabaseConnector.java | 3 +-- src/test/java/Database.java | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main/java/ch/hevs/isi/db/DatabaseConnector.java b/src/main/java/ch/hevs/isi/db/DatabaseConnector.java index f5b363b..61580b3 100644 --- a/src/main/java/ch/hevs/isi/db/DatabaseConnector.java +++ b/src/main/java/ch/hevs/isi/db/DatabaseConnector.java @@ -76,7 +76,7 @@ public class DatabaseConnector implements DataPointListener { */ public void initialize(String url){ // Full URL of the InfluxDB server - String fullURL = null; + String fullURL; try{ if(urlForWrite == null){ if(url == null){ @@ -118,7 +118,6 @@ public class DatabaseConnector implements DataPointListener { data += "}"; Utility.pDebug(data); sendDataToDatabase(data); - fullURL =null; // Reset the full URL } } diff --git a/src/test/java/Database.java b/src/test/java/Database.java index d5518ad..ad8e26e 100644 --- a/src/test/java/Database.java +++ b/src/test/java/Database.java @@ -1,5 +1,4 @@ import ch.hevs.isi.MinecraftController; -import ch.hevs.isi.core.BooleanDataPoint; import ch.hevs.isi.core.FloatDataPoint; import ch.hevs.isi.db.DatabaseConnector; @@ -25,7 +24,7 @@ public class Database { // Set the grid voltage to 750 // and make a sawtooth from 750 to 850 and back to 750 in 30 steps for (float i = 0; i < 3; i += 0.1f) { - System.out.println(""); + System.out.println(); clock.setValue(i); gridVoltage.setValue(750 + (100*i)%100); }