1
0

Merge pull request #26 from HEI-SYND-221-231-SIn/comments/DB

Comments/db
This commit is contained in:
Nils Ritler 2023-06-16 10:52:17 +02:00 committed by GitHub
commit a245711209
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 11 deletions

View File

@ -6,7 +6,6 @@ import ch.hevs.isi.core.DataPoint;
import ch.hevs.isi.core.DataPointListener; import ch.hevs.isi.core.DataPointListener;
import ch.hevs.isi.core.FloatDataPoint; import ch.hevs.isi.core.FloatDataPoint;
import ch.hevs.isi.utils.Utility; import ch.hevs.isi.utils.Utility;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
@ -19,7 +18,7 @@ import java.util.Properties;
public class DatabaseConnector implements DataPointListener { 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 URL urlForWrite = null; // URL of the InfluxDB server
private HttpURLConnection con = null; // Connection to 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 private boolean initialized = false; // Boolean to know if the database connector is initialized
@ -77,7 +76,7 @@ public class DatabaseConnector implements DataPointListener {
*/ */
public void initialize(String url){ public void initialize(String url){
// Full URL of the InfluxDB server // Full URL of the InfluxDB server
String fullURL = null; String fullURL;
try{ try{
if(urlForWrite == null){ if(urlForWrite == null){
if(url == null){ if(url == null){
@ -119,13 +118,12 @@ public class DatabaseConnector implements DataPointListener {
data += "}"; data += "}";
Utility.pDebug(data); Utility.pDebug(data);
sendDataToDatabase(data); sendDataToDatabase(data);
fullURL =null; // Reset the full URL
} }
} }
/** /**
* Push the data point to the database * Add the timestamp to the data point
* @param dp Data point to push * @param dp Data point to add the timestamp
*/ */
private void pushToDatabase(DataPoint dp) { private void pushToDatabase(DataPoint dp) {
// Initialize the database connector if not already done // Initialize the database connector if not already done
@ -142,6 +140,12 @@ public class DatabaseConnector implements DataPointListener {
sendDataToDatabase(data); 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){ private void sendDataToDatabase(String data){
try { try {
// Create connection and set headers // Create connection and set headers

View File

@ -1,23 +1,30 @@
import ch.hevs.isi.MinecraftController; import ch.hevs.isi.MinecraftController;
import ch.hevs.isi.core.BooleanDataPoint;
import ch.hevs.isi.core.FloatDataPoint; import ch.hevs.isi.core.FloatDataPoint;
import ch.hevs.isi.db.DatabaseConnector; 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 class Database {
public static void main(String[] args) { public static void main(String[] args) {
// set the erase parameter to true
MinecraftController.ERASE_PREVIOUS_DATA_INB_DB = true; MinecraftController.ERASE_PREVIOUS_DATA_INB_DB = true;
// Create 2 data points
FloatDataPoint clock = new FloatDataPoint("CLOCK_FLOAT", false); FloatDataPoint clock = new FloatDataPoint("CLOCK_FLOAT", false);
FloatDataPoint gridVoltage = new FloatDataPoint("GRID_U_FLOAT", true); FloatDataPoint gridVoltage = new FloatDataPoint("GRID_U_FLOAT", true);
BooleanDataPoint solarPanel = new BooleanDataPoint("REMOTE_SOLAR_SW", true);
// Initialize the database connector
DatabaseConnector.getMySelf().initialize(null); DatabaseConnector.getMySelf().initialize(null);
// Set the time to 0
clock.setValue(0f); 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) { for (float i = 0; i < 3; i += 0.1f) {
System.out.println(""); System.out.println();
clock.setValue(i); clock.setValue(i);
gridVoltage.setValue(750 + (100*i)%100); gridVoltage.setValue(750 + (100*i)%100);
} }