diff --git a/src/main/java/com/application/DB/Constants.java b/src/main/java/com/application/DB/Constants.java index ce33886d1f45d73ca38eccdd47830ef94d0476d1..2ea1fe0321ec64eb36a55687839a0ac3264a7c4b 100644 --- a/src/main/java/com/application/DB/Constants.java +++ b/src/main/java/com/application/DB/Constants.java @@ -18,10 +18,9 @@ public final class Constants { // Today's date public static final String TODAYS_DATE = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now()); - // Start- & end time public static String START_TIME = ""; - public static String STOP_TIME = ""; + public static String STOP_TIME = TODAYS_DATE; // User inputs public static String TREE_SPECIES = ""; @@ -35,11 +34,15 @@ public final class Constants { // Non linear regression public static final double ADJUST_REGRESSION = 5.5; - // Database ID/name + // Current sawmill settings; public static final String PROJECT_ID = "sf-drying-optimization"; - public static final int LOCATION_ID = 124; public static final String MAN_MOISTURE_TABLE = "int_dk_manMoisture"; + public static final String KWH_TABLE_NAME = "int_sd_winccsensordata"; + public static final String KWH_NAME_PARAMETER = "VariantValue"; + public static final String KWH_TIMESTAMP_NAME_PARAMETER = "Timestamp"; + public static final String KWH_VALUE_ID_NAME_PARAMETER = "ValueID"; + public static final String KWH_VALUE_ID_VALUE_PARAMETER = "51"; // Which chamber is used // Location Valasen(124) diff --git a/src/main/java/com/application/DB/DB.java b/src/main/java/com/application/DB/DB.java index 17cfb2c9f5540cc729be813816663278ac32105f..cb88cf9ba15f2c16718e680bd54aa962e718272d 100644 --- a/src/main/java/com/application/DB/DB.java +++ b/src/main/java/com/application/DB/DB.java @@ -267,31 +267,7 @@ public class DB { //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("" + KwhName + "").getNumericValue().intValue(); - } - //System.out.println("baseline: "+baseline); - - // kWh value - int variantValue = row.get("" + KwhName + "").getNumericValue().intValue() - baseline; //-baseline - - // Retrieving the wanted data - long timeStamp = row.get("" + timestamp + "").getTimestampValue() / 1000; - // Riktig format, men i string - String formatedTimeStamp = HelpingFunctions.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 - } + HelpingFunctions.iterateKwhValues(data, baseline, result, KwhName, timestamp); System.out.println("Data size: " + data.size()); @@ -597,4 +573,39 @@ public class DB { HelpingFunctions.createQueryJob(sqlStatement); } + + /** + * Retrieves data from current drying process + * + * @return a map consisting of Timestamp(date&time) and Kwh value + * @throws Exception throws execution if anything is wrong + */ + public static Map<String, Number> getCurrentDrying() throws Exception { + + // Initializing the data map to store the results + Map<String, Number> data = new HashMap<>(); + // Initializing baseline + int baseline = 0; + + // Sqlstatement + final String sqlStatement = "SELECT `" + KWH_TIMESTAMP_NAME_PARAMETER + "`, `" + KWH_NAME_PARAMETER + "` " + + "FROM `" + PROJECT_ID + "." + LOCATION_ID + "." + KWH_TABLE_NAME + "` " + + "WHERE " + KWH_TIMESTAMP_NAME_PARAMETER + " BETWEEN " + '"' + START_TIME + '"' + + " AND " + '"' + STOP_TIME + '"' + + " AND " + KWH_VALUE_ID_NAME_PARAMETER + " = " + KWH_VALUE_ID_VALUE_PARAMETER + " " + + " AND " + KWH_NAME_PARAMETER + " <> 0 " + + " ORDER BY " + KWH_TIMESTAMP_NAME_PARAMETER + " ASC"; + + System.out.println(sqlStatement); + + // Retrieves the results from the queryjob + TableResult result = HelpingFunctions.createQueryJob(sqlStatement); + + //System.out.println("InTidTork\t\t\tUtTidTork"); + // Iterating through the results + HelpingFunctions.iterateKwhValues(data, baseline, result, KWH_NAME_PARAMETER, KWH_TIMESTAMP_NAME_PARAMETER); + return new TreeMap<>(data); + } + + } diff --git a/src/main/java/com/application/DB/HelpingFunctions.java b/src/main/java/com/application/DB/HelpingFunctions.java index 5467ca7743cfd3294e3a80c72138495faffd3658..a34f4f536e07e53e75938bc46c990806a5fa6995 100644 --- a/src/main/java/com/application/DB/HelpingFunctions.java +++ b/src/main/java/com/application/DB/HelpingFunctions.java @@ -117,4 +117,36 @@ public class HelpingFunctions { public static Map<String, String> getManMoist() { return manMoist; } + + /** + * Function to iterate through all the Kwh values and storing them in a map + * + * @param data a map to store all the data + * @param baseline a baseline to base all the next values of to get a zero point + * @param result TableResult to iterate through + * @param kwhNameParameter Name of the Kwh name parameter in the database + * @param kwhTimestampNameParameter Name of the timestamp parameter in the database + */ + static void iterateKwhValues(Map<String, Number> data, int baseline, TableResult result, String kwhNameParameter, String kwhTimestampNameParameter) { + for (FieldValueList row : result.iterateAll()) { + // Sets the baseline in order to reset the kWh counter + if (baseline == 0) { + baseline = row.get("" + kwhNameParameter + "").getNumericValue().intValue(); + } + + // kWh value + int variantValue = row.get("" + kwhNameParameter + "").getNumericValue().intValue() - baseline; //-baseline + + // Retrieving the wanted data + long timeStamp = row.get("" + kwhTimestampNameParameter + "").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); + } + } + } } diff --git a/src/main/java/com/application/GUI/InputPopUpWindow.java b/src/main/java/com/application/GUI/InputPopUpWindow.java index 1d26d6379136d481fed66d3d7b87e2d873605c34..a4071edd86a9da32bcfc9e8ad88d8b3b461a133e 100644 --- a/src/main/java/com/application/GUI/InputPopUpWindow.java +++ b/src/main/java/com/application/GUI/InputPopUpWindow.java @@ -13,6 +13,7 @@ import java.util.Map; import java.util.concurrent.atomic.AtomicBoolean; import static com.application.DB.Constants.*; +import static com.application.DB.DB.getCurrentDrying; import static com.application.Main.*; import static com.application.DB.DB.setInputParameters; import static com.application.GUI.LineChartFunctionality.loadSingleSeries; @@ -247,7 +248,31 @@ public class InputPopUpWindow { ex.printStackTrace(); } + // Retrieve data for current drying period + try{ + Thread thread = new Thread(() -> { + try { + + // Henter her data fra databasen + Map<String, Number> data = getCurrentDrying(); + Platform.runLater(() -> { + try { + //loadSingleSeries(data); + } catch (Exception ex) { + ex.printStackTrace(); + } + }); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + ); + thread.setDaemon(true); + thread.start(); + } catch (Exception ex) { + ex.printStackTrace(); + } } diff --git a/target/classes/com/application/DB/Constants.class b/target/classes/com/application/DB/Constants.class index 63ce6b6ba5f67a9529377d76c676478db897eb76..86a1941d22a795754d864cc38476bcc881177aec 100644 Binary files a/target/classes/com/application/DB/Constants.class and b/target/classes/com/application/DB/Constants.class differ diff --git a/target/classes/com/application/GUI/InputPopUpWindow.class b/target/classes/com/application/GUI/InputPopUpWindow.class index 6f8be194dd66499c7a4cda284470d56527148834..04830397692eb383cf896d85766005d3041e3420 100644 Binary files a/target/classes/com/application/GUI/InputPopUpWindow.class and b/target/classes/com/application/GUI/InputPopUpWindow.class differ