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

Merge branch 'Eilerts_branch' into 'master'

Eilerts branch

See merge request mesji/bacheloroppgave_2022!31
parents a612e81f 8aa29fb7
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,92 @@ 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");
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);
// Adding the data to a list in order to sort through later
data.put(formatedTimeStamp, variantValue);
// kWh value
int variantValue = row.get("VariantValue").getNumericValue().intValue()-baseline;
// 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 and unresonable large values
if(variantValue > 0 && variantValue < 5000000){
// 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
}
NavigableMap<String, Number> sortedData = new TreeMap<>(data);
if(!sortedData.isEmpty()) {
finalResults.put(index,sortedData);
index += 1;
}
}
// Defining a treemap to sort through the data
NavigableMap<Integer, Map> sortedFinalResults = new TreeMap<>(finalResults);
for (Map.Entry<String, Number> entry : data.entrySet()) {
System.out.printf("Timestamp: \t%s\t\t\tKWh: \t%s\n",entry.getKey(),entry.getValue());
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 data;
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 +231,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
......@@ -236,7 +260,7 @@ public class DB {
// Retrieves the results from the queryjob
TableResult result = queryJob.getQueryResults();
System.out.println("InTidTork\t\t\tUtTidTork");
//System.out.println("InTidTork\t\t\tUtTidTork");
// Iterating through the results
for (FieldValueList row : result.iterateAll()) {
......@@ -297,9 +321,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 +331,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
......@@ -269,16 +269,35 @@ public class Main extends Application {
final LineChart<String, Number> lineChart = new LineChart<>(xAxis, yAxis);
lineChart.setTitle("Drying Process");
lineChart.setTitle("Drying Processes");
Map<String, Number> kwh = DB.getKwh();
Map<String, Number> sortedKwh = new TreeMap<>(kwh);
Map<Integer, Map> kWh = DB.getKwh();
XYChart.Series series1 = new XYChart.Series();
series1.setName("Drying 1");
sortedKwh.forEach((key, value) -> series1.getData().add(new XYChart.Data(key,value)));
lineChart.getData().add(series1);
for (Map.Entry<Integer, Map> entryKwh : kWh.entrySet()) {
Map data = entryKwh.getValue();
XYChart.Series series = new XYChart.Series();
for (Object entryData : data.entrySet()){
//System.out.println("data: \t\t"+entryData);
String entryString = entryData.toString();
String[] arr = entryString.split("=");
String date = arr[0];
int kwhValue = Integer.parseInt(arr[1]);
System.out.printf("Date: \t%s\t\t\tkWh: \t%s\n",date,kwhValue);
// Connect the data to a series
series.getData().add(new XYChart.Data(date,kwhValue));
}
lineChart.getData().add(series);
System.out.println("\n\nNew line\n\n");
}
/*
for (Map.Entry<Integer, Map> entryKwh : kWh.entrySet()) {
System.out.printf("Index: \t%s\t\t\tkWh: \t%s\n",entryKwh.getKey(),entryKwh.getValue());
}
*/
return lineChart;
}
}
......
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