private void GetCurrentUnit()
 {
     try
     {
         this.Invoke((Action) delegate
         {
             txtCurrUnit.Text = "";
             DataTable dtNew  = new DAL.MeterSlaveMaster().GetMeterSlaveMaster();
             if (dtNew.Rows.Count > 0)
             {
                 txtCurrUnit.Text = Convert.ToString(dtNew.Rows[0][0]) + " Kwh";
             }
             else
             {
                 txtCurrUnit.Text = "0 Kwh";
             }
         });
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message.ToString());
     }
 }
        private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
        {
            string fileName = "COMPortLog" + DateTime.Now.ToString("dd-MM-yyyy");

            try
            {
                this.Invoke((Action) delegate
                {
                    SerialPort sp  = (SerialPort)sender;
                    string indata  = sp.ReadLine();
                    string[] words = indata.Split('|');

                    if (words.Length >= 3)
                    {
                        CURR_FUR_NAME    = Convert.ToString(words[0]).Trim();
                        CURR_FUR_NO      = Convert.ToInt32(words[1]);
                        CURR_POUR_STATUS = Convert.ToString(words[2]).Trim() == "POURSTOP" ? Convert.ToInt32(DAL.MyEnum.PourStatus.POUR_STOP) : Convert.ToInt32(DAL.MyEnum.PourStatus.POUR_START);

                        if (CURR_FUR_NAME.Equals("FURNACE2") && CURR_FUR_NO.Equals(1))
                        {
                            SetText(CURR_FUR_NAME + "|" + CURR_FUR_NO + "|" + Convert.ToString(words[2]) + "|True");
                            using (System.IO.StreamWriter file = new System.IO.StreamWriter(Application.StartupPath + "\\Log\\" + fileName + ".txt", true))
                            {
                                file.WriteLine("Date >> " + DateTime.Now.ToString("dd/MMM/yyyy hh:mm:ss tt") + " >> STRING >> " + indata + "");
                            }
                        }

                        objENTFur.Mode       = "GetTopOne";
                        objENTFur.fur_name   = CURR_FUR_NAME.Trim();
                        objENTFur.fur_no     = CURR_FUR_NO;
                        objENTFur.fur_status = CURR_POUR_STATUS;
                        lstENTFur            = objDALFur.GetFurnaceSwitch(objENTFur);

                        if (lstENTFur.Count > 0)
                        {
                            if (lstENTFur[0].fur_status != CURR_POUR_STATUS)
                            {
                                // get last time from unit file.
                                SetText(getDateFromCSVFile().ToString("dd/MMM/yyyy hh:mm:ss tt") + " Date From File.");
                                objENTFur.fur_file_time = getDateFromCSVFile().ToString("dd/MMM/yyyy hh:mm:ss tt");
                                objENTFur.fur_id        = Guid.NewGuid();
                                objENTFur.Mode          = "ADD";

                                if (objDALFur.InsertUpdateDeleteFurnaceSwitch(objENTFur))
                                {
                                    SetText(indata + "|True|InsertSuccess");
                                    using (System.IO.StreamWriter file = new System.IO.StreamWriter(Application.StartupPath + "\\Log\\" + fileName + ".txt", true))
                                    {
                                        file.WriteLine("Date >> " + DateTime.Now.ToString("dd/MMM/yyyy hh:mm:ss tt") + " >> STRING >> " + indata + "|True|InsertSuccess");
                                    }
                                }
                                else
                                {
                                    SetText(indata + "|False|InsertFail");
                                    using (System.IO.StreamWriter file = new System.IO.StreamWriter(Application.StartupPath + "\\Log\\" + fileName + ".txt", true))
                                    {
                                        file.WriteLine("Date >> " + DateTime.Now.ToString("dd/MMM/yyyy hh:mm:ss tt") + " >> STRING >> " + indata + "|False|InsertFail");
                                    }
                                }

                                if (CURR_POUR_STATUS == 1)
                                {
                                    // get top one record by id for updated
                                    List <ENT.HeatStartStopReport> lstHSSR = new List <ENT.HeatStartStopReport>();
                                    lstHSSR = new DAL.HeatStartStopReport().GetHeatStartStopReport(new ENT.HeatStartStopReport {
                                        Mode = "GetByTopOneUpdate"
                                    });
                                    if (lstHSSR.Count > 0)
                                    {
                                        // when status is 0 then update record one time
                                        if (lstHSSR[0].isupdated == 0)
                                        {
                                            try
                                            {
                                                // get last record from MeterSlaveMaster for update into HeatStartStopReport table
                                                List <ENT.MeterSlaveMaster> lstMeter = new List <ENT.MeterSlaveMaster>();
                                                lstMeter = new DAL.MeterSlaveMaster().GetTopOneRecord(new ENT.MeterSlaveMaster {
                                                    Mode = "GetByTopOne"
                                                });
                                                if (lstMeter.Count > 0)
                                                {
                                                    ENT.HeatStartStopReport objHSSR = new ENT.HeatStartStopReport();
                                                    objHSSR                 = new ENT.HeatStartStopReport();
                                                    objHSSR.ID              = lstHSSR[0].ID;
                                                    objHSSR.fur_name        = lstENTFur[0].fur_name;
                                                    objHSSR.fur_no          = lstENTFur[0].fur_no;
                                                    objHSSR.fur_status_stop = lstENTFur[0].fur_status;
                                                    objHSSR.fur_open_time   = Convert.ToDateTime(lstENTFur[0].fur_open_time);
                                                    objHSSR.fur_close_time  = Convert.ToDateTime(lstENTFur[0].fur_close_time);
                                                    objHSSR.LineCountEnd    = lstMeter[0].LineCount;
                                                    objHSSR.EndDataTime     = lstMeter[0].DataTime;
                                                    objHSSR.DataValue       = lstMeter[0].DataValue;
                                                    objHSSR.isupdated       = 1;
                                                    objHSSR.Mode            = "UPDATE";
                                                    if (new DAL.HeatStartStopReport().UpdateHeatStartStopReport(objHSSR))
                                                    {
                                                        using (System.IO.StreamWriter file = new System.IO.StreamWriter(Application.StartupPath + "\\Log\\" + fileName + ".txt", true))
                                                        {
                                                            file.WriteLine("Date >> " + DateTime.Now.ToString("dd/MMM/yyyy hh:mm:ss tt") + " >> FILE_NAME >> " + lstMeter[0].FileName + " >> " + " >> LINE_NO >> " + objHSSR.LineCountEnd + " >>  Updated For Report.");
                                                        }
                                                    }
                                                }
                                            }
                                            catch (Exception ex)
                                            {
                                                using (System.IO.StreamWriter file = new System.IO.StreamWriter(Application.StartupPath + "\\Log\\" + fileName + ".txt", true))
                                                {
                                                    file.WriteLine("Date >> " + DateTime.Now.ToString("dd/MMM/yyyy hh:mm:ss tt") + " >> ERROR >> " + ex.Message + "");
                                                }
                                            }
                                        }
                                    }
                                }
                                else if (CURR_POUR_STATUS == 0)
                                {
                                    // get record from database where isupdated=0 if not found any record then insert one record with isupdated=0
                                    List <ENT.HeatStartStopReport> lstHSSR = new List <ENT.HeatStartStopReport>();
                                    lstHSSR = new DAL.HeatStartStopReport().GetHeatStartStopReport(new ENT.HeatStartStopReport {
                                        Mode = "GetByTopOneInsert"
                                    });
                                    if (lstHSSR.Count == 0)
                                    {
                                        // get last record from MeterSlaveMaster for insert into HeatStartStopReport table
                                        List <ENT.MeterSlaveMaster> lstMeter = new List <ENT.MeterSlaveMaster>();
                                        lstMeter = new DAL.MeterSlaveMaster().GetTopOneRecord(new ENT.MeterSlaveMaster {
                                            Mode = "GetByTopOne"
                                        });
                                        if (lstMeter.Count > 0)
                                        {
                                            ENT.HeatStartStopReport objHSSR = new ENT.HeatStartStopReport();
                                            objHSSR.LineCountStart          = lstMeter[0].LineCount;
                                            objHSSR.LineCountEnd            = lstMeter[0].LineCount;
                                            objHSSR.StartDataTime           = lstMeter[0].DataTime;
                                            objHSSR.EndDataTime             = lstMeter[0].DataTime;
                                            objHSSR.DataValue  = lstMeter[0].DataValue;
                                            objHSSR.DataValue2 = lstMeter[0].DataValue;
                                            objHSSR.FileName   = lstMeter[0].FileName;
                                            objHSSR.EntryDate  = DateTime.Now;
                                            objHSSR.isupdated  = 0;
                                            objHSSR.Mode       = "ADD";
                                            if (new DAL.HeatStartStopReport().InsertHeatStartStopReport(objHSSR))
                                            {
                                                using (System.IO.StreamWriter file = new System.IO.StreamWriter(Application.StartupPath + "\\Log\\" + fileName + ".txt", true))
                                                {
                                                    file.WriteLine("Date >> " + DateTime.Now.ToString("dd/MMM/yyyy hh:mm:ss tt") + " >> FILE_NAME >> " + objHSSR.FileName + " >> " + " LINE_NO >> " + objHSSR.LineCountStart + " >>  Inserted For Report.");
                                                }
                                            }

                                            // Insert First record for dryrun heat report
                                            ENT.DryrunHeatReport objHDRH = new ENT.DryrunHeatReport();
                                            try
                                            {
                                                objHDRH.LineCountStart = lstMeter[0].LineCount;
                                                objHDRH.LineCountEnd   = lstMeter[0].LineCount;
                                                objHDRH.StartDataTime  = lstMeter[0].DataTime;
                                                objHDRH.EndDataTime    = lstMeter[0].DataTime;
                                                objHDRH.DataValueLast  = lstMeter[0].DataValue;
                                                objHDRH.DataValueFirst = lstMeter[0].DataValue;
                                                objHDRH.FileName       = lstMeter[0].FileName;
                                                objHDRH.EntryDate      = DateTime.Now;
                                                objHDRH.IsUpdated      = 0;
                                                objHDRH.Mode           = "ADD";
                                                if (new DAL.DryrunHeatReport().InsertUpdateDeleteDryrunHeatReport(objHDRH))
                                                {
                                                    using (System.IO.StreamWriter file = new System.IO.StreamWriter(Application.StartupPath + "\\Log\\" + fileName + ".txt", true))
                                                    {
                                                        file.WriteLine("Date >> " + DateTime.Now.ToString("dd/MMM/yyyy hh:mm:ss tt") + " >> FILE_NAME >> " + objHDRH.FileName + " >> " + " LINE_NO >> " + objHDRH.LineCountStart + " >>  Inserted For Dryrun Report.");
                                                    }
                                                }
                                            }
                                            catch (Exception ex)
                                            {
                                                using (System.IO.StreamWriter file = new System.IO.StreamWriter(Application.StartupPath + "\\ErrorLog.txt", true))
                                                {
                                                    file.WriteLine("Date >> " + DateTime.Now.ToString("dd/MMM/yyyy hh:mm:ss tt") + " >> FILE_NAME >> " + objHDRH.FileName + " >> " + " >> LINE_NO >> " + objHDRH.LineCountStart + " >> " + ex.Message.ToString());
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            // get last time from unit file.
                            SetText(getDateFromCSVFile().ToString("dd/MMM/yyyy hh:mm:ss tt") + " Date From File.");
                            objENTFur.fur_file_time = getDateFromCSVFile().ToString("dd/MMM/yyyy hh:mm:ss tt");
                            objENTFur.fur_id        = Guid.NewGuid();
                            objENTFur.Mode          = "ADD";
                            if (objDALFur.InsertUpdateDeleteFurnaceSwitch(objENTFur))
                            {
                                SetText(indata + "|True|FirstInsertSuccess");
                            }
                            else
                            {
                                SetText(indata + "|False|FirstInsertFail");
                            }
                        }
                    }
                    else
                    {
                        SetText(indata + "|NA");
                    }
                });
            }
            catch (Exception ex)
            {
                using (System.IO.StreamWriter file = new System.IO.StreamWriter(Application.StartupPath + "\\Log\\" + fileName + ".txt", true))
                {
                    file.WriteLine("Date >> " + DateTime.Now.ToString("dd/MMM/yyyy hh:mm:ss tt") + " >> ERROR >> " + ex.Message + "");
                }
                this.SetText("Error => " + ex.Message.ToString());
            }
        }