private void TaskDataIntegrityProcess(ICancelableForm fw) // Task when process button is pressed { Thread.Sleep(100); try { ProcessDataIntegrity(fw); } catch (Exception e) { FormCustomConsole.Write("Exception at process data integrity \r\n" + e.ToString() + "\r\n\r\n"); } }
// Update selected rows in slow way while keeping results on other rows private void UpdateValuesFromDB(ICancelableForm fw) { string oracleDateStart; string oracleDateStartOffset; string oracleDateEnd; string oracleDateEndOffset; string dateStringTimestamp = ""; string querryStart; string querryEnd; string resStart = ""; string resEnd = ""; bool startValid = false; bool endValid = false; // If row is enabled, process it foreach (DataGridViewRow item in dataGridView1.Rows) { if (Convert.ToBoolean(item.Cells[0].Value) == true) { // Selected element is enabled // First intialize time TimeFormatterClass tf = new TimeFormatterClass(); do { // Form querry string for time try { dateStringTimestamp = GetStartYear() + "/" + GetStartMonth() + "/" + tf.GetDay() + " " + tf.GetHour() + ":" + tf.GetMin() + "-" + GetEndYear() + "/" + GetEndMonth() + "/" + tf.GetDay() + " " + tf.GetHour() + ":" + tf.GetMin(); oracleDateStart = HelperClass.ConvertToOracleTime(GetStartYear(), GetStartMonth(), tf.GetDay(), tf.GetHour(), tf.GetMin()); oracleDateStartOffset = HelperClass.ConvertToOracleTime(GetStartYear(), GetStartMonth(), tf.GetDay(), tf.GetHour(), (Convert.ToInt32(tf.GetMin()) + 1).ToString()); oracleDateEnd = HelperClass.ConvertToOracleTime(GetEndYear(), GetEndMonth(), tf.GetDay(), tf.GetHour(), tf.GetMin()); oracleDateEndOffset = HelperClass.ConvertToOracleTime(GetEndYear(), GetEndMonth(), tf.GetDay(), tf.GetHour(), (Convert.ToInt32(tf.GetMin()) + 1).ToString()); } catch (Exception) { break; // This means all time periods are tried, abort this } querryStart = "SELECT EPTC,EQTC FROM DATA_HISTORY WHERE DEVICE_ID=" + item.Cells[1].Value + " AND READ_TIME >= " + oracleDateStart + " AND READ_TIME <= " + oracleDateStartOffset + "AND ROWNUM <= 1"; querryEnd = "SELECT EPTC,EQTC FROM DATA_HISTORY WHERE DEVICE_ID=" + item.Cells[1].Value + " AND READ_TIME >= " + oracleDateEnd + " AND READ_TIME <= " + oracleDateEndOffset + "AND ROWNUM <= 1"; char delim = '|'; try { resStart = DatabaseControllerClass.FetchValuesFromDatabase(DatabaseSettingsClass.ReadConnectionString(), querryStart, delim); resEnd = DatabaseControllerClass.FetchValuesFromDatabase(DatabaseSettingsClass.ReadConnectionString(), querryEnd, delim); } catch (Exception) { Task.Run(() => MessageBox.Show("Timeout for device ID:" + item.Cells[1].Value)); return; } // Now we should have a results, but we must check for validity startValid = HelperClass.CheckDataValidity(resStart); endValid = HelperClass.CheckDataValidity(resEnd); if (!(startValid && endValid)) // If one of each is not valid calculate next time { tf.CalculateNextValues(); if (Convert.ToInt32(tf.GetDay()) == ConfigClass.other.daysToCheck) // If we reached maximum days { break; } } } while (!(startValid && endValid)); // Will run until both values are valid or unitl end is reached // Update percentage bar and check if aborted if (fw.AlreadyClosed) { return; } fw.ProgresStep(); // Update progress bar // Result have active and reactive component if (startValid && endValid == true) { // We have both values, lets process them bool warning; // will warn if negative active power string valueRes = FormatEnergyValues(resStart, resEnd, out warning); string[] temp = valueRes.Split('/'); item.Cells[4].Value = temp[0].Trim(); item.Cells[5].Value = temp[1].Trim(); string[] tempTime = dateStringTimestamp.Split('-'); item.Cells[6].Value = tempTime[0].Trim(); item.Cells[7].Value = tempTime[1].Trim(); if (warning == true) { item.Cells[4].Style.BackColor = Color.Yellow; item.Cells[5].Style.BackColor = Color.Yellow; item.Cells[6].Style.BackColor = Color.Yellow; item.Cells[7].Style.BackColor = Color.Yellow; } else { item.Cells[4].Style.BackColor = Color.White; item.Cells[5].Style.BackColor = Color.White; item.Cells[6].Style.BackColor = Color.White; item.Cells[7].Style.BackColor = Color.White; } } else { item.Cells[4].Value = VALUE_MISSING; item.Cells[4].Style.BackColor = Color.Red; item.Cells[5].Value = VALUE_MISSING; item.Cells[5].Style.BackColor = Color.Red; item.Cells[6].Value = VALUE_MISSING; item.Cells[6].Style.BackColor = Color.Red; item.Cells[7].Value = VALUE_MISSING; item.Cells[7].Style.BackColor = Color.Red; } } else // Cell is not selected, erase value { // Nothing i just want to update } } }
private void TaskUpdate(ICancelableForm fw) // Task when update button is pressed { UpdateValuesFromDB(fw); }
private void TaskSlow(ICancelableForm fw) // Task to fetch slow from database { GetValuesFromDB(fw); }
private void TaskFast(ICancelableForm fw) // Task to fetch fast from database { GetValuesFromDBFast(fw); }
private void ProcessDataIntegrity(ICancelableForm fw) { string sysFormat = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern; string sysUIFormat = CultureInfo.CurrentUICulture.DateTimeFormat.ShortDatePattern; FormCustomConsole.WriteLine("CurrentCulture: " + sysFormat); FormCustomConsole.WriteLine("CurrentUICulture: " + sysUIFormat); DateTime dateStart = GetDateFiltered(dateTimePickerStartDataIntegrity); DateTime dateEnd = GetDateFiltered(dateTimePickerEndDataIntegrity); // Check if day selection is valid TimeSpan dayDiff = dateEnd.Subtract(dateStart); // It will hold days difference if (dayDiff.Days <= 0) { MessageBox.Show("Select valid time!"); return; } string oracleDateStart; string oracleDateEnd; string querryCommand; string result = ""; int currentRow = -1; // If row is enabled, process it foreach (DataGridViewRow item in dataGridView2.Rows) { currentRow++; // Start from -1 so it is 0 at first iteration if (Convert.ToBoolean(item.Cells[0].Value) == true) { // Selected element is enabled // Create oracle time selection string oracleDateStart = HelperClass.ConvertToOracleTime(dateStart); oracleDateEnd = HelperClass.ConvertToOracleTime(dateEnd); // Create command querryCommand = "SELECT VALID,IPAA,IPBA,IPCA,VABA,VBCA,VCAA, READ_TIME, ID FROM DATA_HISTORY WHERE DEVICE_ID=" + item.Cells[1].Value + " AND READ_TIME >= " + oracleDateStart + " AND READ_TIME < " + oracleDateEnd + "AND ROWNUM <= " + ConfigClass.MAX_ROWS_GET.ToString(); char delim = '|'; try { result = DatabaseControllerClass.FetchValuesFromDatabase(DatabaseSettingsClass.ReadConnectionString(), querryCommand, delim); } catch (Exception) { Task.Run(() => MessageBox.Show("Timeout for device ID:" + item.Cells[1].Value)); return; } // Instantiate dataclass for current row allData[currentRow] = new DataClass(dayDiff.Days, currentRow, dateStart, dateEnd); // Fill it with values from database allData[currentRow].FillDataRow(result, Convert.ToInt32(item.Cells[1].Value), delim); // Process data row for: null values, zero values, duplicate values and generaly some way of corrupted data allData[currentRow].ProcessData(); allData[currentRow].RunAnalysis(); // This will calculate all needed info string tempSummary = allData[currentRow].GenerateSummary(); string[] summary = tempSummary.Split(delim); // Fill cells item.Cells[4].Value = summary[0]; // valid item.Cells[5].Value = summary[1]; // flag status item.Cells[6].Value = dateStart.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture); item.Cells[7].Value = dateEnd.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture); item.Cells[8].Value = "Ready"; item.Cells[8].Style.BackColor = Color.Lavender; // Update percentage bar and check if aborted fw.ProgresStep(); // Update progress bar if (fw.AlreadyClosed) { return; } } else // Cell is not selected { } } }
public HelperInterruptibleWorker(ICancelableAsync _client, ICancelableForm _f) { client = _client; f = _f; }