水位管理 v0.9

This commit is contained in:
cesar liu 2022-08-03 10:06:02 +08:00
parent 2db530de20
commit 439b46ff96
5 changed files with 167 additions and 58 deletions

View File

@ -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;

View File

@ -181,6 +181,7 @@ namespace solarApp.Service
string sql = @" select id, CompanyId, `code` , SerialNumber, CONCAT(`code` ,'01') SiteID, SiteDB, `name` SiteName
from power_station
where deleted = 0 and `status`= 1 ";
// id <> 14";and id > 24
List<station_list> ds = conn.Query<station_list>(sql).AsList<station_list>();
conn.Close();
return ds;

View File

@ -17,7 +17,14 @@ namespace solarApp.Service
/// <param name="errMessage">異常資訊</param>
/// <param name="myCon">連線資訊</param>
/// <param name="cmd">command物件</param>
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";
@ -25,8 +32,8 @@ namespace solarApp.Service
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();
}
}
}

View File

@ -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
//

View File

@ -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)