diff --git a/src/main/java/com/application/DB/DB.java b/src/main/java/com/application/DB/DB.java index ca1ceb1cd18cc120df764b1ce11ad9d60b481efd..5defc3d33d35d4117839ab11600934aba6352675 100644 --- a/src/main/java/com/application/DB/DB.java +++ b/src/main/java/com/application/DB/DB.java @@ -7,12 +7,9 @@ import com.google.cloud.bigquery.*; import java.io.File; import java.io.FileInputStream; import java.sql.Timestamp; -import java.text.DateFormat; import java.text.SimpleDateFormat; -import java.time.LocalDate; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; -import java.time.temporal.TemporalAccessor; import java.util.*; /** @@ -24,7 +21,12 @@ import java.util.*; public class DB { private static Job queryJob; - private static SimpleDateFormat dateFormat; + + private static SimpleDateFormat getDateFormat(){ + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); + return dateFormat; + } /** * Retrieves the credentials file @@ -46,7 +48,9 @@ public class DB { } public static void main(String[] args) throws Exception { - getKwh(); + //getKwh(); + //getName(); + getZeroPointDate(); } @@ -106,15 +110,11 @@ public class DB { // Initializing the data map to store the results Map<String, Number> data = new HashMap<>(); - // Initializing the date format - DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); - DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - // Preparing a query statement final String sqlStatement = "SELECT TimeStamp, VariantValue " + "FROM sf-drying-optimization.124.int_sd_winccsensordata " + - "WHERE TimeStamp BETWEEN \"2021-01-25 08:51:03\" " + - "AND \"2021-01-28 11:10:09\" ORDER BY TimeStamp ASC"; + "WHERE TimeStamp BETWEEN \"2021-01-30 08:51:03\" " + + "AND \"2021-02-15 11:10:09\" ORDER BY TimeStamp ASC"; // Creates a job configuration queryJob = getJob(QueryJobConfiguration.newBuilder(sqlStatement).build()); @@ -130,35 +130,19 @@ public class DB { // Retrieving the wanted data long timeStamp = row.get("TimeStamp").getTimestampValue()/1000; // Riktig format, men i string - String formatedTimeStamp = dateFormat.format(timeStamp); - // Format the date to LocalDateTime datatype - LocalDateTime date = LocalDateTime.parse(formatedTimeStamp, format); - // Creates a future date to compare against - LocalDateTime dateFuture = date.plusDays(1); - - //System.out.println(date); + String formatedTimeStamp = getDateFormat().format(timeStamp); + // Adding the data to a list in order to sort through later data.put(formatedTimeStamp, variantValue); } -/* - NavigableMap<String, Number> sortedData = new TreeMap<>(data); - - for (Map.Entry<String, Number> entry : sortedData.entrySet()) { - String key = entry.getKey(); - String next = sortedData.higherEntry(entry.getKey()).getKey(); // next - //System.out.println("NĂ¥: "+key); - //System.out.printf("Neste: %s\n\n",next); - } - - */ + //NavigableMap<String, Number> sortedData = new TreeMap<>(data); return data; } /** * Retrieves information about kwh and the corresponding date * - * @return the results * @throws Exception returns potential error */ public static void getName() throws Exception { @@ -167,12 +151,6 @@ public class DB { Map<Object,ArrayList<Object>> data = new HashMap<>(); - // Initializing the date format - DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); - dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); - - - // Preparing a query statement final String sqlStatement = "SELECT Name, CalculatedStart, CalculatedStop FROM `sf-drying-optimization.124.int_dk_valmaticsdryingbatches`" + @@ -193,15 +171,108 @@ public class DB { long calculatedStart = row.get("CalculatedStart").getTimestampValue()/1000; long calculatedStop = row.get("CalculatedStop").getTimestampValue()/1000; // Formatting the dates - String formattedCalculatedStart = dateFormat.format(calculatedStart); - String formattedCalculatedStop = dateFormat.format(calculatedStop); + String formattedCalculatedStart = getDateFormat().format(calculatedStart); + String formattedCalculatedStop = getDateFormat().format(calculatedStop); java.sql.Timestamp timestamp = new Timestamp(calculatedStart); //System.out.println(timestamp); - //System.out.printf("%s\t\t\t%s\t\t\t%s\n",name, formattedCalculatedStart, formattedCalculatedStop); + System.out.printf("%s\t\t\t%s\t\t\t%s\n",name, formattedCalculatedStart, formattedCalculatedStop); } //return data; } + + private static Map<String, String> getZeroPointDate() throws Exception{ + + // Initializing a date format in the data type DateTimeFormatter + // Required for iterating through the dates. + DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + + // Initializing the dates map to store the results + Map<String, String> dates = new HashMap<>(); + + // Preparing a query statement + final String sqlStatement = + "SELECT InTidTork, UtTidTork FROM `sf-drying-optimization.124.int_gs_ds_sipalpackages`" + + "WHERE Tork Like \"%5%\" AND InTidTork BETWEEN \"2021-01-30 08:51:03\" " + + "AND \"2022-03-15 11:10:09\" ORDER BY InTidTork ASC"; + + // Creates a job configuration + queryJob = getJob(QueryJobConfiguration.newBuilder(sqlStatement).build()); + + // Retrieves the results from the queryjob + TableResult result = queryJob.getQueryResults(); + + System.out.println("InTidTork\t\t\tUtTidTork"); + + // Iterating through the results + for (FieldValueList row : result.iterateAll()) { + + // Defining variables + String formatedInTidTork; + String formatedUtTidTork; + + // Retrieving the data + // InTidTork: + if(!row.get("InTidTork").isNull()){ + long InTidTorkLong = row.get("InTidTork").getTimestampValue()/1000; + // Formating the data from long to a string in the correct date format + formatedInTidTork = getDateFormat().format(InTidTorkLong); + } else { + formatedInTidTork = ""; + } + + // UtTidTork: + if(!row.get("UtTidTork").isNull()){ + long utTidTorkLong = row.get("UtTidTork").getTimestampValue()/1000; + // Formating the data from long to a string in the correct date format + formatedUtTidTork = getDateFormat().format(utTidTorkLong); + } else { + formatedUtTidTork = ""; + } + + //System.out.printf("%s\t\t\t%s\n",formatedInTidTork,formatedUtTidTork); + + // Adds the data to the dates map + dates.put(formatedInTidTork,formatedUtTidTork); + } + + // Defining a treemap to sort through the data + NavigableMap<String, String> dataSet = new TreeMap<>(dates); + + // Defining a hashmap to store the final sorted data + Map<String, String> sortedDates = new HashMap<>(); + + // Iterating through the data in order to find and set a zeropoint for the dates. + for (Map.Entry<String, String> entry : dataSet.entrySet()) { + + // Retrieving the entry key and value + String inTid = entry.getKey(); + String utTid = entry.getValue(); + + // Format the date to LocalDateTime datatype + LocalDateTime date = LocalDateTime.parse(inTid, format); + // Creates a future date to compare against one day forward in time + LocalDateTime dateNowPlus = date.plusDays(1); + String formatedDateNowPlus = format.format(dateNowPlus); + + try { + // Retrieves the next entry in the list + String next = dataSet.higherEntry(entry.getKey()).getKey(); + + // 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. + if(next.compareTo(formatedDateNowPlus) > 0){ + // Storing the dates that will be used for resetting kwh's. + sortedDates.put(inTid,utTid); + } + } catch (NullPointerException e){ + // Can't find any more iterations, breaks out of the loop. + break; + } + } + return sortedDates; + } } \ No newline at end of file diff --git a/target/classes/com.application/GUI/graphical_user_interface.fxml b/target/classes/com.application/GUI/graphical_user_interface.fxml index 975d47da34a8cbb9301984afc53582812ac0721b..4d8f5c57661d0a3add54e8c0ee84bb5649706afc 100644 --- a/target/classes/com.application/GUI/graphical_user_interface.fxml +++ b/target/classes/com.application/GUI/graphical_user_interface.fxml @@ -55,14 +55,14 @@ <left> <ImageView fitHeight="50.0" fitWidth="50.0" pickOnBounds="true" preserveRatio="true" translateX="5.0" BorderPane.alignment="CENTER"> <image> - <Image url="@moelven_logo_m.jpg" /> + <Image url="@moelven_logo_m.png" /> </image> </ImageView> </left> <center> <ImageView fitHeight="30.0" fitWidth="125.0" pickOnBounds="true" preserveRatio="true"> <image> - <Image url="@moelven_logo_title.jpg" /> + <Image url="@moelven_logo_title.png" /> </image> </ImageView> </center> diff --git a/target/classes/com/application/DB/DB.class b/target/classes/com/application/DB/DB.class index f55cd733a771a5caab5806fc12243e1ebbed11d6..b8c993545329c3ff8fe3fd7b263cd97cde2e4748 100644 Binary files a/target/classes/com/application/DB/DB.class and b/target/classes/com/application/DB/DB.class differ