From 43e45f8d66d73752e1d1976285915ce499e1f71e Mon Sep 17 00:00:00 2001 From: Nils Ritler <79571155+Slisls@users.noreply.github.com> Date: Fri, 12 May 2023 16:09:45 +0200 Subject: [PATCH 1/8] singleton pattern, readfloat and readboolean are done --- .../ch/hevs/isi/field/ModbusAccessor.java | 107 ++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 src/main/java/ch/hevs/isi/field/ModbusAccessor.java diff --git a/src/main/java/ch/hevs/isi/field/ModbusAccessor.java b/src/main/java/ch/hevs/isi/field/ModbusAccessor.java new file mode 100644 index 0000000..2446c7c --- /dev/null +++ b/src/main/java/ch/hevs/isi/field/ModbusAccessor.java @@ -0,0 +1,107 @@ +package ch.hevs.isi.field; + +import com.serotonin.modbus4j.ModbusFactory; +import com.serotonin.modbus4j.ModbusMaster; +import com.serotonin.modbus4j.code.DataType; +import com.serotonin.modbus4j.exception.ErrorResponseException; +import com.serotonin.modbus4j.exception.ModbusTransportException; +import com.serotonin.modbus4j.ip.IpParameters; +import com.serotonin.modbus4j.ip.tcp.TcpMaster; +import com.serotonin.modbus4j.locator.BaseLocator; + +public class ModbusAccessor { + + private static ModbusAccessor mySelf = null; + private ModbusMaster master = null; + + /** + * private constructor + * */ + private ModbusAccessor(){ + + } + + /** + * static method to create a singleton pattern of the class + * checks if an instance of the class is already made + * if not, it creates an instance of the class ModbusAccessor + * @return instance of ModbusAccessor + */ + public static ModbusAccessor getMySelf(){ + if (mySelf == null){ + mySelf = new ModbusAccessor(); + } + return mySelf; + } + + public void connect(String ipAddress, int port){ + ModbusFactory MF = new ModbusFactory(); + IpParameters IpParams = new IpParameters(); + + this.master = MF.createTcpMaster(IpParams, true); + /** + * TODO finish connect() + */ + } + + /** + * methods to write in the modbus registers + */ + + /** + * method to write a boolean value on the modbus address + * @param address the address of the modbus register + * @param value the value to write in the register + */ + public void writeBoolean (int address, boolean value){ + /** + * TODO writeBoolean() + */ + } + + /** + * method to write a float value on the modbus address + * @param address the address of the modbus register + * @param value the value to write in the register + */ + public void writeFloat (int address, float value){ + /** + * TODO writeFloat() + */ + } + + /** + * methods to read from the modbus registers + */ + + /** + * method to read a boolean value in the correct modbus register + * get the coil status of the register. + * @param register address of register + * @return boolean value of the desired register + */ + public boolean readBoolean(int register) throws ModbusTransportException, ErrorResponseException { + /** + * first parameter = slaveID = always 1 because we only have one + * second parameter = register address ==> it is named offset because it starts with 0 and goes to the desired address + */ + boolean booleanValue = this.master.getValue(BaseLocator.coilStatus(1,register)); + return booleanValue; + } + + /** + * method to read a boolean value in the correct modbus register + * get the value from the holding register + * @param register address of the register + * @return float value of the desired register + */ + public float readFloat(int register){ + /** + * first parameter = slaveID = always 1 because we only have one + * second parameter = register address ==> it is named offset because it starts with 0 and goes to the desired address + * thrid parameter = DataType of the value. The max value is 3 Byte Float, but the class DataType has only 2 or 4 byte + */ + float floatValue = this.master.getValue(BaseLocator.holdingRegister(1,register, DataType.FOUR_BYTE_FLOAT); + return floatValue; + } +} From cdc5dddb80c5f93a6b0284325774c020fbc1bb26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Heredero?= Date: Mon, 15 May 2023 09:43:50 +0200 Subject: [PATCH 2/8] initial commit DataBase branche --- src/config.properties | 4 ++++ .../java/ch/hevs/isi/db/DatabaseConnector.java | 2 ++ src/test/java/Database.java | 18 ++++++++++++++++++ 3 files changed, 24 insertions(+) create mode 100644 src/config.properties create mode 100644 src/test/java/Database.java diff --git a/src/config.properties b/src/config.properties new file mode 100644 index 0000000..0b3e573 --- /dev/null +++ b/src/config.properties @@ -0,0 +1,4 @@ +URL = https://influx.sdi.hevs.ch +ORG = SIn15 +BUCKET = SIn15 +TOKEN = ${SECRET_TOKEN} \ No newline at end of file diff --git a/src/main/java/ch/hevs/isi/db/DatabaseConnector.java b/src/main/java/ch/hevs/isi/db/DatabaseConnector.java index 3385bee..6c8b5df 100644 --- a/src/main/java/ch/hevs/isi/db/DatabaseConnector.java +++ b/src/main/java/ch/hevs/isi/db/DatabaseConnector.java @@ -4,7 +4,9 @@ package ch.hevs.isi.db; import ch.hevs.isi.core.DataPoint; import ch.hevs.isi.core.DataPointListener; + public class DatabaseConnector implements DataPointListener { + String token = "bW6-s_EkTkZ-rT-Mj0kt1uZQYOPKio5FdEK-OtwWJlxDwYQ5OkH599t1jUz4VV47SrkKonLDHB0jRJ3Eu6OUTA=="; private static DatabaseConnector mySelf = null; private DatabaseConnector (){ diff --git a/src/test/java/Database.java b/src/test/java/Database.java new file mode 100644 index 0000000..6f2ffb7 --- /dev/null +++ b/src/test/java/Database.java @@ -0,0 +1,18 @@ +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(); + try (InputStream input = new FileInputStream("src/config.properties")) { + properties.load(input); + } catch (Exception e) { + e.printStackTrace(); + } + String token = properties.getProperty("TOKEN"); + + System.out.println(token); + } +} From 7e9ae2e1f1da861dcf341080b1482acac1d1a0b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Heredero?= Date: Wed, 17 May 2023 14:13:27 +0200 Subject: [PATCH 3/8] create DatabaseConnector and test it --- src/config.properties | 3 +- .../ch/hevs/isi/db/DatabaseConnector.java | 58 ++++++++++++++++++- src/test/java/Database.java | 14 ++++- 3 files changed, 67 insertions(+), 8 deletions(-) diff --git a/src/config.properties b/src/config.properties index 0b3e573..bba40f7 100644 --- a/src/config.properties +++ b/src/config.properties @@ -1,4 +1,3 @@ URL = https://influx.sdi.hevs.ch ORG = SIn15 -BUCKET = SIn15 -TOKEN = ${SECRET_TOKEN} \ No newline at end of file +BUCKET = SIn15 \ No newline at end of file diff --git a/src/main/java/ch/hevs/isi/db/DatabaseConnector.java b/src/main/java/ch/hevs/isi/db/DatabaseConnector.java index 6c8b5df..9cf3507 100644 --- a/src/main/java/ch/hevs/isi/db/DatabaseConnector.java +++ b/src/main/java/ch/hevs/isi/db/DatabaseConnector.java @@ -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 diff --git a/src/test/java/Database.java b/src/test/java/Database.java index 6f2ffb7..f490f3c 100644 --- a/src/test/java/Database.java +++ b/src/test/java/Database.java @@ -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); } } From ee64e95be31d23216dc99e32980b6ce55f389621 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Heredero?= Date: Wed, 17 May 2023 14:13:55 +0200 Subject: [PATCH 4/8] fix way to get DataPoint --- src/main/java/ch/hevs/isi/core/BooleanDataPoint.java | 2 +- src/main/java/ch/hevs/isi/core/FloatDataPoint.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/ch/hevs/isi/core/BooleanDataPoint.java b/src/main/java/ch/hevs/isi/core/BooleanDataPoint.java index 5a53db6..a432e70 100644 --- a/src/main/java/ch/hevs/isi/core/BooleanDataPoint.java +++ b/src/main/java/ch/hevs/isi/core/BooleanDataPoint.java @@ -25,7 +25,7 @@ public class BooleanDataPoint extends DataPoint{ public String toString(){ String s; s = this.getLabel(); - s += ": "; + s += "="; s += this.getValue(); return s; } diff --git a/src/main/java/ch/hevs/isi/core/FloatDataPoint.java b/src/main/java/ch/hevs/isi/core/FloatDataPoint.java index 215b874..c168dd4 100644 --- a/src/main/java/ch/hevs/isi/core/FloatDataPoint.java +++ b/src/main/java/ch/hevs/isi/core/FloatDataPoint.java @@ -25,7 +25,7 @@ public class FloatDataPoint extends DataPoint{ public String toString(){ String s; s = this.getLabel(); - s += ": "; + s += "="; s += this.getValue(); return s; } From 596740a4748056b805d836553409914e1a8cd076 Mon Sep 17 00:00:00 2001 From: Nils Ritler <79571155+Slisls@users.noreply.github.com> Date: Wed, 17 May 2023 15:50:46 +0200 Subject: [PATCH 5/8] ModbusAccessor finished and checked --- .../ch/hevs/isi/field/ModbusAccessor.java | 154 ++++++++++++------ 1 file changed, 103 insertions(+), 51 deletions(-) diff --git a/src/main/java/ch/hevs/isi/field/ModbusAccessor.java b/src/main/java/ch/hevs/isi/field/ModbusAccessor.java index 2446c7c..d5612ae 100644 --- a/src/main/java/ch/hevs/isi/field/ModbusAccessor.java +++ b/src/main/java/ch/hevs/isi/field/ModbusAccessor.java @@ -4,6 +4,7 @@ import com.serotonin.modbus4j.ModbusFactory; import com.serotonin.modbus4j.ModbusMaster; import com.serotonin.modbus4j.code.DataType; import com.serotonin.modbus4j.exception.ErrorResponseException; +import com.serotonin.modbus4j.exception.ModbusInitException; import com.serotonin.modbus4j.exception.ModbusTransportException; import com.serotonin.modbus4j.ip.IpParameters; import com.serotonin.modbus4j.ip.tcp.TcpMaster; @@ -11,8 +12,8 @@ import com.serotonin.modbus4j.locator.BaseLocator; public class ModbusAccessor { - private static ModbusAccessor mySelf = null; - private ModbusMaster master = null; + private static ModbusAccessor mySelf = null; //instance of the class + private ModbusMaster master = null; //instance of the modbus master /** * private constructor @@ -28,80 +29,131 @@ public class ModbusAccessor { * @return instance of ModbusAccessor */ public static ModbusAccessor getMySelf(){ - if (mySelf == null){ - mySelf = new ModbusAccessor(); + if (mySelf == null){ //if no instance is created + mySelf = new ModbusAccessor(); //create a new instance of the class } - return mySelf; + return mySelf; //if there is already an instance, return the existing instance } + /** + * this method creates a ModbusFactory and a TCPMaster + * it also initialize the Master + * @param ipAddress IP-address of the server + * @param port port of the server + */ public void connect(String ipAddress, int port){ - ModbusFactory MF = new ModbusFactory(); - IpParameters IpParams = new IpParameters(); + ModbusFactory MF = new ModbusFactory(); //create a modbus factory + IpParameters IpParams = new IpParameters(); //create the ip parameters + IpParams.setHost(ipAddress); //set the server ip-address + IpParams.setPort(port); //set the server port + + //create the TCPMaster with the correct IP parameters and keep it alive this.master = MF.createTcpMaster(IpParams, true); - /** - * TODO finish connect() - */ + try { + this.master.init(); //initialize the master + } + catch(ModbusInitException e) + { + e.printStackTrace(); + } + } + //methods to write + /** + * method to write a boolean value in the correct modbus register + * @param register address of the register + * @param value the desired boolean value + * @throws ModbusTransportException + * @throws ErrorResponseException + */ + public void writeBoolean (int register, boolean value) throws ModbusTransportException, ErrorResponseException{ + //first parameter = slaveID = always 1 because we only have one + //second parameter = register address ==> it is named offset because it starts with 0 and goes to the desired address + this.master.setValue(BaseLocator.coilStatus(1,register), value); //set the desired value in the correct register } /** - * methods to write in the modbus registers + * method to write a float value in the correct modbus register + * @param register address of the register + * @param value the desired float value + * @throws ModbusTransportException + * @throws ErrorResponseException */ - - /** - * method to write a boolean value on the modbus address - * @param address the address of the modbus register - * @param value the value to write in the register - */ - public void writeBoolean (int address, boolean value){ - /** - * TODO writeBoolean() - */ + public void writeFloat (int register, float value) throws ModbusTransportException, ErrorResponseException{ + //first parameter = slaveID = always 1 because we only have one + //second parameter = register address ==> it is named offset because it starts with 0 and goes to the desired address + //third parameter = DataType of the value. The max value is 3 Byte Float, but the class DataType has only 2 or 4 byte + this.master.setValue(BaseLocator.holdingRegister(1,register,DataType.FOUR_BYTE_FLOAT), value); } - - /** - * method to write a float value on the modbus address - * @param address the address of the modbus register - * @param value the value to write in the register - */ - public void writeFloat (int address, float value){ - /** - * TODO writeFloat() - */ - } - - /** - * methods to read from the modbus registers - */ - + //methods to read /** * method to read a boolean value in the correct modbus register * get the coil status of the register. * @param register address of register - * @return boolean value of the desired register + * @return boolean value of the desired register + * @throws ModbusTransportException + * @throws ErrorResponseException */ public boolean readBoolean(int register) throws ModbusTransportException, ErrorResponseException { - /** - * first parameter = slaveID = always 1 because we only have one - * second parameter = register address ==> it is named offset because it starts with 0 and goes to the desired address - */ + //first parameter = slaveID = always 1 because we only have one + //second parameter = register address ==> it is named offset because it starts with 0 and goes to the desired address boolean booleanValue = this.master.getValue(BaseLocator.coilStatus(1,register)); - return booleanValue; + return booleanValue; } - /** * method to read a boolean value in the correct modbus register * get the value from the holding register * @param register address of the register * @return float value of the desired register + * @throws ModbusTransportException + * @throws ErrorResponseException */ - public float readFloat(int register){ - /** - * first parameter = slaveID = always 1 because we only have one - * second parameter = register address ==> it is named offset because it starts with 0 and goes to the desired address - * thrid parameter = DataType of the value. The max value is 3 Byte Float, but the class DataType has only 2 or 4 byte - */ - float floatValue = this.master.getValue(BaseLocator.holdingRegister(1,register, DataType.FOUR_BYTE_FLOAT); + public float readFloat(int register)throws ModbusTransportException, ErrorResponseException{ + //first parameter = slaveID = always 1 because we only have one + //second parameter = register address ==> it is named offset because it starts with 0 and goes to the desired address + //third parameter = DataType of the value. The max value is 3 Byte Float, but the class DataType has only 2 or 4 byte + float floatValue = (float) this.master.getValue(BaseLocator.holdingRegister(1,register, DataType.FOUR_BYTE_FLOAT)); return floatValue; } + /** + * this main method is only for testing the ModbusAccessor class + * @param args + */ + public static void main(String[] args) { + //create an instance of ModbusAccessor and connect it with the server + ModbusAccessor test = ModbusAccessor.getMySelf(); + test.connect("LocalHost", 1502); + //do this all the time + while(true){ + try{ + //get a boolean value => solar SetPoint + boolean solar_connect_st = test.readBoolean(609); //SOLAR_CONNECT_ST + //get a float value => factory SetPoint + float factory_st = test.readFloat(605); //FACTORY_ST + + //write a float value to the Remote_Factory_SP + test.writeFloat(205,0.56F); //REMOTE_FACTORY_SP + //write a boolean value to the Remote_Solar_SW + test.writeBoolean(401,false); //REMOTE_SOLAR_SW + + //check the factory SetPoint + System.out.println("Factory Setpoint is: " + factory_st); + + //check the solar SetPoint + if(solar_connect_st){ + System.out.println("Solar is connected"); + }else{ + System.out.println("Solar is disconnected"); + } + }catch (ModbusTransportException e){ + + }catch (ErrorResponseException e){ + + } + } + } } + + + + From e306d5eacba48cdcd2efdf9edecd1a417795e9ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Heredero?= Date: Wed, 17 May 2023 20:52:03 +0200 Subject: [PATCH 6/8] implement full DatabaseConnector and do some tests --- .../java/ch/hevs/isi/MinecraftController.java | 18 ++ .../ch/hevs/isi/db/DatabaseConnector.java | 160 +++++++++++++++--- src/test/java/Database.java | 27 +-- 3 files changed, 165 insertions(+), 40 deletions(-) diff --git a/src/main/java/ch/hevs/isi/MinecraftController.java b/src/main/java/ch/hevs/isi/MinecraftController.java index ed7bb59..6bc5059 100644 --- a/src/main/java/ch/hevs/isi/MinecraftController.java +++ b/src/main/java/ch/hevs/isi/MinecraftController.java @@ -1,5 +1,8 @@ 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; public class MinecraftController { @@ -74,5 +77,20 @@ public class MinecraftController { // 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 + + } } diff --git a/src/main/java/ch/hevs/isi/db/DatabaseConnector.java b/src/main/java/ch/hevs/isi/db/DatabaseConnector.java index 9cf3507..e91edb6 100644 --- a/src/main/java/ch/hevs/isi/db/DatabaseConnector.java +++ b/src/main/java/ch/hevs/isi/db/DatabaseConnector.java @@ -1,86 +1,190 @@ package ch.hevs.isi.db; +import ch.hevs.isi.MinecraftController; 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.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.MalformedURLException; +import java.net.ProtocolException; import java.net.URL; import java.util.Properties; public class DatabaseConnector implements DataPointListener { - 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 Properties properties = new Properties(); // Properties of the config.properties file + private String fullURL = null; // Full URL of the InfluxDB server + 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 + private TimeManager _timeManager = new TimeManager(3); // Time manager to manage the time of the data points + private long _timestamp = 0; // Timestamp of the data points + 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 constructor of the database connector + * Read the config.properties file and initialize the database connector + */ private DatabaseConnector (){ + // Read the config.properties file 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"); + + // Get the URL, the organization and the bucket from the config.properties file if their are null + 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(){ if (mySelf == null){ mySelf = new DatabaseConnector(); } 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{ - 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) { throw new RuntimeException(e); - } catch (IOException 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) { + 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 { - HttpURLConnection con = (HttpURLConnection) myURL.openConnection(); - con.setRequestMethod("POST"); - con.setRequestProperty("Authorization", "Token " + default_token); - con.setRequestProperty("Content-Type", "application/json"); - con.setDoOutput(true); + // Create connection and set headers + if(con == null){ + con = (HttpURLConnection) urlForWrite.openConnection(); + con.setRequestMethod("POST"); + con.setRequestProperty("Authorization", "Token " + default_token); + con.setRequestProperty("Content-Type", "application/json"); + con.setDoOutput(true); + } + + // Send data OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream()); writer.write(data); writer.flush(); + + // Get response code 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 = null; } catch (IOException e) { e.printStackTrace(); } } + /** + * Push the data point to the database + * @param dp Data point to push + */ @Override public void onNewValue(DataPoint dp) { - pushToDatabase(dp); + if(dp.getLabel().equals("CLOAK_FLOAT")) { + FloatDataPoint fdp = (FloatDataPoint) dp; + _timeManager.setTimestamp(fdp.getValue()); + _timestamp = _timeManager.getNanosForDB(); + } + if(_timestamp != 0){ + pushToDatabase(dp); + } } } diff --git a/src/test/java/Database.java b/src/test/java/Database.java index f490f3c..70815ef 100644 --- a/src/test/java/Database.java +++ b/src/test/java/Database.java @@ -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.FloatDataPoint; import ch.hevs.isi.db.DatabaseConnector; @@ -8,19 +10,20 @@ import java.util.Properties; public class Database { public static void main(String[] args) { - Properties properties = new Properties(); - try (InputStream input = new FileInputStream("src/config.properties")) { - properties.load(input); - } catch (Exception e) { - e.printStackTrace(); - } - String url = properties.getProperty("URL"); - - DatabaseConnector db = DatabaseConnector.getMySelf(); - db.initialize(url); - + MinecraftController.ERASE_PREVIOUS_DATA_INB_DB = true; + FloatDataPoint clock = new FloatDataPoint("CLOAK_FLOAT", false); 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); + } } } From 76279a5d0dcea111e4f8fb20784f32aac34d894d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Heredero?= Date: Wed, 17 May 2023 20:52:53 +0200 Subject: [PATCH 7/8] fix true/false --- src/main/java/ch/hevs/isi/core/BooleanDataPoint.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ch/hevs/isi/core/BooleanDataPoint.java b/src/main/java/ch/hevs/isi/core/BooleanDataPoint.java index a432e70..d18131f 100644 --- a/src/main/java/ch/hevs/isi/core/BooleanDataPoint.java +++ b/src/main/java/ch/hevs/isi/core/BooleanDataPoint.java @@ -26,7 +26,7 @@ public class BooleanDataPoint extends DataPoint{ String s; s = this.getLabel(); s += "="; - s += this.getValue(); + s += this.getValue() ? "1" : "0"; return s; } } From a577e8a4efc50e10da2efe472bba60ad8e760886 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Heredero?= Date: Wed, 17 May 2023 20:53:14 +0200 Subject: [PATCH 8/8] add DEBUG_MODE --- src/main/java/ch/hevs/isi/utils/Utility.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/ch/hevs/isi/utils/Utility.java b/src/main/java/ch/hevs/isi/utils/Utility.java index 9edfd89..62b7cf4 100644 --- a/src/main/java/ch/hevs/isi/utils/Utility.java +++ b/src/main/java/ch/hevs/isi/utils/Utility.java @@ -21,6 +21,7 @@ import java.util.*; public class Utility { /** Default size for the TCP input stream */ public static final int TCP_BUFFER_SIZE = 4096; + public static final boolean DEBUG_MODE = false; /** Object to get some random values... */ public static Random rnd = new Random(1); @@ -419,4 +420,8 @@ public class Utility { System.out.println(logMsg); } } + + public static void pDebug(String msg) { + if(DEBUG_MODE) System.out.println(msg); + } }