Skip to content
Snippets Groups Projects
Commit d8333184 authored by Eilert Tunheim's avatar Eilert Tunheim
Browse files

Added full functionality to gathering kWh! Retireves seperate drying periods.

parent a1f9e2b8
No related branches found
No related tags found
No related merge requests found
......@@ -22,7 +22,7 @@ public class DB {
private static Job queryJob;
private static SimpleDateFormat getDateFormat(){
private static SimpleDateFormat getDateFormat() {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
return dateFormat;
......@@ -100,68 +100,91 @@ public class DB {
/**
* Retrieves information about kwh and the corresponding date
* Retrieves information about kWh and the corresponding date
*
* @return the results
* @throws Exception returns potential error
*/
public static Map<String, Number> getKwh() throws Exception {
/*
for (Map.Entry<String, String> entry : getZeroPointDate().entrySet()) {
//System.out.printf("Intid: \t%s\t\t\tOuttid: \t%s\n",entry.getKey(),entry.getValue());
// Preparing a query statement
final String sqlStatement = "SELECT TimeStamp, VariantValue " +
"FROM sf-drying-optimization.124.int_sd_winccsensordata " +
"WHERE TimeStamp BETWEEN " + entry.getKey() + " AND " + entry.getValue() +
"ORDER BY TimeStamp ASC";// Preparing a query statement
}
*/
public static Map<Integer, Map> getKwh() throws Exception {
// Initializing the data map to store the results
Map<String, Number> data = new HashMap<>();
Map<Integer, Map> finalResults = new HashMap<>();
int index = 0;
/*
final String sqlStatement = "SELECT TimeStamp, VariantValue " +
"FROM sf-drying-optimization.124.int_sd_winccsensordata " +
"WHERE TimeStamp BETWEEN \"2021-01-30 08:51:03\" " +
"AND \"2021-02-15 11:10:09\" ORDER BY TimeStamp ASC";
*/
for (Map.Entry<String, String> entry : getZeroPointDate().entrySet()) {
//System.out.printf("Intid: \t%s\t\t\tOuttid: \t%s\n",entry.getKey(),entry.getValue());
// Initializing the data map to store the results
Map<String, Number> data = new HashMap<>();
// Creates a job configuration
queryJob = getJob(QueryJobConfiguration.newBuilder(sqlStatement).build());
// Preparing a query statement
final String sqlStatement = "SELECT DISTINCT TimeStamp, VariantValue " +
"FROM sf-drying-optimization.124.int_sd_winccsensordata " +
"WHERE TimeStamp BETWEEN " + '"'+ entry.getKey() + '"' +
" AND " + '"' + entry.getValue() + '"' +
" ORDER BY TimeStamp ASC";// Preparing a query statement
// Iterating through the results
TableResult result = queryJob.getQueryResults();
System.out.println("Timestamp \t KWh");
for (FieldValueList row : result.iterateAll()) {
//System.out.println(sqlStatement);
// Kwh
int variantValue = row.get("VariantValue").getNumericValue().intValue();
// Creates a job configuration
queryJob = getJob(QueryJobConfiguration.newBuilder(sqlStatement).build());
// Retrieving the wanted data
long timeStamp = row.get("TimeStamp").getTimestampValue()/1000;
// Riktig format, men i string
String formatedTimeStamp = getDateFormat().format(timeStamp);
// Iterating through the results
TableResult result = queryJob.getQueryResults();
//System.out.println("Timestamp \t kWh");
// Adding the data to a list in order to sort through later
data.put(formatedTimeStamp, variantValue);
}
int baseline = 0;
for (FieldValueList row : result.iterateAll()) {
// Sets the baseline in order to reset the kWh counter
if (baseline == 0){
baseline = row.get("VariantValue").getNumericValue().intValue();
}
//System.out.println("baseline: "+baseline);
// kWh value
int variantValue = row.get("VariantValue").getNumericValue().intValue()-baseline;
for (Map.Entry<String, Number> entry : data.entrySet()) {
System.out.printf("Timestamp: \t%s\t\t\tKWh: \t%s\n",entry.getKey(),entry.getValue());
// Retrieving the wanted data
long timeStamp = row.get("TimeStamp").getTimestampValue() / 1000;
// Riktig format, men i string
String formatedTimeStamp = getDateFormat().format(timeStamp);
// Checks for negative values,
if(variantValue > 0){
// Adding the data to a list in order to sort through later
data.put(formatedTimeStamp, variantValue);
}
//System.out.printf("Timestamp: \t%s\t\t\tkWh: \t%s\t\t\tBaseline: %s\n",formatedTimeStamp,variantValue,baseline);
// Checks if the data is empty
}
if(!data.isEmpty()) {
finalResults.put(index,data);
index += 1;
}
}
// Defining a treemap to sort through the data
NavigableMap<Integer, Map> sortedFinalResults = new TreeMap<>(finalResults);
return data;
/*
for (Map.Entry<Integer, Map> entry : sortedFinalResults.entrySet()) {
System.out.printf("Timestamp: \t%s\t\t\tkWh: \t%s\n",entry.getKey(),entry.getValue());
}
*/
return sortedFinalResults;
}
/**
* Retrieves information about kwh and the corresponding date
* Retrieves information about kWh and the corresponding date
*
* @throws Exception returns potential error
*/
......@@ -207,7 +230,7 @@ public class DB {
* This function retrieves the intidtork and uttidtork dates from the database.
* This variables are not sorted and thus this function iterates through the data in order to
* find the last intidtork date and the corresponding uttidtork data.
* These values will be used to reset the kwh.
* These values will be used to reset the kWh.
*
* A possible extention of this function could be to limit the number of dring periodes.
* This could be done with counting the number of entries in
......@@ -297,9 +320,9 @@ public class DB {
// Checks if the next date is longer than 1 day,
// if so the current date is the last intidtork date and
// is the one we will use in order to reset the kwh.
// is the one we will use in order to reset the kWh.
if(next.compareTo(formatedDateNowPlus) > 0){
// Storing the dates that will be used for resetting kwh.
// Storing the dates that will be used for resetting kWh.
sortedDates.put(inTid,utTid);
}
} catch (NullPointerException e){
......@@ -307,6 +330,7 @@ public class DB {
break;
}
}
return sortedDates;
// Defining a treemap to sort through the data
return new TreeMap<>(sortedDates);
}
}
\ No newline at end of file
......@@ -271,7 +271,7 @@ public class Main extends Application {
lineChart.setTitle("Drying Process");
Map<String, Number> kwh = DB.getKwh();
Map<String, Number> kwh = null;// = DB.getKwh();
Map<String, Number> sortedKwh = new TreeMap<>(kwh);
XYChart.Series series1 = new XYChart.Series();
......
No preview for this file type
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment