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 { ...@@ -22,7 +22,7 @@ public class DB {
private static Job queryJob; private static Job queryJob;
private static SimpleDateFormat getDateFormat(){ private static SimpleDateFormat getDateFormat() {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
return dateFormat; return dateFormat;
...@@ -100,68 +100,91 @@ public class DB { ...@@ -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 * @return the results
* @throws Exception returns potential error * @throws Exception returns potential error
*/ */
public static Map<String, Number> getKwh() throws Exception { public static Map<Integer, Map> 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
}
*/
// Initializing the data map to store the results // 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 " + final String sqlStatement = "SELECT TimeStamp, VariantValue " +
"FROM sf-drying-optimization.124.int_sd_winccsensordata " + "FROM sf-drying-optimization.124.int_sd_winccsensordata " +
"WHERE TimeStamp BETWEEN \"2021-01-30 08:51:03\" " + "WHERE TimeStamp BETWEEN \"2021-01-30 08:51:03\" " +
"AND \"2021-02-15 11:10:09\" ORDER BY TimeStamp ASC"; "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 // Preparing a query statement
queryJob = getJob(QueryJobConfiguration.newBuilder(sqlStatement).build()); 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 //System.out.println(sqlStatement);
TableResult result = queryJob.getQueryResults();
System.out.println("Timestamp \t KWh");
for (FieldValueList row : result.iterateAll()) {
// Kwh // Creates a job configuration
int variantValue = row.get("VariantValue").getNumericValue().intValue(); queryJob = getJob(QueryJobConfiguration.newBuilder(sqlStatement).build());
// Retrieving the wanted data // Iterating through the results
long timeStamp = row.get("TimeStamp").getTimestampValue()/1000; TableResult result = queryJob.getQueryResults();
// Riktig format, men i string //System.out.println("Timestamp \t kWh");
String formatedTimeStamp = getDateFormat().format(timeStamp);
// Adding the data to a list in order to sort through later int baseline = 0;
data.put(formatedTimeStamp, variantValue); 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()) { // Retrieving the wanted data
System.out.printf("Timestamp: \t%s\t\t\tKWh: \t%s\n",entry.getKey(),entry.getValue()); 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 * @throws Exception returns potential error
*/ */
...@@ -207,7 +230,7 @@ public class DB { ...@@ -207,7 +230,7 @@ public class DB {
* This function retrieves the intidtork and uttidtork dates from the database. * 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 * 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. * 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. * 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 * This could be done with counting the number of entries in
...@@ -297,9 +320,9 @@ public class DB { ...@@ -297,9 +320,9 @@ public class DB {
// Checks if the next date is longer than 1 day, // Checks if the next date is longer than 1 day,
// if so the current date is the last intidtork date and // 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){ 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); sortedDates.put(inTid,utTid);
} }
} catch (NullPointerException e){ } catch (NullPointerException e){
...@@ -307,6 +330,7 @@ public class DB { ...@@ -307,6 +330,7 @@ public class DB {
break; 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 { ...@@ -271,7 +271,7 @@ public class Main extends Application {
lineChart.setTitle("Drying Process"); lineChart.setTitle("Drying Process");
Map<String, Number> kwh = DB.getKwh(); Map<String, Number> kwh = null;// = DB.getKwh();
Map<String, Number> sortedKwh = new TreeMap<>(kwh); Map<String, Number> sortedKwh = new TreeMap<>(kwh);
XYChart.Series series1 = new XYChart.Series(); 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.
Please register or to comment