public List <ENT.MeterSlaveMaster> GetTopOneRecord(ENT.MeterSlaveMaster objENT) { List <ENT.MeterSlaveMaster> lstENT = new List <ENT.MeterSlaveMaster>(); try { sqlCMD = new SqlCommand(); sqlCMD.CommandText = "GetMeterSlaveMaster"; sqlCMD.Parameters.AddWithValue("@ID", objENT.ID); sqlCMD.Parameters.AddWithValue("@Mode", objENT.Mode); lstENT = DBHelper.GetEntityList <ENT.MeterSlaveMaster>(sqlCMD); } catch (Exception ex) { throw ex; } return(lstENT); }
public bool InsertUpdateDeleteMeterSlaveMaster(ENT.MeterSlaveMaster objENT) { bool row = false; try { sqlCMD = new SqlCommand(); sqlCMD.CommandText = "InsertUpdateDeleteMeterSlaveMaster"; sqlCMD.Parameters.AddWithValue("@ID", objENT.ID); sqlCMD.Parameters.AddWithValue("@Address", objENT.Address); sqlCMD.Parameters.AddWithValue("@DataDate", objENT.DataDate); sqlCMD.Parameters.AddWithValue("@DataValue", objENT.DataValue); sqlCMD.Parameters.AddWithValue("@MeterID", objENT.MeterID); sqlCMD.Parameters.AddWithValue("@Mode", objENT.Mode); row = objCRUD.InsertUpdateDelete(sqlCMD); } catch (Exception ex) { throw ex; } return(row); }
private void ObjTime_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { string strFilename = "01SELEC" + DateTime.Now.ToString("yyMMdd") + ".csv"; try { objTime.Stop(); objTime.Enabled = false; int cntInsert = 0; string copyToFilePath = Path.Combine(Application.StartupPath, strFilename); string[] copyFromFilePath = File.ReadAllLines(Path.Combine(Application.StartupPath, "DownloadFilePath.txt")); tmInerval = Convert.ToInt32(copyFromFilePath[1]); objTime.Interval = ((1000 * 60) * tmInerval); #region Copy File From FTP Folder if (File.Exists(copyToFilePath)) { if (File.Exists(Path.Combine(copyFromFilePath[0], strFilename))) { File.Delete(copyToFilePath); File.Copy(Path.Combine(copyFromFilePath[0], strFilename), copyToFilePath); } else { notifyIcon1.BalloonTipText = "File not generated by machine, Please check machine status."; notifyIcon1.ShowBalloonTip(2000); objTime.Start(); } } else { if (File.Exists(Path.Combine(copyFromFilePath[0], strFilename))) { File.Copy(Path.Combine(copyFromFilePath[0], strFilename), copyToFilePath); } else { notifyIcon1.BalloonTipText = "File not generated by machine, Please check machine status."; notifyIcon1.ShowBalloonTip(2000); objTime.Start(); } } #endregion #region Line Count Of File DataTable dtNew = new DataTable(); if (conn.State == ConnectionState.Closed) { conn.Open(); } string strqry = "SELECT COUNT(*) AS Cnt FROM MeterSlaveMaster WHERE FileName='" + strFilename.Trim() + "'"; SqlCommand cmd = new SqlCommand(); cmd.CommandText = strqry; cmd.CommandType = CommandType.Text; cmd.Connection = conn; SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(dtNew); if (conn.State == ConnectionState.Open) { conn.Close(); } int lines_count = Convert.ToInt32(dtNew.Rows[0][0]); #endregion DataTable dtCSV = DAL.MeterSlaveMaster.GetDataTableFromCSVFile((Path.Combine(Application.StartupPath, strFilename))); for (int m = lines_count; m < dtCSV.Rows.Count; m++) { try { ENT.MeterSlaveMaster objENT = new ENT.MeterSlaveMaster(); objENT.ID = Guid.NewGuid(); string date = dtCSV.Rows[m][0].ToString().Substring(0, 8); string[] arrDate = date.Split('-'); DateTime dtDate = new DateTime(2000 + Convert.ToInt32(arrDate[0]), Convert.ToInt32(arrDate[1]), Convert.ToInt32(arrDate[2]), 0, 0, 0); objENT.DataDate = dtDate; string time = dtCSV.Rows[m][0].ToString().Substring(9, 8); string[] arrTime = time.Split(':'); DateTime dtTime = new DateTime(2000 + Convert.ToInt32(arrDate[0]), Convert.ToInt32(arrDate[1]), Convert.ToInt32(arrDate[2]), Convert.ToInt32(arrTime[0]), Convert.ToInt32(arrTime[1]), Convert.ToInt32(arrTime[2])); objENT.DataTime = dtTime; objENT.MeterID = 0; objENT.SlaveID = 0; objENT.Address = 0; objENT.Quantity = 0; objENT.DataValue = Convert.ToDecimal(Convert.ToString(dtCSV.Rows[m][1])); #region Get Last Unit DataTable dtTop = new DataTable(); if (conn.State == ConnectionState.Closed) { conn.Open(); } string strTopOne = "SELECT TOP 1 ISNULL(DataValue,0) as topone FROM MeterSlaveMaster ORDER BY EntryDate DESC, LineCount DESC"; SqlCommand cmdtop = new SqlCommand(); cmdtop.CommandText = strTopOne; cmdtop.CommandType = CommandType.Text; cmdtop.Connection = conn; SqlDataAdapter daTop = new SqlDataAdapter(cmdtop); daTop.Fill(dtTop); if (conn.State == ConnectionState.Open) { conn.Close(); } if (dtTop.Rows.Count > 0) { objENT.DataValue2 = Convert.ToDecimal(dtTop.Rows[0][0]); } else { objENT.DataValue2 = 0; } #endregion string QueryString = "INSERT INTO MeterSlaveMaster (ID,DataDate,DataTime,MeterID,SlaveID,Address,Quantity,DataValue,DataValue2,FileName,LineCount,EntryDate) values ('" + objENT.ID.ToString() + "','" + objENT.DataDate.ToString("dd/MMM/yyyy") + "','" + objENT.DataTime.ToString("dd/MMM/yyyy hh:mm:ss tt") + "'," + objENT.MeterID + "," + objENT.SlaveID + "," + objENT.Address + "," + objENT.Quantity + "," + objENT.DataValue + "," + objENT.DataValue2 + ",'" + strFilename.Trim() + "'," + m.ToString() + ",getdate())"; if (conn.State == ConnectionState.Closed) { conn.Open(); } SqlCommand cmdInsert = new SqlCommand(); cmdInsert.CommandText = QueryString; cmdInsert.CommandType = CommandType.Text; cmdInsert.Connection = conn; if (cmdInsert.ExecuteNonQuery() > 0) { cntInsert = cntInsert + 1; } if (conn.State == ConnectionState.Open) { conn.Close(); } } 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 >> " + strFilename + " >> " + " >> LINE_NO >> " + cntInsert + " >> " + ex.Message.ToString()); } if (conn.State == ConnectionState.Open) { conn.Close(); } } } objTime.Start(); objTime.Enabled = true; } 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 >> " + strFilename + " >> " + ex.Message.ToString()); } if (conn.State == ConnectionState.Open) { conn.Close(); } notifyIcon1.BalloonTipText = "Error : " + ex.Message.ToString(); notifyIcon1.ShowBalloonTip(2000); objTime.Start(); } }
private void ReadData() { string strFilename = "01SELEC" + DateTime.Now.ToString("yyMMdd") + ".csv"; try { objTime.Stop(); objTime.Enabled = false; int cntInsert = 0; string copyToFilePath = Path.Combine(Application.StartupPath, strFilename); string[] copyFromFilePath = File.ReadAllLines(Path.Combine(Application.StartupPath, "DownloadFilePath.txt")); tmInerval = Convert.ToInt32(copyFromFilePath[1]); objTime.Interval = ((1000 * 60) * tmInerval); #region Copy File From FTP Folder if (File.Exists(copyToFilePath)) { if (File.Exists(Path.Combine(copyFromFilePath[0], strFilename))) { File.Delete(copyToFilePath); File.Copy(Path.Combine(copyFromFilePath[0], strFilename), copyToFilePath); } else { notifyIcon1.BalloonTipText = "File not generated by machine, Please check machine status."; notifyIcon1.ShowBalloonTip(2000); objTime.Start(); } } else { if (File.Exists(Path.Combine(copyFromFilePath[0], strFilename))) { File.Copy(Path.Combine(copyFromFilePath[0], strFilename), copyToFilePath); } else { notifyIcon1.BalloonTipText = "File not generated by machine, Please check machine status."; notifyIcon1.ShowBalloonTip(2000); objTime.Start(); } } #endregion #region Line Count Of File DataTable dtNew = new DataTable(); if (conn.State == ConnectionState.Closed) { conn.Open(); } string strqry = "SELECT COUNT(*) AS Cnt FROM MeterSlaveMaster WHERE FileName='" + strFilename.Trim() + "'"; SqlCommand cmd = new SqlCommand(); cmd.CommandText = strqry; cmd.CommandType = CommandType.Text; cmd.Connection = conn; SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(dtNew); if (conn.State == ConnectionState.Open) { conn.Close(); } int lines_count = Convert.ToInt32(dtNew.Rows[0][0]); #endregion DataTable dtCSV = DAL.MeterSlaveMaster.GetDataTableFromCSVFile((Path.Combine(Application.StartupPath, strFilename))); for (int m = lines_count; m < dtCSV.Rows.Count; m++) { try { ENT.MeterSlaveMaster objENT = new ENT.MeterSlaveMaster(); ENT.HeatStartStopReport objHSSR = new ENT.HeatStartStopReport(); ENT.DryrunHeatReport objHDRH = new ENT.DryrunHeatReport(); objENT.ID = Guid.NewGuid(); string date = dtCSV.Rows[m][0].ToString().Substring(0, 8); string[] arrDate = date.Split('-'); DateTime dtDate = new DateTime(2000 + Convert.ToInt32(arrDate[0]), Convert.ToInt32(arrDate[1]), Convert.ToInt32(arrDate[2]), 0, 0, 0); objENT.DataDate = dtDate; string time = dtCSV.Rows[m][0].ToString().Substring(9, 8); string[] arrTime = time.Split(':'); DateTime dtTime = new DateTime(2000 + Convert.ToInt32(arrDate[0]), Convert.ToInt32(arrDate[1]), Convert.ToInt32(arrDate[2]), Convert.ToInt32(arrTime[0]), Convert.ToInt32(arrTime[1]), Convert.ToInt32(arrTime[2])); objENT.DataTime = dtTime; objENT.MeterID = 0; objENT.SlaveID = 0; objENT.Address = 0; objENT.Quantity = 0; objENT.DataValue = Convert.ToDecimal(Convert.ToString(dtCSV.Rows[m][1])); string strValues = Convert.ToString(dtCSV.Rows[m][2]); strValues = strValues.Replace("[", "").Replace("]", ""); string[] Values = strValues.Split(','); objENT.Value1 = Convert.ToDecimal(Values[0]); objENT.Value2 = Convert.ToDecimal(Values[1]); #region Get Last Unit DataTable dtTop = new DataTable(); if (conn.State == ConnectionState.Closed) { conn.Open(); } string strTopOne = "SELECT TOP 1 ISNULL(DataValue,0) as DataValue,Value2,FinalUnit FROM MeterSlaveMaster ORDER BY EntryDate DESC, LineCount DESC"; SqlCommand cmdtop = new SqlCommand(); cmdtop.CommandText = strTopOne; cmdtop.CommandType = CommandType.Text; cmdtop.Connection = conn; SqlDataAdapter daTop = new SqlDataAdapter(cmdtop); daTop.Fill(dtTop); decimal dcmPrvValue2 = 0; if (conn.State == ConnectionState.Open) { conn.Close(); } if (dtTop.Rows.Count > 0) { objENT.DataValue2 = Convert.ToDecimal(dtTop.Rows[0]["DataValue"]); objENT.FinalUnit = Convert.ToDecimal(dtTop.Rows[0]["FinalUnit"]); dcmPrvValue2 = Convert.ToDecimal(dtTop.Rows[0]["Value2"]); using (System.IO.StreamWriter file = new System.IO.StreamWriter(Application.StartupPath + "\\Log\\LastUnitLog-" + DateTime.Now.ToString("dd-MM-yyyy") + ".txt", true)) { file.WriteLine("Date >> " + DateTime.Now.ToString("dd/MMM/yyyy hh:mm:ss tt") + " >> DataValue Found >> " + objENT.DataValue2 + " >> " + " LINE_NO >> " + m); } } else { objENT.DataValue2 = 0; objENT.FinalUnit = 0; using (System.IO.StreamWriter file = new System.IO.StreamWriter(Application.StartupPath + "\\Log\\LastUnitLog-" + DateTime.Now.ToString("dd-MM-yyyy") + ".txt", true)) { file.WriteLine("Date >> " + DateTime.Now.ToString("dd/MMM/yyyy hh:mm:ss tt") + " >> DataValue Not Found>> " + objENT.DataValue2 + " >> " + " LINE_NO >> " + m); } } decimal dcmDiff = objENT.Value2 - dcmPrvValue2; dcmDiff = dcmDiff / 64; if (dcmDiff < 0) { dcmDiff = dcmDiff + 1024; } objENT.Difference = dcmDiff; objENT.FinalUnit = objENT.FinalUnit + dcmDiff; #endregion string QueryString = "INSERT INTO MeterSlaveMaster (ID,DataDate,DataTime,MeterID,SlaveID,Address,Quantity,DataValue,DataValue2,Difference,FinalUnit,Value1,Value2,FileName,LineCount,EntryDate) values ('" + objENT.ID.ToString() + "','" + objENT.DataDate.ToString("dd/MMM/yyyy") + "','" + objENT.DataTime.ToString("dd/MMM/yyyy hh:mm:ss tt") + "'," + objENT.MeterID + "," + objENT.SlaveID + "," + objENT.Address + "," + objENT.Quantity + "," + objENT.DataValue + "," + objENT.DataValue2 + "," + objENT.Difference + "," + objENT.FinalUnit + "," + objENT.Value1 + "," + objENT.Value2 + ",'" + strFilename.Trim() + "'," + (m + 1).ToString() + ",getdate())"; if (conn.State == ConnectionState.Closed) { conn.Open(); } SqlCommand cmdInsert = new SqlCommand(); cmdInsert.CommandText = QueryString; cmdInsert.CommandType = CommandType.Text; cmdInsert.Connection = conn; if (cmdInsert.ExecuteNonQuery() > 0) { cntInsert = cntInsert + 1; if (m == 0) { // Insert First record for heat report try { objHSSR.LineCountStart = lines_count + 1; objHSSR.LineCountEnd = lines_count + 1; objHSSR.StartDataTime = objENT.DataTime; objHSSR.EndDataTime = objENT.DataTime; objHSSR.DataValue = objENT.DataValue; objHSSR.DataValue2 = objENT.DataValue; objHSSR.FileName = strFilename.Trim(); 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\\LastUnitLog-" + DateTime.Now.ToString("dd-MM-yyyy") + ".txt", true)) { file.WriteLine("Date >> " + DateTime.Now.ToString("dd/MMM/yyyy hh:mm:ss tt") + " >> FILE_NAME >> " + strFilename + " >> LINE_NO >> " + cntInsert + " >> Inserted For Report."); } } } catch (Exception ex) { using (System.IO.StreamWriter file = new System.IO.StreamWriter(Application.StartupPath + "\\Log\\ErrorLog.txt", true)) { file.WriteLine("Date >> " + DateTime.Now.ToString("dd/MMM/yyyy hh:mm:ss tt") + " >> FILE_NAME >> " + strFilename + " >> LINE_NO >> " + cntInsert + " >> HeatReport >> " + ex.Message.ToString()); } } // Insert First record for dryrun heat report try { objHDRH.LineCountStart = lines_count + 1; objHDRH.LineCountEnd = lines_count + 1; objHDRH.StartDataTime = objENT.DataTime; objHDRH.EndDataTime = objENT.DataTime; objHDRH.DataValueLast = objENT.DataValue; objHDRH.DataValueFirst = objENT.DataValue; objHDRH.FileName = strFilename.Trim(); 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\\LastUnitLog-" + DateTime.Now.ToString("dd-MM-yyyy") + ".txt", true)) { file.WriteLine("Date >> " + DateTime.Now.ToString("dd/MMM/yyyy hh:mm:ss tt") + " >> FILE_NAME >> " + strFilename + " >> " + " >> LINE_NO >> " + cntInsert + " >> Inserted For Dryrun Report."); } } } catch (Exception ex) { using (System.IO.StreamWriter file = new System.IO.StreamWriter(Application.StartupPath + "\\Log\\ErrorLog.txt", true)) { file.WriteLine("Date >> " + DateTime.Now.ToString("dd/MMM/yyyy hh:mm:ss tt") + " >> FILE_NAME >> " + strFilename + " >> " + " >> LINE_NO >> " + cntInsert + " >> DryrunReport >> " + ex.Message.ToString()); } } } } if (conn.State == ConnectionState.Open) { conn.Close(); } } catch (Exception ex) { using (System.IO.StreamWriter file = new System.IO.StreamWriter(Application.StartupPath + "\\Log\\ErrorLog.txt", true)) { file.WriteLine("Date >> " + DateTime.Now.ToString("dd/MMM/yyyy hh:mm:ss tt") + " >> FILE_NAME >> " + strFilename + " >> " + " >> LINE_NO >> " + cntInsert + " >> " + ex.Message.ToString()); } if (conn.State == ConnectionState.Open) { conn.Close(); } } } objTime.Start(); objTime.Enabled = true; } catch (Exception ex) { using (System.IO.StreamWriter file = new System.IO.StreamWriter(Application.StartupPath + "\\Log\\ErrorLog.txt", true)) { file.WriteLine("Date >> " + DateTime.Now.ToString("dd/MMM/yyyy hh:mm:ss tt") + " >> FILE_NAME >> " + strFilename + " >> " + ex.Message.ToString()); } if (conn.State == ConnectionState.Open) { conn.Close(); } notifyIcon1.BalloonTipText = "Error : " + ex.Message.ToString(); notifyIcon1.ShowBalloonTip(2000); objTime.Start(); } }