1
0

implement full DatabaseConnector and do some tests

This commit is contained in:
Rémi Heredero 2023-05-17 20:52:03 +02:00
parent ee64e95be3
commit e306d5eacb
3 changed files with 165 additions and 40 deletions

View File

@ -1,5 +1,8 @@
package ch.hevs.isi; package ch.hevs.isi;
import ch.hevs.isi.core.BooleanDataPoint;
import ch.hevs.isi.core.FloatDataPoint;
import ch.hevs.isi.db.DatabaseConnector;
import ch.hevs.isi.utils.Utility; import ch.hevs.isi.utils.Utility;
public class MinecraftController { public class MinecraftController {
@ -74,5 +77,20 @@ public class MinecraftController {
// Start coding here ... // Start coding here ...
// Initialize the database connector
if((dbProtocol != null) && (dbHostName != null)){
DatabaseConnector.url = dbProtocol+ "://" + dbHostName;
DatabaseConnector.getMySelf().initialize(dbProtocol+ "://" + dbHostName);
}
if(dbName != null){
DatabaseConnector.org = dbName;
}
if(dbUserName != null){
DatabaseConnector.bucket = dbUserName;
}
// Initialize the Modbus TCP connector
} }
} }

View File

@ -1,86 +1,190 @@
package ch.hevs.isi.db; package ch.hevs.isi.db;
import ch.hevs.isi.MinecraftController;
import ch.hevs.isi.core.DataPoint; 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.utils.Utility;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL; import java.net.URL;
import java.util.Properties; import java.util.Properties;
public class DatabaseConnector implements DataPointListener { public class DatabaseConnector implements DataPointListener {
Properties properties = new Properties(); private Properties properties = new Properties(); // Properties of the config.properties file
String default_token = System.getenv("SECRET_TOKEN"); private String fullURL = null; // Full URL of the InfluxDB server
String default_url; private URL urlForWrite = null; // URL of the InfluxDB server
String default_org; private HttpURLConnection con = null; // Connection to the InfluxDB server
String default_bucket; private boolean initialized = false; // Boolean to know if the database connector is initialized
String fullURL; private TimeManager _timeManager = new TimeManager(3); // Time manager to manage the time of the data points
URL myURL; private long _timestamp = 0; // Timestamp of the data points
HttpURLConnection con; String default_token = System.getenv("SECRET_TOKEN"); // Token to access the InfluxDB server
public static String url = null; // URL of the InfluxDB server
public static String org = null; // Organization of the InfluxDB server
public static String bucket = null; // Bucket of the InfluxDB server
private static DatabaseConnector mySelf = null; private static DatabaseConnector mySelf = null;
/**
* Private constructor of the database connector
* Read the config.properties file and initialize the database connector
*/
private DatabaseConnector (){ private DatabaseConnector (){
// Read the config.properties file
try (InputStream input = new FileInputStream("src/config.properties")) { try (InputStream input = new FileInputStream("src/config.properties")) {
properties.load(input); properties.load(input);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
default_url = properties.getProperty("URL");
default_org = properties.getProperty("ORG"); // Get the URL, the organization and the bucket from the config.properties file if their are null
default_bucket = properties.getProperty("BUCKET"); if (url == null){
url = properties.getProperty("URL");
}
if (org == null){
org = properties.getProperty("ORG");
}
if (bucket == null){
bucket = properties.getProperty("BUCKET");
}
} }
/**
* Get the instance of the database connector
* @return The instance of the database connector
*/
public static DatabaseConnector getMySelf(){ public static DatabaseConnector getMySelf(){
if (mySelf == null){ if (mySelf == null){
mySelf = new DatabaseConnector(); mySelf = new DatabaseConnector();
} }
return mySelf; 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); * Initialize the database connector
* @param url URL of the database. If null take the URL from the config.properties file
*/
public void initialize(String url){
try{ try{
myURL = new URL(fullURL); if(urlForWrite == null){
if(url == null){
url = properties.getProperty("URL");
}
fullURL = url + "/api/v2/write?org=" + org + "&bucket=" + bucket;
Utility.pDebug("URL: " + fullURL);
urlForWrite = new URL(fullURL);
}
initialized = true;
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
}
// If the user want to erase the previous data in the database
// Delete the previous data
if(MinecraftController.ERASE_PREVIOUS_DATA_INB_DB){
try {
// Create the URL to delete the data
fullURL = url + "/api/v2/delete?org=" + org + "&bucket=" + bucket;
Utility.pDebug("URL: " + fullURL);
URL urlForDelete = new URL(fullURL);
// Create the connection to the database
con = (HttpURLConnection) urlForDelete.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Authorization", "Token " + default_token);
con.setRequestProperty("Content-Type", "text/plain");
con.setRequestProperty("Accept", "application/json");
con.setDoOutput(true);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
} catch (ProtocolException e) {
throw new RuntimeException(e);
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
// Create the data to send to delete the data in the database and send it
String data = "{\n";
data += "\"start\": \"2000-01-01T00:00:00Z\",\n";
data += "\"stop\": \"2024-04-25T10:45:00Z\",\n";
data += "\"predicate\": \"_measurement=\\\"Minecraft\\\"\"\n";
data += "}";
Utility.pDebug(data);
sendDataToDatabase(data);
fullURL=null; // Reset the full URL
}
} }
private void pushToDatabase(DataPoint dp){ /**
String data = "measurement " + dp.toString(); * Push the data point to the database
* @param dp Data point to push
*/
private void pushToDatabase(DataPoint dp) {
// Initialize the database connector if not already done
if(initialized == false){
initialize(null);
}
// Create the data to send
String data = "Minecraft ";
data += dp.toString();
data += " " + _timestamp;
// Send the data to the database
sendDataToDatabase(data);
}
private void sendDataToDatabase(String data){
try { try {
HttpURLConnection con = (HttpURLConnection) myURL.openConnection(); // Create connection and set headers
if(con == null){
con = (HttpURLConnection) urlForWrite.openConnection();
con.setRequestMethod("POST"); con.setRequestMethod("POST");
con.setRequestProperty("Authorization", "Token " + default_token); con.setRequestProperty("Authorization", "Token " + default_token);
con.setRequestProperty("Content-Type", "application/json"); con.setRequestProperty("Content-Type", "application/json");
con.setDoOutput(true); con.setDoOutput(true);
}
// Send data
OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream()); OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream());
writer.write(data); writer.write(data);
writer.flush(); writer.flush();
// Get response code
int respondCode = con.getResponseCode(); int respondCode = con.getResponseCode();
System.out.println(dp.toString() + " -> Database. Code: " + respondCode); if(respondCode != 204){
System.out.println("Error: " + respondCode + ", Data: " + data);
} else {
System.out.println(data + " -> Database");
}
con.disconnect(); con.disconnect();
con = null;
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
/**
* Push the data point to the database
* @param dp Data point to push
*/
@Override @Override
public void onNewValue(DataPoint dp) { public void onNewValue(DataPoint dp) {
if(dp.getLabel().equals("CLOAK_FLOAT")) {
FloatDataPoint fdp = (FloatDataPoint) dp;
_timeManager.setTimestamp(fdp.getValue());
_timestamp = _timeManager.getNanosForDB();
}
if(_timestamp != 0){
pushToDatabase(dp); pushToDatabase(dp);
} }
}
} }

View File

@ -1,3 +1,5 @@
import ch.hevs.isi.MinecraftController;
import ch.hevs.isi.core.BooleanDataPoint;
import ch.hevs.isi.core.DataPoint; import ch.hevs.isi.core.DataPoint;
import ch.hevs.isi.core.FloatDataPoint; import ch.hevs.isi.core.FloatDataPoint;
import ch.hevs.isi.db.DatabaseConnector; import ch.hevs.isi.db.DatabaseConnector;
@ -8,19 +10,20 @@ import java.util.Properties;
public class Database { public class Database {
public static void main(String[] args) { public static void main(String[] args) {
Properties properties = new Properties(); MinecraftController.ERASE_PREVIOUS_DATA_INB_DB = true;
try (InputStream input = new FileInputStream("src/config.properties")) { FloatDataPoint clock = new FloatDataPoint("CLOAK_FLOAT", false);
properties.load(input);
} catch (Exception e) {
e.printStackTrace();
}
String url = properties.getProperty("URL");
DatabaseConnector db = DatabaseConnector.getMySelf();
db.initialize(url);
FloatDataPoint gridVoltage = new FloatDataPoint("GRID_U_FLOAT", true); FloatDataPoint gridVoltage = new FloatDataPoint("GRID_U_FLOAT", true);
gridVoltage.setValue(500); BooleanDataPoint solarPanel = new BooleanDataPoint("REMOTE_SOLAR_SW", true);
DatabaseConnector.getMySelf().initialize(null);
clock.setValue(0f);
for (float i = 0; i < 3; i += 0.1f) {
System.out.println("");
clock.setValue(i);
gridVoltage.setValue(750 + (100*i)%100);
}
} }
} }