From 439b46ff96a5c901354d164b33c85bb7720b4c82 Mon Sep 17 00:00:00 2001 From: cesar liu Date: Wed, 3 Aug 2022 10:06:02 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B0=B4=E4=BD=8D=E7=AE=A1=E7=90=86=20v0.9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- solarApp/Service/archiveLowData.cs | 150 ++++++++++++++++++++++------- solarApp/Service/getStationSvc.cs | 3 +- solarApp/Service/procArchiveLog.cs | 15 ++- solarApp/fmArchive.Designer.cs | 36 ++++--- solarApp/fmArchive.cs | 21 +++- 5 files changed, 167 insertions(+), 58 deletions(-) diff --git a/solarApp/Service/archiveLowData.cs b/solarApp/Service/archiveLowData.cs index 6645212..9e1727e 100644 --- a/solarApp/Service/archiveLowData.cs +++ b/solarApp/Service/archiveLowData.cs @@ -39,7 +39,10 @@ namespace solarApp.Service //_date1 = date1; get_siteInfo(); checkTable(); - moveData(); + + moveData_station(); + moveData_inv(); + moveData_sensor(); result = true; } catch (Exception ex) @@ -277,63 +280,138 @@ namespace solarApp.Service return result; } - bool moveData() + bool moveData_station() { bool result = false; + procArchiveLog arclog = new procArchiveLog(); using (MySqlConnection conn = new MySqlConnection(Connection1)) { conn.Open(); + DateTime dt_start1 = DateTime.Now; + var dateTime = DateTime.Now.ToString("yyyy-MM-dd HH"); try { - var dateTime = DateTime.Now.ToString("yyyy-MM-dd HH"); - #region station搬移 - System.Diagnostics.Debug.WriteLine($"【ArchiveRowData】開始執行[{_siteDB}.s{_siteID}_station]的資料表向[{_siteDB}_history.s{_siteID}_station]搬移"); - string for_insert_station = $@"INSERT {_siteDB}_history.s{_siteID01}_station - SELECT * FROM {_siteDB}.s{_siteID01}_station + //System.Diagnostics.Debug.WriteLine($"【ArchiveRowData】開始執行[{_siteDB}.s{_siteID}_station]的資料表向[{_siteDB}_history.s{_siteID}_station]搬移"); + string for_insert_station = $@"INSERT {_siteDB}_history.s{_siteID01}_station(`ID`, `TIMESTAMP`, `SITEID`, `SITETYPE`, `CONTROLLERID`, `KWH`, `TODAYKWH`, `TOTALKWH`, `KWHKWP`, `PR`, `MP`, `SOLARHOUR`, `insertTime`) + SELECT `ID`, `TIMESTAMP`, `SITEID`, `SITETYPE`, `CONTROLLERID`, `KWH`, `TODAYKWH`, `TOTALKWH`, `KWHKWP`, `PR`, `MP`, `SOLARHOUR`, `insertTime` FROM {_siteDB}.s{_siteID01}_station WHERE FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d') < '2022-07'"; - int insert_station = conn.Execute(for_insert_station, commandTimeout : 600); - System.Diagnostics.Debug.WriteLine($"【ArchiveRowData】{_siteDB}.s{_siteID}_station的七月前資料寫入至{_siteDB}_history.s{_siteID}_station【寫入成功】於{dateTime}"); + int rowCT = conn.Execute(for_insert_station, commandTimeout : 600); + // System.Diagnostics.Debug.WriteLine($"【ArchiveRowData】{_siteDB}.s{_siteID}_station的七月前資料寫入至{_siteDB}_history.s{_siteID}_station【寫入成功】於{dateTime}"); + + // log + TimeSpan duration = DateTime.Now - dt_start1; + arclog.insert_log(_powerStationID, "move raw s1", duration.TotalSeconds, $"{_siteDB}.s{_siteID}_station", $"{_siteDB}_history.s{_siteID}_station", "0", "", rowCT.ToString(), conn, null); + dt_start1 = DateTime.Now; //刪除原本的資料 string for_delete_station = $@"DELETE FROM {_siteDB}.s{_siteID01}_station WHERE FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d') < '2022-07'"; - int delete_station = conn.Execute(for_delete_station, commandTimeout: 600); - System.Diagnostics.Debug.WriteLine($"【ArchiveRowData】{_siteDB}.s{_siteID}_sensoravg搬移至{_siteDB}_history.s{_siteID}_sensoravg【搬移資料成功】於{dateTime} 共 {delete_station.ToString()} 筆"); + rowCT = conn.Execute(for_delete_station, commandTimeout: 600); + //System.Diagnostics.Debug.WriteLine($"【ArchiveRowData】{_siteDB}.s{_siteID}_sensoravg搬移至{_siteDB}_history.s{_siteID}_sensoravg【搬移資料成功】於{dateTime} 共 {delete_station.ToString()} 筆"); + duration = DateTime.Now - dt_start1; + arclog.insert_log(_powerStationID, "delete raw s2", duration.TotalSeconds, $"{_siteDB}.s{_siteID}_station", "", "0", "", rowCT.ToString(), conn, null); + #endregion - #region inv搬移 - System.Diagnostics.Debug.WriteLine($"【ArchiveRowData】開始執行[{_siteDB}.s{_siteID}_inv]的資料表向[{_siteDB}_history.s{_siteID}_inv]搬移"); - string for_insert_inv = $@"INSERT {_siteDB}_history.s{_siteID01}_inv - SELECT * FROM {_siteDB}.s{_siteID01}_inv - WHERE LEFT(crdTime, 7) < '2022-07'"; - int insert_inv = conn.Execute(for_insert_inv, commandTimeout : 600); - System.Diagnostics.Debug.WriteLine($"【ArchiveRowData】{_siteDB}.s{_siteID}_inv的七月前資料寫入至{_siteDB}_history.s{_siteID}_inv【寫入成功】於{dateTime}"); - string for_delete_inv = $@"DELETE FROM {_siteDB}.s{_siteID01}_inv - WHERE LEFT(crdTime, 7) < '2022-07'"; - int delete_inv = conn.Execute(for_delete_inv, commandTimeout: 6000); - System.Diagnostics.Debug.WriteLine($"【ArchiveRowData】{_siteDB}.s{_siteID}_sensoravg搬移至{_siteDB}_history.s{_siteID}_sensoravg【搬移資料成功】於{dateTime} 共 {delete_inv.ToString()} 筆"); - #endregion - #region sensoravg搬移 - System.Diagnostics.Debug.WriteLine($"【ArchiveRowData】開始執行[{_siteDB}.s{_siteID}_sensoravg]的資料表向[{_siteDB}_history.s{_siteID}_sensoravg]搬移"); - string for_insert_sensoravg = $@"INSERT {_siteDB}_history.s{_siteID01}_sensoravg - SELECT * FROM {_siteDB}.s{_siteID01}_sensoravg - WHERE LEFT(crdTime, 7) < '2022-07'"; - int insert_sensoravg = conn.Execute(for_insert_sensoravg, commandTimeout : 600); - System.Diagnostics.Debug.WriteLine($"【ArchiveRowData】{_siteDB}.s{_siteID}_sensoravg的七月前資料寫入至{_siteDB}_history.s{_siteID}_sensoravg【寫入成功】於{dateTime}"); - string for_delete_sensoravg = $@"DELETE FROM {_siteDB}.s{_siteID01}_sensoravg - WHERE LEFT(crdTime, 7) < '2022-07'"; - int delete_sensoravg = conn.Execute(for_delete_sensoravg, commandTimeout: 600); - System.Diagnostics.Debug.WriteLine($"【ArchiveRowData】{_siteDB}.s{_siteID}_sensoravg搬移至{_siteDB}_history.s{_siteID}_sensoravg【搬移資料成功】於{dateTime} 共 {delete_sensoravg.ToString()} 筆"); - #endregion result = true; } catch (Exception ex) { - System.Diagnostics.Debug.WriteLine("【ArchiveRowData】[搬移資料失敗】"); + //System.Diagnostics.Debug.WriteLine("【ArchiveRowData】[搬移資料失敗】"); + TimeSpan duration = DateTime.Now - dt_start1; + arclog.insert_log(_powerStationID, "move raw s1", duration.TotalSeconds, $"{_siteDB}.s{_siteID}_station", $"{_siteDB}_history.s{_siteID}_station", "1", ex.Message, "0", conn, null); + throw ex; + } + + conn.Close(); + } + return result; + } + + bool moveData_inv() + { + bool result = false; + procArchiveLog arclog = new procArchiveLog(); + using (MySqlConnection conn = new MySqlConnection(Connection1)) + { + conn.Open(); + DateTime dt_start1 = DateTime.Now; + var dateTime = DateTime.Now.ToString("yyyy-MM-dd HH"); + try + { + #region inv搬移 + dt_start1 = DateTime.Now; + // System.Diagnostics.Debug.WriteLine($"【ArchiveRowData】開始執行[{_siteDB}.s{_siteID}_inv]的資料表向[{_siteDB}_history.s{_siteID}_inv]搬移"); + string for_insert_inv = $@"INSERT {_siteDB}_history.s{_siteID01}_inv + SELECT * FROM {_siteDB}.s{_siteID01}_inv + WHERE LEFT(crdTime, 7) < '2022-07' "; + int rowCT = conn.Execute(for_insert_inv, commandTimeout: 6000); + //System.Diagnostics.Debug.WriteLine($"【ArchiveRowData】{_siteDB}.s{_siteID}_inv的七月前資料寫入至{_siteDB}_history.s{_siteID}_inv【寫入成功】於{dateTime}"); + // log + TimeSpan duration = DateTime.Now - dt_start1; + arclog.insert_log(_powerStationID, "move raw s1", duration.TotalSeconds, $"{_siteDB}.s{_siteID01}_inv", $"{_siteDB}_history.s{_siteID01}_inv", "0", "", rowCT.ToString(), conn, null); + dt_start1 = DateTime.Now; + + string for_delete_inv = $@"DELETE FROM {_siteDB}.s{_siteID01}_inv + WHERE LEFT(crdTime, 7) < '2022-07'"; + rowCT = conn.Execute(for_delete_inv, commandTimeout: 6000); + //System.Diagnostics.Debug.WriteLine($"【ArchiveRowData】{_siteDB}.s{_siteID}_sensoravg搬移至{_siteDB}_history.s{_siteID}_sensoravg【搬移資料成功】於{dateTime} 共 {delete_inv.ToString()} 筆"); + duration = DateTime.Now - dt_start1; + arclog.insert_log(_powerStationID, "delete raw s2", duration.TotalSeconds, $"{_siteDB}.s{_siteID01}_inv", "", "0", "", rowCT.ToString(), conn, null); + } + catch (Exception ex) + { + TimeSpan duration = DateTime.Now - dt_start1; + arclog.insert_log(_powerStationID, "move raw s1", duration.TotalSeconds, $"{_siteDB}.s{_siteID01}_station", $"{_siteDB}_history.s{_siteID01}_station", "1", ex.Message, "0", conn, null); throw ex; } + #endregion + conn.Close(); + } + return result; + } + + + bool moveData_sensor() { + bool result = false; + procArchiveLog arclog = new procArchiveLog(); + using (MySqlConnection conn = new MySqlConnection(Connection1)) + { + conn.Open(); + DateTime dt_start1 = DateTime.Now; + var dateTime = DateTime.Now.ToString("yyyy-MM-dd HH"); + try + { + #region sensoravg搬移 + dt_start1 = DateTime.Now; + //System.Diagnostics.Debug.WriteLine($"【ArchiveRowData】開始執行[{_siteDB}.s{_siteID}_sensoravg]的資料表向[{_siteDB}_history.s{_siteID}_sensoravg]搬移"); + string for_insert_sensoravg = $@"INSERT {_siteDB}_history.s{_siteID01}_sensoravg + SELECT * FROM {_siteDB}.s{_siteID01}_sensoravg + WHERE LEFT(crdTime, 7) < '2022-07'"; + int rowCT = conn.Execute(for_insert_sensoravg, commandTimeout: 600); + // log + TimeSpan duration = DateTime.Now - dt_start1; + arclog.insert_log(_powerStationID, "move raw s1", duration.TotalSeconds, $"{_siteDB}.s{_siteID01}_sensoravg", $"{_siteDB}_history.s{_siteID01}_sensoravg", "0", "", rowCT.ToString(), conn, null); + dt_start1 = DateTime.Now; + + string for_delete_sensoravg = $@"DELETE FROM {_siteDB}.s{_siteID01}_sensoravg WHERE LEFT(crdTime, 7) < '2022-07'"; + rowCT = conn.Execute(for_delete_sensoravg, commandTimeout: 600); + + duration = DateTime.Now - dt_start1; + arclog.insert_log(_powerStationID, "delete raw s2", duration.TotalSeconds, $"{_siteDB}.s{_siteID01}_sensoravg", "", "0", "", rowCT.ToString(), conn, null); + + } + catch (Exception ex) + { + TimeSpan duration = DateTime.Now - dt_start1; + arclog.insert_log(_powerStationID, "move raw s1", duration.TotalSeconds, $"{_siteDB}.s{_siteID01}_sensoravg", $"{_siteDB}_history.s{_siteID01}_sensoravg", "1", ex.Message, "0", conn, null); + + throw; + } + #endregion + conn.Close(); } return result; diff --git a/solarApp/Service/getStationSvc.cs b/solarApp/Service/getStationSvc.cs index a46f0f3..4bfbb73 100644 --- a/solarApp/Service/getStationSvc.cs +++ b/solarApp/Service/getStationSvc.cs @@ -180,7 +180,8 @@ namespace solarApp.Service conn.Open(); // 01 控制器編號 string sql = @" select id, CompanyId, `code` , SerialNumber, CONCAT(`code` ,'01') SiteID, SiteDB, `name` SiteName from power_station - where deleted = 0 and `status`= 1"; + where deleted = 0 and `status`= 1 "; +// id <> 14";and id > 24 List ds = conn.Query(sql).AsList(); conn.Close(); return ds; diff --git a/solarApp/Service/procArchiveLog.cs b/solarApp/Service/procArchiveLog.cs index 85e509a..4a429a0 100644 --- a/solarApp/Service/procArchiveLog.cs +++ b/solarApp/Service/procArchiveLog.cs @@ -17,16 +17,23 @@ namespace solarApp.Service /// 異常資訊 /// 連線資訊 /// command物件 - public void insert_log(string power_stationID, string action_name, double duration_sec, string src_table, string dest_table, string procResult, string errMessage, string note, MySqlConnection myCon , MySqlCommand cmd) { - + public void insert_log(string power_stationID, string action_name, double duration_sec, string src_table, string dest_table, string procResult, string errMessage, string note, MySqlConnection myCon , MySqlCommand cmd = null) { + + MySqlCommand _cmd = new MySqlCommand(); + if (cmd == null) { + _cmd.Connection = myCon; + _cmd.CommandTimeout = 720; + } + else _cmd = cmd; + string nowTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); //string procResult = (string.IsNullOrEmpty(errMessage)) ? "0" : "1"; string sql = @"INSERT INTO `solar_master`.`log_archive`(power_stationID, `action_name`, `action_time`, `duration_sec`, `src_table`, `dest_table`, `result`, `err_txt`, `note`) VALUES ("+ power_stationID + ", '" + action_name + "', '" + nowTime + "', " + duration_sec + ", '" + // action_time, duration_sec src_table + "', '"+ dest_table + "', b'"+ procResult + "', '" + // src_table, dest_table, result errMessage + "', '"+ note +"');"; // err_txt - cmd.CommandText = sql; - cmd.ExecuteNonQuery(); + _cmd.CommandText = sql; + _cmd.ExecuteNonQuery(); } } } diff --git a/solarApp/fmArchive.Designer.cs b/solarApp/fmArchive.Designer.cs index 852dc4b..910b07a 100644 --- a/solarApp/fmArchive.Designer.cs +++ b/solarApp/fmArchive.Designer.cs @@ -30,11 +30,12 @@ namespace solarApp private void InitializeComponent() { this.components = new System.ComponentModel.Container(); - System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle(); - System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle(); + System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle3 = new System.Windows.Forms.DataGridViewCellStyle(); + System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle4 = new System.Windows.Forms.DataGridViewCellStyle(); this.tabControl1 = new System.Windows.Forms.TabControl(); this.tabPage1 = new System.Windows.Forms.TabPage(); this.splitContainer1 = new System.Windows.Forms.SplitContainer(); + this.button2 = new System.Windows.Forms.Button(); this.bt_archive_One = new System.Windows.Forms.Button(); this.bt_lackStation = new System.Windows.Forms.Button(); this.bt_sensor_lackData = new System.Windows.Forms.Button(); @@ -83,7 +84,6 @@ namespace solarApp this.tabPage3 = new System.Windows.Forms.TabPage(); this.button1 = new System.Windows.Forms.Button(); this.timer1 = new System.Windows.Forms.Timer(this.components); - this.button2 = new System.Windows.Forms.Button(); this.tabControl1.SuspendLayout(); this.tabPage1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit(); @@ -164,6 +164,17 @@ namespace solarApp this.splitContainer1.SplitterWidth = 10; this.splitContainer1.TabIndex = 0; // + // button2 + // + this.button2.Font = new System.Drawing.Font("Microsoft JhengHei UI", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); + this.button2.Location = new System.Drawing.Point(170, 576); + this.button2.Name = "button2"; + this.button2.Size = new System.Drawing.Size(135, 44); + this.button2.TabIndex = 17; + this.button2.Text = "Sensor 歸檔"; + this.button2.UseVisualStyleBackColor = true; + this.button2.Click += new System.EventHandler(this.button2_Click); + // // bt_archive_One // this.bt_archive_One.Location = new System.Drawing.Point(10, 850); @@ -346,8 +357,8 @@ namespace solarApp // this.gv_inv_detail.AllowUserToAddRows = false; this.gv_inv_detail.AllowUserToDeleteRows = false; - dataGridViewCellStyle1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(255)))), ((int)(((byte)(255))))); - this.gv_inv_detail.AlternatingRowsDefaultCellStyle = dataGridViewCellStyle1; + dataGridViewCellStyle3.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(255)))), ((int)(((byte)(255))))); + this.gv_inv_detail.AlternatingRowsDefaultCellStyle = dataGridViewCellStyle3; this.gv_inv_detail.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; this.gv_inv_detail.Dock = System.Windows.Forms.DockStyle.Fill; this.gv_inv_detail.Location = new System.Drawing.Point(768, 75); @@ -363,8 +374,8 @@ namespace solarApp // this.gv_rpt_invDay.AllowUserToAddRows = false; this.gv_rpt_invDay.AllowUserToDeleteRows = false; - dataGridViewCellStyle2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(255)))), ((int)(((byte)(255))))); - this.gv_rpt_invDay.AlternatingRowsDefaultCellStyle = dataGridViewCellStyle2; + dataGridViewCellStyle4.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(255)))), ((int)(((byte)(255))))); + this.gv_rpt_invDay.AlternatingRowsDefaultCellStyle = dataGridViewCellStyle4; this.gv_rpt_invDay.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; this.gv_rpt_invDay.Dock = System.Windows.Forms.DockStyle.Left; this.gv_rpt_invDay.Location = new System.Drawing.Point(0, 75); @@ -682,16 +693,9 @@ namespace solarApp this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click_1); // - // button2 + // timer1 // - this.button2.Font = new System.Drawing.Font("Microsoft JhengHei UI", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); - this.button2.Location = new System.Drawing.Point(170, 576); - this.button2.Name = "button2"; - this.button2.Size = new System.Drawing.Size(135, 44); - this.button2.TabIndex = 17; - this.button2.Text = "Sensor 歸檔"; - this.button2.UseVisualStyleBackColor = true; - this.button2.Click += new System.EventHandler(this.button2_Click); + this.timer1.Interval = 10000; // // fmArchive // diff --git a/solarApp/fmArchive.cs b/solarApp/fmArchive.cs index 6b41283..a758674 100644 --- a/solarApp/fmArchive.cs +++ b/solarApp/fmArchive.cs @@ -97,6 +97,7 @@ namespace solarApp private void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { + //每日歸檔 if (System.DateTime.Now.Hour == 00 && DateTime.Now.Minute == 15) { //MessageBox.Show("ok"); @@ -106,6 +107,23 @@ namespace solarApp bt_archive.PerformClick(); //bt_archive_Click.PerformClick(); } + + //水位管理 + if (System.DateTime.Now.Hour == 03 && DateTime.Now.Minute == 30 && DateTime.Now.Second == 00) + { + int i = 0; + var site_list = stationSvc.get_station_list(); + + archiveLowData archiveData = new archiveLowData(); + + foreach (var item in site_list) + { + archiveData.archiveData(item.SiteID.Substring(0, 9)); + + i++; + } + lbMsgTitle.Text = System.DateTime.Now.ToString() + " 水位管理 - 資料搬移完成!"; + } } private void rb_site_CheckedChanged(object sender, EventArgs e) @@ -637,9 +655,10 @@ namespace solarApp foreach (var item in site_list) { archiveData.archiveData(item.SiteID.Substring(0, 9)); + i++; } - lbMsgTitle.Text = System.DateTime.Now.ToString() + " 完成!"; + lbMsgTitle.Text = System.DateTime.Now.ToString() + " 水位管理 - 資料搬移完成!"; } private void button2_Click(object sender, EventArgs e)