diff --git a/src/main/java/com/application/DB/DB.java b/src/main/java/com/application/DB/DB.java index 7e81652b1e392a09bf0160d0daffc9a9d9dbe32c..f65225d81da79ffbc8571a86c3dc41f81271cffa 100644 --- a/src/main/java/com/application/DB/DB.java +++ b/src/main/java/com/application/DB/DB.java @@ -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 diff --git a/src/main/java/com/application/Main.java b/src/main/java/com/application/Main.java index 1413fe7390eb12f506e689565e7767658bbc9e8f..6da5ee776e73e68abe7095e3abb95efb2e0767ef 100644 --- a/src/main/java/com/application/Main.java +++ b/src/main/java/com/application/Main.java @@ -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; } } diff --git a/target/classes/com/application/DB/DB.class b/target/classes/com/application/DB/DB.class index ec31148fa1f578a2ce3c089cb84d90f4678287d5..b043ea6f9c7d141f62c4802fae9187a01698bd52 100644 Binary files a/target/classes/com/application/DB/DB.class and b/target/classes/com/application/DB/DB.class differ diff --git a/target/classes/com/application/Main.class b/target/classes/com/application/Main.class index fd2fd756630461a11db07cc7f98d510688c791c2..b9a0b1c909958ed7e95a60800e7fa3918561a1d6 100644 Binary files a/target/classes/com/application/Main.class and b/target/classes/com/application/Main.class differ