diff --git a/solarApp/Service/getStationSvc.cs b/solarApp/Service/getStationSvc.cs index 730f73b..7c71f36 100644 --- a/solarApp/Service/getStationSvc.cs +++ b/solarApp/Service/getStationSvc.cs @@ -13,7 +13,20 @@ namespace solarApp.Service /// public class getStationSvc { - string Connection1 = ConfigurationManager.ConnectionStrings["mySql"].ConnectionString; + string Connection1 = ""; + public getStationSvc(string Connection_parame = null) + { + if (!string.IsNullOrEmpty(Connection_parame)) + { + Connection1 = Connection_parame; + } + else + { + Connection1 = ConfigurationManager.ConnectionStrings["mySql"].ConnectionString; + } + + } + //string Connection1 = ConfigurationManager.ConnectionStrings["mySql"].ConnectionString; protected string tableName = "power_station"; /// /// 電站 Raw Data @@ -195,7 +208,7 @@ namespace solarApp.Service string sql = @" select * from power_station where deleted = 0 and `status`= 1"; - List ds = conn.Query(sql).AsList(); + List ds = conn.Query(sql, commandTimeout: 600).AsList(); conn.Close(); return ds; } diff --git a/solarApp/Service/procArchiveHourly.cs b/solarApp/Service/procArchiveHourly.cs index be0c5a0..b3712bf 100644 --- a/solarApp/Service/procArchiveHourly.cs +++ b/solarApp/Service/procArchiveHourly.cs @@ -6,18 +6,31 @@ using Microsoft.Extensions.Logging; using Newtonsoft.Json; using solarApp.Model; using static solarApp.Model.weather_model; +using System.Configuration; namespace solarApp.Service { public class procArchiveHourly { - // private readonly ILogger logger; private readonly ILogger logger; private double carbonRate; //public procArchiveHourly(ILogger logger) //{ // this.logger = logger; //} + string Connection1 = string.Empty; + + public procArchiveHourly(string Connection_parame = null) + { + if (!string.IsNullOrEmpty(Connection_parame)) + { + Connection1 = Connection_parame; + } + else + { + Connection1 = ConfigurationManager.ConnectionStrings["mySql"].ConnectionString; + } + } public List proc_s1_site() { List calcPowerStations = new List(); try @@ -36,8 +49,7 @@ namespace solarApp.Service #region step1. 找出所有電站 // logger.LogInformation("【CalcPowerStationJob】【開始取得電站資料】"); - //var powerStations = powerStationRepository.GetAllAsync(); - getStationSvc stationSvc = new getStationSvc(); + getStationSvc stationSvc = new getStationSvc(Connection1); var powerStations = stationSvc.get_powerStation(); // logger.LogInformation("【CalcPowerStationJob】【取得成功電站資料】"); // logger.LogInformation("【CalcPowerStationJob】【電站資料】 - {0}", System.Text.Json.JsonSerializer.Serialize(powerStations)); @@ -55,22 +67,22 @@ namespace solarApp.Service List weatherObservations = new List(); #region 氣象觀測(取資料) - try - { - // logger.LogInformation("【CalcPowerStationJob】【開始取得氣象觀測】"); - var client = new HttpClient(); - var UVUri = "https://opendata.cwb.gov.tw/api/v1/rest/datastore/O-A0003-001?Authorization=CWB-EA24220B-DDCC-4188-84E5-AD37A0E03F80&elementName=TIME,TEMP"; - HttpResponseMessage response = client.GetAsync(UVUri).Result; - String jsonUVs = response.Content.ReadAsStringAsync().Result.ToString(); - observation = JsonConvert.DeserializeObject(jsonUVs); - // logger.LogInformation("【CalcPowerStationJob】【取得成功氣象觀測】"); - } - catch (Exception ex) - { - // logger.LogError("【CalcPowerStationJob】【取得失敗氣象觀測】"); - // logger.LogError("【{0}】{1}", "CalcPowerStationJob", ex.Message); - observation = null; - } + //try + //{ + // // logger.LogInformation("【CalcPowerStationJob】【開始取得氣象觀測】"); + // var client = new HttpClient(); + // var UVUri = "https://opendata.cwb.gov.tw/api/v1/rest/datastore/O-A0003-001?Authorization=CWB-EA24220B-DDCC-4188-84E5-AD37A0E03F80&elementName=TIME,TEMP"; + // HttpResponseMessage response = client.GetAsync(UVUri).Result; + // String jsonUVs = response.Content.ReadAsStringAsync().Result.ToString(); + // observation = JsonConvert.DeserializeObject(jsonUVs); + // // logger.LogInformation("【CalcPowerStationJob】【取得成功氣象觀測】"); + //} + //catch (Exception ex) + //{ + // // logger.LogError("【CalcPowerStationJob】【取得失敗氣象觀測】"); + // // logger.LogError("【{0}】{1}", "CalcPowerStationJob", ex.Message); + // observation = null; + //} #endregion #region step2. 從電站的DB及電站編號找出該電站的控制器 diff --git a/solarApp/Service/procArchiveSensorHourly.cs b/solarApp/Service/procArchiveSensorHourly.cs index ad64454..fe052f1 100644 --- a/solarApp/Service/procArchiveSensorHourly.cs +++ b/solarApp/Service/procArchiveSensorHourly.cs @@ -20,7 +20,7 @@ namespace solarApp.Service public string _date1 { get; set; } //public string _date2 { get; set; } public string _powerStationID { get; set; } - public procArchiveSensorHourly(string Connection_parame = null, ILogger logger = null) + public procArchiveSensorHourly(string Connection_parame = null) { if (!string.IsNullOrEmpty(Connection_parame)) { @@ -31,10 +31,6 @@ namespace solarApp.Service Connection1 = ConfigurationManager.ConnectionStrings["mySql"].ConnectionString; } - if (logger != null) - { - _logger = logger; - } } public bool get_siteInfo() @@ -48,13 +44,13 @@ namespace solarApp.Service if (_logger != null) { - _logger.LogInformation("【ProcArchiveSensorHourly】開始執行[{0}]在{1}逆變器取得電站編號", _siteID, _date1); + _logger.LogInformation("【ProcArchiveSensorHourly】開始執行[{0}]在{1}取得電站編號", _siteID, _date1); } #region 取得 PowerStationID string sql = @" select id , `code` siteID, siteDB, `name` siteName from solar_master.power_station where `code` = @siteID"; - var ds = conn.Query(sql, new { siteID = _siteID }).AsList(); + var ds = conn.Query(sql, new { siteID = _siteID }, commandTimeout: 600).AsList(); foreach (var item in ds) { _powerStationID = item.id; @@ -64,7 +60,7 @@ namespace solarApp.Service if (_logger != null) { - _logger.LogInformation("【ProcArchiveSensorHourly】執行完成[{0}]在{1}逆變器取得電站編號 - {2}", _siteID, _date1, _powerStationID); + _logger.LogInformation("【ProcArchiveSensorHourly】執行完成[{0}]在{1}取得電站編號 - {2}", _siteID, _date1, _powerStationID); } #endregion @@ -76,8 +72,8 @@ namespace solarApp.Service { if (_logger != null) { - _logger.LogError("【ProcArchiveSensorHourly】執行失敗[{0}]在{1}逆變器取得電站編號", _siteID, _date1); - _logger.LogError("【ProcArchiveSensorHourly】執行失敗[{0}]在{1}逆變器取得電站編號 - [Exception]:{2}", _siteID, _date1, ex.ToString()); + _logger.LogError("【ProcArchiveSensorHourly】執行失敗[{0}]在{1}取得電站編號", _siteID, _date1); + _logger.LogError("【ProcArchiveSensorHourly】執行失敗[{0}]在{1}取得電站編號 - [Exception]:{2}", _siteID, _date1, ex.ToString()); } throw ex; @@ -108,7 +104,7 @@ namespace solarApp.Service select a.id, uid, a.PowerStationID, Enabled, SerialNumber, name, type, SensorTypeId, sensorTypeDetailid, DBName, TableName, ColName - from solar_com0002.device a + from {_siteDB}.device a where a.powerstationid = {_powerStationID} and deleted = 0 and Enabled = 1 -- has sharedevice @@ -116,17 +112,17 @@ namespace solarApp.Service select a.id, uid, b.PowerStationID, Enabled, SerialNumber, name, type, SensorTypeId, sensorTypeDetailid, DBName, TableName, ColName - from solar_com0002.device a - join solar_com0002.`SHAREdevice` b on a.id = b.deviceid + from {_siteDB}.device a + join {_siteDB}.`SHAREdevice` b on a.id = b.deviceid where b.powerstationid = {_powerStationID} and deleted = 0 and Enabled = 1 ) x join sensor_type y on x.sensortypeid = y.id join sensor_type_detail z on y.id = z.SensorTypeId"; - var ds = conn.Query(sql); + var ds = conn.Query(sql, commandTimeout: 600); StringBuilder sb_inserr = new StringBuilder(); StringBuilder sb_select = new StringBuilder(); - sb_select.Append($@"select {_powerStationID}, DATE_FORMAT(left(`crdTime`, 13),'%Y-%m-%d %H:%i:%s'), "); + sb_select.Append($@"select {_powerStationID}, CONCAT('{t1}', ':00:00') as 'TIMESTAMP', "); string irrColName = ""; bool isFirst = true; foreach (var item in ds) @@ -139,7 +135,7 @@ namespace solarApp.Service if (isFirst) { sb_inserr.Append($@"{item.SetWhat}"); - sb_select.Append($@"{item.HourType}({item.ColName})"); + sb_select.Append($@"ifnull({item.HourType}({item.ColName}), 0)"); isFirst = false; } else @@ -152,10 +148,10 @@ namespace solarApp.Service var tb = ds.AsList(); sb_select.Append($@" from {tb[0].DBName}.{tb[0].TableName} where Left(crdTime, 13) = '{t1}'"); string dblocation = tb[0].DBName+"."+tb[0].TableName; - sb_inserr.Insert(0, "insert into solar_master.sensor_history_hour(PowerStationId, TIMESTAMP, "); + sb_inserr.Insert(0, "insert into sensor_history_hour(PowerStationId, TIMESTAMP, ");//sensor_history_hour sb_inserr.Append(") "); string ss = sb_inserr.Append(sb_select.ToString()).ToString(); - conn.Execute(ss); + conn.Execute(ss, commandTimeout: 600); if (irrColName != "") { bool a = countIrr(dblocation, irrColName, t1); @@ -204,7 +200,7 @@ namespace solarApp.Service where Left(crdTime, 13) = '{t1}' and {sensorColNum} <> 0 ) a ON a.ctime = b.TIMESTAMP AND a.powerstationid = b.powerstationId SET Irradiance = a.Irr"; - conn.Execute(ss); + conn.Execute(ss, commandTimeout: 600); conn.Close(); } result = true; @@ -252,7 +248,7 @@ namespace solarApp.Service { IrrDay = 0; } - + string sql2 = $@" select IrrDay from solar_master.sensor_history_hour z where powerstationID = {_powerStationID} and LEFT(z.`TIMESTAMP`, 13 ) = '{Convert.ToDateTime(d1).AddHours(-1).ToString("yyyy-MM-dd HH")}' "; @@ -274,11 +270,11 @@ namespace solarApp.Service { IrrDayHour = IrrDay - twoHourAgoIrrDay;// 前一小時的IrrDay - 前兩小時的IrrDay } - + string ss = $@"UPDATE solar_master.sensor_history_hour SET IrrDayHour = {IrrDayHour} WHERE LEFT(TIMESTAMP, 13) = '{t1}' AND PowerStationId = {_powerStationID}"; - conn.Execute(ss); + conn.Execute(ss, commandTimeout: 600); if (_logger != null) { _logger.LogInformation("【ProcArchiveSensorHourly】執行完成[{0}]在{1}累積日照差更新至資料表的資料", _siteID, _date1); @@ -316,7 +312,7 @@ namespace solarApp.Service string sql = @"delete from solar_master.sensor_history_hour where powerstationID = @powerStationID and left(TIMESTAMP, 10) = @date1"; MySqlCommand cmd = new MySqlCommand(); cmd.Connection = conn; - cmd.CommandTimeout = 360; + cmd.CommandTimeout = 600; cmd.Parameters.AddWithValue("@date1", clearTime); cmd.Parameters.AddWithValue("@PowerStationID", _powerStationID); cmd.CommandText = sql; diff --git a/solarApp/Service/procSensorSvc.cs b/solarApp/Service/procSensorSvc.cs index 3686086..b3857cc 100644 --- a/solarApp/Service/procSensorSvc.cs +++ b/solarApp/Service/procSensorSvc.cs @@ -32,6 +32,7 @@ namespace solarApp.Service public string _date1 { get; set; } //public string _date2 { get; set; } public string _powerStationID { get; set; } + public bool isFirst { get; set; } //public procSensorSvc(string siteDB, string siteID, string date1, string date2) //{ @@ -73,6 +74,17 @@ namespace solarApp.Service return result; } + public bool archiveSensorHistoryHourData(string siteID, string date1) + { + bool result = false; + _siteID = siteID; + _date1 = date1; + get_siteInfo(); + insert_sensor_for_history_hour(); + result = true; + return result; + } + public bool archiveData_test(string siteID, string date1) { @@ -187,7 +199,6 @@ namespace solarApp.Service { procArchiveLog arclog = new procArchiveLog(); bool result = false; - bool isFirst = true; using (MySqlConnection conn = new MySqlConnection(Connection1)) { conn.Open(); @@ -298,34 +309,34 @@ namespace solarApp.Service // )b on LEFT(a.`TIMESTAMP`, 13) = hour_1 // Set irrDayhour = case when a.`irrday` - b.`irrday` < 0 then 0 else a.`irrday` - b.`irrday` end // where a.powerstationID = {_powerStationID}"; - if (isFirst) - { - sql = $@"DROP TEMPORARY TABLE IF EXISTS `temp_s1`; - create TEMPORARY TABLE temp_s1 - select powerstationID, `TIMESTAMP`, irrday - from sensor_history_hour a - where LEFT(a.TIMESTAMP, 10 ) = '{_date1}'; - ALTER TABLE `temp_s1` ADD INDEX `temp_indexs1` (powerstationID, `TIMESTAMP`);"; - dt_start2 = DateTime.Now; - cmd.CommandText = sql; - int updateCT = cmd.ExecuteNonQuery(); - cmd.Parameters.Clear(); - duration = DateTime.Now - dt_start2; - arclog.insert_log(_powerStationID, "sensor step0 irrDayHour init", duration.TotalSeconds, "sensor_history_hour", "update sensor hour", "0", "", updateCT.ToString(), conn, cmd); - } + //if (isFirst) + //{ + // sql = $@"DROP TEMPORARY TABLE IF EXISTS `temp_s1`; + // create TEMPORARY TABLE temp_s1 + // select powerstationID, `TIMESTAMP`, irrday + // from sensor_history_hour a + // where LEFT(a.TIMESTAMP, 10 ) = '{_date1}'; + // ALTER TABLE `temp_s1` ADD INDEX `temp_indexs1` (powerstationID, `TIMESTAMP`);"; + // dt_start2 = DateTime.Now; + // cmd.CommandText = sql; + // int updateCT = cmd.ExecuteNonQuery(); + // cmd.Parameters.Clear(); + // duration = DateTime.Now - dt_start2; + // arclog.insert_log(_powerStationID, "sensor step0 irrDayHour init", duration.TotalSeconds, "sensor_history_hour", "update sensor hour", "0", "", updateCT.ToString(), conn, cmd); + //} - sql = $@" update sensor_history_hour a join - (select powerstationID, LEFT(DATE_ADD(`TIMESTAMP`, INTERVAL +1 hour), 13) hour_1, irrday from temp_s1 ) b - on LEFT(a.`TIMESTAMP`, 13) = hour_1 and a.powerstationID = b.powerstationID - Set irrDayhour = case when a.`irrday` - b.`irrday` < 0 then 0 else a.`irrday` - b.`irrday` end - where LEFT(a.TIMESTAMP, 10 ) = '{_date1}' - and a.powerstationID = {_powerStationID} "; - dt_start2 = DateTime.Now; - cmd.CommandText = sql; - int rowCT2 = cmd.ExecuteNonQuery(); - cmd.Parameters.Clear(); - duration = DateTime.Now - dt_start2; - arclog.insert_log(_powerStationID, "sensor step1 for irrDayHour", duration.TotalSeconds, "sensor_history_hour", "update sensor hour", "0", "", rowCT2.ToString(), conn, cmd); + //sql = $@" update sensor_history_hour a join + // (select powerstationID, LEFT(DATE_ADD(`TIMESTAMP`, INTERVAL +1 hour), 13) hour_1, irrday from temp_s1 ) b + // on LEFT(a.`TIMESTAMP`, 13) = hour_1 and a.powerstationID = b.powerstationID + // Set irrDayhour = case when a.`irrday` - b.`irrday` < 0 then 0 else a.`irrday` - b.`irrday` end + // where LEFT(a.TIMESTAMP, 10 ) = '{_date1}' + // and a.powerstationID = {_powerStationID} "; + //dt_start2 = DateTime.Now; + //cmd.CommandText = sql; + //int rowCT2 = cmd.ExecuteNonQuery(); + //cmd.Parameters.Clear(); + //duration = DateTime.Now - dt_start2; + //arclog.insert_log(_powerStationID, "sensor step1 for irrDayHour", duration.TotalSeconds, "sensor_history_hour", "update sensor hour", "0", "", rowCT2.ToString(), conn, cmd); //sql = $@"update sensor_history_hour a join @@ -548,6 +559,66 @@ namespace solarApp.Service return result; } + bool insert_sensor_for_history_hour() + { + procArchiveLog arclog = new procArchiveLog(); + bool result = false; + using (MySqlConnection conn = new MySqlConnection(Connection1)) + { + conn.Open(); + string sql; + //電表 + DateTime dt_start = DateTime.Now; + try + { + #region hour + MySqlCommand cmd = new MySqlCommand(); + cmd.Connection = conn; + cmd.CommandTimeout = 720; + + DateTime dt_start2 = DateTime.Now; + TimeSpan duration = DateTime.Now - dt_start2; + if (isFirst) + { + sql = $@"DROP TEMPORARY TABLE IF EXISTS `temp_s1`; + create TEMPORARY TABLE temp_s1 + select powerstationID, `TIMESTAMP`, irrday + from sensor_history_hour a + where LEFT(a.TIMESTAMP, 10 ) = '{_date1}'; + ALTER TABLE `temp_s1` ADD INDEX `temp_indexs1` (powerstationID, `TIMESTAMP`);"; + dt_start2 = DateTime.Now; + cmd.CommandText = sql; + int updateCT = cmd.ExecuteNonQuery(); + cmd.Parameters.Clear(); + duration = DateTime.Now - dt_start2; + arclog.insert_log(_powerStationID, "sensor step0 irrDayHour init", duration.TotalSeconds, "sensor_history_hour", "update sensor hour", "0", "", updateCT.ToString(), conn, cmd); + } + + sql = $@" update sensor_history_hour a join + (select powerstationID, LEFT(DATE_ADD(`TIMESTAMP`, INTERVAL +1 hour), 13) hour_1, irrday from temp_s1 ) b + on LEFT(a.`TIMESTAMP`, 13) = hour_1 and a.powerstationID = b.powerstationID + Set irrDayhour = case when a.`irrday` - b.`irrday` < 0 then 0 else a.`irrday` - b.`irrday` end + where LEFT(a.TIMESTAMP, 10 ) = '{_date1}' + and a.powerstationID = {_powerStationID} "; + dt_start2 = DateTime.Now; + cmd.CommandText = sql; + int rowCT2 = cmd.ExecuteNonQuery(); + cmd.Parameters.Clear(); + duration = DateTime.Now - dt_start2; + arclog.insert_log(_powerStationID, "sensor step1 for irrDayHour", duration.TotalSeconds, "sensor_history_hour", "update sensor hour", "0", "", rowCT2.ToString(), conn, cmd); + #endregion hour + + conn.Close(); + result = true; + } + catch (Exception ex) + { + throw ex; + } + } + return result; + } + public bool archiveMeterData(string siteID, string date1) { diff --git a/solarApp/Service/procSyncError.cs b/solarApp/Service/procSyncError.cs index cbc08bd..619de80 100644 --- a/solarApp/Service/procSyncError.cs +++ b/solarApp/Service/procSyncError.cs @@ -202,7 +202,7 @@ namespace solarApp.Service using (MySqlConnection conn = new MySqlConnection(Connection1)) { conn.Open(); - string sql = $@"SELECT a.`*`,ns.Id, + string sql = $@"SELECT a.`*`, case priority when 1 then DATE_ADD(dev_time, INTERVAL 24 HOUR) when 2 then DATE_ADD(dev_time, INTERVAL 12 HOUR) else dev_time end emailTime @@ -235,8 +235,7 @@ namespace solarApp.Service left join power_station ps on ps.`Code` = site_id left join operation_record pr on pr.ErrorCode = a.id where sourceState = 1 and datestamp > '{System.DateTime.Now.AddDays(-60).ToString("yyyy-MM-dd")}' and ps.`Code` is not null - ) a LEFT JOIN notice_schedule ns ON a.id = ns.ExceptionId - WHERE ns.Id IS NULL + ) a order by a.id desc limit 1000"; List ds = conn.Query(sql, commandTimeout: 600).AsList(); conn.Close(); diff --git a/solarApp/fmArchive.cs b/solarApp/fmArchive.cs index 4c64354..cf78d38 100644 --- a/solarApp/fmArchive.cs +++ b/solarApp/fmArchive.cs @@ -41,10 +41,13 @@ namespace solarApp //invSvc._siteID01 = lbSiteID_sensor.Text; //invSvc._date1 = date1; //invSvc._date2 = date1; + sensorSvc.isFirst = true; foreach (DateTime day in EachDay(DateTime.Parse(date1), DateTime.Parse(date2))) { //string d1 = day.ToString("yyyy-MM-dd"); sensorSvc.archiveData(lbSiteID_sensor.Text.Substring(0, 9), day.ToString("yyyy-MM-dd")); + sensorSvc.archiveSensorHistoryHourData(lbSiteID_sensor.Text.Substring(0, 9), day.ToString("yyyy-MM-dd"));/////要改 + sensorSvc.isFirst = false; } //sensorSvc._siteDB = lbSiteDB_sensor.Text; //sensorSvc._siteID = lbSiteID_sensor.Text.Substring(0, 9); @@ -280,7 +283,6 @@ namespace solarApp private void bt_archive_Click(object sender, EventArgs e) { - int i = 0; var site_list = stationSvc.get_station_list(); //bt_Sensor.PerformClick(); @@ -305,8 +307,17 @@ namespace solarApp invSvc.archiveData(item.SiteID.Substring(0, 9), day.ToString("yyyy-MM-dd")); siteSvc.archiveData(item.SiteID.Substring(0, 9), day.ToString("yyyy-MM-dd")); invSvc.report_invDay(item.SiteID.Substring(0, 9), day.ToString("yyyy-MM-dd")); - } - i++; + } + } + sensorSvc.isFirst = true; + foreach (var item in site_list) + { + //for sensor_history_hour + foreach (DateTime day in EachDay(DateTime.Parse(date1), DateTime.Parse(date2))) + { + sensorSvc.archiveSensorHistoryHourData(item.SiteID.Substring(0, 9), day.ToString("yyyy-MM-dd"));/////要改 + sensorSvc.isFirst = false; + } } autoTask = true; lbMsgTitle.Text = System.DateTime.Now.ToString() + " 完成!"; diff --git a/solarApp/fmMain.Designer.cs b/solarApp/fmMain.Designer.cs index f0f8b69..0aeb24e 100644 --- a/solarApp/fmMain.Designer.cs +++ b/solarApp/fmMain.Designer.cs @@ -1685,7 +1685,7 @@ namespace solarApp // button1 // this.button1.Cursor = System.Windows.Forms.Cursors.Arrow; - this.button1.Location = new System.Drawing.Point(1352, 51); + this.button1.Location = new System.Drawing.Point(1603, 51); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(94, 29); this.button1.TabIndex = 3; diff --git a/solarApp/fmMain.cs b/solarApp/fmMain.cs index bca851f..fdb59b6 100644 --- a/solarApp/fmMain.cs +++ b/solarApp/fmMain.cs @@ -332,12 +332,12 @@ namespace solarApp if(label13.Text == "solar_master.notice_schedule資料表") { gv_notice_data.DataSource = errorSvc.QueryAlarmorion_orionalarmrecordData(); - label13.Text = "solar_master.notice_schedule資料表"; + label13.Text = "solar_master.alarmorion_orionalarmrecord資料表"; } else { gv_notice_data.DataSource = errorSvc.QueryNoticeData(); - label13.Text = "solar_master.alarmorion_orionalarmrecord資料表"; + label13.Text = "solar_master.notice_schedule資料表"; } } }