Beispiel #1
0
        // 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
                }
            }
        }