private void AddTodayRow(string date, DataMeasure temperature, DataMeasure humidity, DataMeasure voltage, DataMeasure pressure)
        {
            // Build a data-grid row as summary of today values
            DataGridViewRow todayRow = new DataGridViewRow();
            int             index    = 0;

            todayRow.CreateCells(dgvWeatherAnalysis);
            todayRow.Cells[index++].Value = date;
            todayRow.Cells[index++].Value = temperature.getMin();
            todayRow.Cells[index++].Value = temperature.getMinTime();
            todayRow.Cells[index++].Value = temperature.getAvg();
            todayRow.Cells[index++].Value = temperature.getMax();
            todayRow.Cells[index++].Value = temperature.getMaxTime();

            todayRow.Cells[index++].Value = humidity.getMin();
            todayRow.Cells[index++].Value = humidity.getMinTime();
            todayRow.Cells[index++].Value = humidity.getAvg();
            todayRow.Cells[index++].Value = humidity.getMax();
            todayRow.Cells[index++].Value = humidity.getMaxTime();

            todayRow.Cells[index++].Value = voltage.getMin();
            todayRow.Cells[index++].Value = voltage.getMinTime();
            todayRow.Cells[index++].Value = voltage.getMax();
            todayRow.Cells[index++].Value = voltage.getMaxTime();

            todayRow.Cells[index++].Value = pressure.getMin();
            todayRow.Cells[index++].Value = pressure.getMinTime();
            todayRow.Cells[index++].Value = pressure.getAvg();
            todayRow.Cells[index++].Value = pressure.getMax();
            todayRow.Cells[index++].Value = pressure.getMaxTime();

            // add this row to the grid
            this.dgvWeatherAnalysis.Rows.Add(todayRow);
        }
        private void AnalyzeData(DataTable weatherData, bool filterSet, DateTime dtStart, DateTime dtEnd)
        {
            // Do some data analysis here
            DataRow     oldDr       = null;
            DataMeasure temperature = new DataMeasure(-273);    // Not likely to get this temperature ever
            DataMeasure humidity    = new DataMeasure(0);       // Sometimes the DHT22 gives 0 reading. Simply ignore those
            DataMeasure voltage     = new DataMeasure(6);       // We won't get 6V reading for sure
            DataMeasure pressure    = new DataMeasure(0);
            bool        row_avail   = false;
            float       humidityValue;

            foreach (DataRow dr in weatherData.Rows)
            {
                //DateTime dt = new DateTime((int)dr[4], (int)dr[5], (int)dr[6], (int)dr[7], (int)dr[8], (int)dr[9]);
                // The data and time of this reading as parsed from computer time
                DateTime dt = DateParser.DateAndTimeStringToDateTime((string)dr[0], (string)dr[1]);

                // check if filtering by dates and if so, if the record is in the range of the filter
                if ((filterSet == false) || ((filterSet == true) && (dt >= dtStart) && (dt <= dtEnd)))
                {
                    // loop through all the rows, and if this is not the first one
                    if (oldDr != null)
                    {
                        string s1 = (string)dr[0];
                        string s2 = (string)oldDr[0];
                        if (s1.Equals(s2) == false) // Did we change dates?
                        {
                            AddTodayRow((string)oldDr[0], temperature, humidity, voltage, pressure);

                            // And now reset the accumulators for next day reading
                            temperature.reset();
                            humidity.reset();
                            voltage.reset();
                            pressure.reset();
                            row_avail = false;
                        }
                    }

                    humidityValue = (float)dr[8];
                    if (humidityValue != 0)                                                    // Drop DHT22 reading if the humidity is 0.
                    {
                        temperature.add(((float)dr[5] + (float)dr[6] + (float)dr[7]) / 3, dt); // Average temperature from 3 temperatures sensor
                        humidity.add((float)dr[8], dt);
                    }
                    else
                    {
                        temperature.add(((float)dr[5] + (float)dr[7]) / 2, dt); // Average temperature from 2 temperatures sensor other than DHT22
                    }
                    voltage.add((float)dr[2], dt);
                    long l = (long)dr[9];
                    pressure.add(l, dt);

                    dtRange.add(dt);
                    row_avail = true;
                    oldDr     = dr;
                }
            }
            // Check if last line needs to be added
            if (row_avail)
            {
                AddTodayRow((string)oldDr[0], temperature, humidity, voltage, pressure);
            }

            // Now update the controls of date filter with min and max dates
            bool     firstTime = dtRange.firstTime();
            DateTime dtMin     = dtRange.getMin();
            DateTime dtMax     = dtRange.getMax();

            this.dateTimePickerFrom.MinDate = dtMin;
            this.dateTimePickerFrom.MaxDate = dtMax;
            if (firstTime == false)
            {
                this.dateTimePickerFrom.Value = dtMin;
            }
            this.dateTimePickerTo.MinDate = dtMin;
            this.dateTimePickerTo.MaxDate = dtMax;
            if (firstTime == false)
            {
                this.dateTimePickerTo.Value = dtMax;
            }
        }