diff --git a/solarApp/App.config b/solarApp/App.config index 83106d1..dfa6bbf 100644 --- a/solarApp/App.config +++ b/solarApp/App.config @@ -1,9 +1,8 @@  - - - + + \ No newline at end of file diff --git a/solarApp/Model/inv_hour.cs b/solarApp/Model/inv_hour.cs index d5163e1..457543a 100644 --- a/solarApp/Model/inv_hour.cs +++ b/solarApp/Model/inv_hour.cs @@ -87,6 +87,16 @@ namespace solarApp.Model public int count { get; set; } } + public class web_inv_15min + { + public string siteID { get; set; } + public string INVERTERID { get; set; } + public string reportdate { get; set; } + public double KWH { get; set; } + public double TODAYKWH { get; set; } + public double kWhKWP { get; set; } + } + public class web_inv_hour { public string INVERTERID { get; set; } diff --git a/solarApp/Model/station_model.cs b/solarApp/Model/station_model.cs index ff0a4f5..3898318 100644 --- a/solarApp/Model/station_model.cs +++ b/solarApp/Model/station_model.cs @@ -40,10 +40,12 @@ namespace solarApp.Model public double PR { get; set; } public double SOLARHOUR { get; set; } public double kwhkwp { get; set; } - //public double MONEY { get; set; } - //public double TODAYMONEY { get; set; } - //public double CARBON { get; set; } - //public double TODAYCARBON { get; set; } + public double MONEY { get; set; } + public double todayMoney { get; set; } + public double totalMoney { get; set; } + public double CARBON { get; set; } + public double todayCarbon { get; set; } + public double totalCarbon { get; set; } } public class web_station_day diff --git a/solarApp/Program.cs b/solarApp/Program.cs index 21ecdcb..478ff41 100644 --- a/solarApp/Program.cs +++ b/solarApp/Program.cs @@ -17,7 +17,8 @@ namespace solarApp Application.SetHighDpiMode(HighDpiMode.SystemAware); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); - Application.Run(new fmMain()); + Application.Run(new fmArchive()); + //Application.Run(new fmMain()); } } } diff --git a/solarApp/Service/getInvSvc.cs b/solarApp/Service/getInvSvc.cs index cfa9b94..c129208 100644 --- a/solarApp/Service/getInvSvc.cs +++ b/solarApp/Service/getInvSvc.cs @@ -67,6 +67,21 @@ namespace solarApp.Service } } + + public List get_web_Inv_15min(string reportDate, string invID, string siteID) + { + using (MySqlConnection conn = new MySqlConnection(Connection1)) + { + conn.Open(); + string sql = @"select `code` siteID, DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d %H:%i') reportdate, inverterid, a.KWH, a.TODAYKWH, round(a.kwhkwp, 3) kwhkwp + from inverter_history_15min a join power_station b on a.powerstationID = b.id + where left(`TIMESTAMP`, 10) = '" + reportDate + "' and inverterid = '" + invID + @"' and `code` = @siteID + order by cast(reportdate as datetime)"; + List ds = conn.Query(sql, new { siteID = siteID }).AsList(); + conn.Close(); + return ds; + } + } /// /// web 呈現值 每小時平均值 inverter - hour /// @@ -91,7 +106,7 @@ namespace solarApp.Service using (MySqlConnection conn = new MySqlConnection(Connection1)) { conn.Open(); - string sql = @"select DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') reportdate, inverterid, round(PR, 2) KWH, round(PR, 2) TODAYKWH, round(PR, 2) TOTALKWH, round(PR, 2) PR + string sql = @"select DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') reportdate, inverterid, round(KWH, 2) KWH, round(TODAYKWH, 2) TODAYKWH, round(TOTALKWH, 2) TOTALKWH, round(PR, 2) PR from inverter_history_day where left(`TIMESTAMP`, 10) between '" + date1 + "' and '"+ date2 +"' and inverterid = '" + invID + "' "; List ds = conn.Query(sql).AsList(); conn.Close(); diff --git a/solarApp/Service/getSensorSvc.cs b/solarApp/Service/getSensorSvc.cs index 76cea75..87b6cda 100644 --- a/solarApp/Service/getSensorSvc.cs +++ b/solarApp/Service/getSensorSvc.cs @@ -47,9 +47,9 @@ namespace solarApp.Service { ss += (ss == string.Empty) ? item.colname : "+" + item.colname; // 2 個以上欄位需要相加 } - if (lstData.Count >= 2) result = "(" + ss + ") / " + lstData.Count.ToString(); // 2個以上 需要除以個數 (平均值) + if (lstData.Count >= 2 && lstData[0].type == "PYR") result = "round( avg( " + ss + ") / " + lstData.Count.ToString() + " , 2) "; //日照計 2個以上 需要除以個數 (平均值) else - result = ss; + result = "round( avg(" + ss + " ), 2) "; } else result = "0"; return result; @@ -61,6 +61,12 @@ namespace solarApp.Service { conn.Open(); + // init 關閉 group full column + string ss = @"SET SESSION sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY,',''));"; + conn.Execute(ss); + + + #region 獲取 Sensor 類別 string sql = @"select a.id, CONCAT( left(UID, 9) ,'01') SiteID, a.`name` sensorName, type, DBName, TableName, colname from " + siteDB + @".device a @@ -103,15 +109,14 @@ namespace solarApp.Service #endregion 獲取 Sensor 類別 sql = @"select a.siteID, a.reportdate, a.modelTempAvg, ifnull(b.irrAvg, 0) irrAvg, a.envTempAvg, humidityAvg, windAvg, dustAvg from ( - select @siteID siteID, FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H:%i') reportdate, round(avg("+ modelTempCol+ @"), 2) modelTempAvg, - round(avg(" + evnTempCol + @"), 2) envTempAvg, round(avg(" + humCol + @"), 2) humidityAvg, round(avg(" + windCol + @"), 2) windAvg, - round(avg(" + dustCol + @"), 2) dustAvg + select @siteID siteID, FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H:%i') reportdate, "+ modelTempCol+ @" modelTempAvg + ," + evnTempCol + @" envTempAvg, " + humCol + @" humidityAvg, " + windCol + @" windAvg, " + dustCol + @" dustAvg from " + siteDB + ".s" + siteID + @"_sensorAvg where FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d') = @reportDate - group by FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H') + group by FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H') ) a left join ( - select concat(FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H'), ':00') reportdate, round(avg(" + irrCol + @"), 2) irrAvg + select FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H:%i') reportdate , " + irrCol + @" irrAvg from " + siteDB + ".s" + siteID + @"_sensorAvg where FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d') = @reportDate "+ irrNot0 + @" #需要過濾 0 的數值 group by FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H') @@ -163,7 +168,50 @@ namespace solarApp.Service string sql = @" select DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d %H') reportdate, b.`code` siteid, round(Irradiance, 2) irrAvg, round(Temperature, 2) modelTempAvg from sensor_history_month a join power_station b on a.PowerStationId = b.id where left(`TIMESTAMP`, 7) between '" + date1 + "' and '" + date2 + "' and b.`code` = @siteID"; - List ds = conn.Query(sql, new { siteID = siteID }).AsList(); + List ds = conn.Query(sql, new { siteID = siteID}).AsList(); + conn.Close(); + return ds; + } + } + + + public List get_web_sensor50_hour(string reportDate, string siteID) + { + using (MySqlConnection conn = new MySqlConnection(Connection1)) + { + conn.Open(); + string sql = @" select DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d %H') reportdate, b.`code` siteid, sensorAVG01, sensorAVG02, sensorAVG03, sensorAVG04, sensorAVG05, sensorAVG06 + from sensoravg_history_hour a join power_station b on a.PowerStationId = b.id + where left(`TIMESTAMP`, 10) = '" + reportDate + "' and b.`code` = @siteID"; + List ds = conn.Query(sql, new { siteID = siteID }).AsList(); + conn.Close(); + return ds; + } + } + + public List get_web_sensor50_day(string date1, string date2, string siteID) + { + using (MySqlConnection conn = new MySqlConnection(Connection1)) + { + conn.Open(); + string sql = @" select DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d %H') reportdate, b.`code` siteid, sensorAVG01, sensorAVG02, sensorAVG03, sensorAVG04, sensorAVG05, sensorAVG06 + from sensoravg_history_day a join power_station b on a.PowerStationId = b.id + where left(`TIMESTAMP`, 10) between @date1 and @date2 and b.`code` = @siteID"; + List ds = conn.Query(sql, new { siteID = siteID, date1 = date1, date2 = date2 }).AsList(); + conn.Close(); + return ds; + } + } + + public List get_web_sensor50_month(string date1, string date2, string siteID) + { + using (MySqlConnection conn = new MySqlConnection(Connection1)) + { + conn.Open(); + string sql = @" select DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d %H') reportdate, b.`code` siteid, sensorAVG01, sensorAVG02, sensorAVG03, sensorAVG04, sensorAVG05, sensorAVG06 + from sensoravg_history_month a join power_station b on a.PowerStationId = b.id + where left(`TIMESTAMP`, 7) between @date1 and @date2 and b.`code` = @siteID"; + List ds = conn.Query(sql, new { siteID = siteID, date1 = date1, date2 = date2 }).AsList(); conn.Close(); return ds; } diff --git a/solarApp/Service/getStationSvc.cs b/solarApp/Service/getStationSvc.cs index 3a62f50..a09c034 100644 --- a/solarApp/Service/getStationSvc.cs +++ b/solarApp/Service/getStationSvc.cs @@ -29,7 +29,7 @@ namespace solarApp.Service string sql = @"select id , FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H:%i') reportdate, siteid, round(KWH, 3) KWH, round(TODAYKWH, 3) TODAYKWH, round(TOTALKWH, 3)TOTALKWH, round(PR, 3) PR, round(TODAYKWH, 3)SOLARHOUR , round(kwhkwp, 3) kwhkwp from " + siteDB + ".s" + siteID + @"_station - where left(FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H:%i'), 10) = @reportDate"; + where FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d') = @reportDate"; List ds = conn.Query(sql, new { reportDate = reportDate }).AsList(); conn.Close(); return ds; @@ -69,11 +69,11 @@ namespace solarApp.Service // from v_station_temp // where left(reportdate, 10) between @date1 and @date2 // group by siteid, left(reportdate, 10)"; - string sql = @" select siteid, left(FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H:%i'), 10) reportdate, round((sum(KWH)), 2) KWH, round((max(TODAYKWH)), 2) TODAYKWH, round((max(TOTALKWH)), 2) TOTALKWH, + string sql = @" select siteid, FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d') reportdate, round((sum(KWH)), 2) KWH, round((max(TODAYKWH)), 2) TODAYKWH, round((max(TOTALKWH)), 2) TOTALKWH, round((max(PR)), 2) PR, round((max(SOLARHOUR)), 2) SOLARHOUR, round((max(KWHKWP)), 2) KWHKWP, count(*) count from " + siteDB+ ".s"+ siteID + @"_station - where left(FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H:%i'), 10) between @date1 and @date2 - group by siteid, left(FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H:%i'), 10)"; + where FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d') between @date1 and @date2 + group by siteid, FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d')"; List ds = conn.Query(sql, new { date1 = date1, date2 = date2 }).AsList(); conn.Close(); return ds; @@ -87,15 +87,17 @@ namespace solarApp.Service /// /// /// - public List get_web_station_hour(string reportDate) + public List get_web_station_hour(string reportDate, string siteID) { using (MySqlConnection conn = new MySqlConnection(Connection1)) { conn.Open(); string sql = @" select DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d %H') reportdate, siteid, round(KWH, 2) KWH, round(TODAYKWH, 2) TODAYKWH, round(TOTALKWH, 2) TOTALKWH, - round(PR, 3) PR, round(KWHKWP, 3) KWHKWP, money - from power_station_history_hour where left(`TIMESTAMP`, 10) = '" + reportDate + "' "; - List ds = conn.Query(sql).AsList(); + round(PR, 3) PR, round(KWHKWP, 3) KWHKWP, round(money, 3) money, round(todaymoney, 3) todaymoney, round(totalmoney, 3) totalmoney, + round(carbon, 3) carbon, round(todayCarbon, 3) todayCarbon, round(totalCarbon, 3) totalCarbon + from power_station_history_hour + where siteID = @siteID and left(`TIMESTAMP`, 10) = '" + reportDate + "' "; + List ds = conn.Query(sql, new { siteID = siteID.Substring(0, 9) }).AsList(); conn.Close(); return ds; } @@ -113,7 +115,7 @@ namespace solarApp.Service { conn.Open(); string sql = @" select DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') reportdate, siteid, round(TODAYKWH, 2) TODAYKWH, round(TOTALKWH, 2) TOTALKWH, - round(PR, 3) PR, round(KWHKWP, 3) KWHKWP, money + round(PR, 3) PR, round(KWHKWP, 3) KWHKWP, money from power_station_history_day where left(`TIMESTAMP`, 10) between @date1 and @date2 and siteid = @siteID"; List ds = conn.Query(sql, new { date1 = date1, date2 = date2 , siteID = siteID}).AsList(); conn.Close(); diff --git a/solarApp/Service/procInvSvc.cs b/solarApp/Service/procInvSvc.cs index 215ff4e..54efdd0 100644 --- a/solarApp/Service/procInvSvc.cs +++ b/solarApp/Service/procInvSvc.cs @@ -12,211 +12,240 @@ namespace solarApp.Service public class procInvSvc { string Connection1 = ConfigurationManager.ConnectionStrings["mySql"].ConnectionString; - /// - /// 每小時平均值 inverter 原始資料 - hour - /// - /// - /// - /// - public List proc_Inv_hour(string reportDate) - { - using (MySqlConnection conn = new MySqlConnection(Connection1)) - { - conn.Open(); - //string sql = @" select a.reportdate, a.inverterid, a.KWH, b.TODAYKWH, b.TOTALKWH, b.PR, a.count from - // ( - // select left(reportdate, 13)reportdate, inverterid - // ,round((sum(WH)/1000), 2) KWH, count(*) count - // from v_inv_temp - // where left(reportdate, 10) = '" + reportDate + "' and inverterid = '" + invID + @"' - // group by left(reportdate, 13), inverterid - // )a join ( - // select reportdate, round(TODAYKWH, 2) TODAYKWH, round(TOTALKWH, 2) TOTALKWH, inverterid, round(PR, 2) PR - // from v_inv_temp - // where left(reportdate, 10) = '" + reportDate + @"' and right(reportdate, 2) = '55' and inverterid = '" + invID + @"' - // )b on a.reportdate = left(b.reportdate, 13) and a.inverterid = b.inverterid "; - string sql = @" -INSERT INTO `solar_master`.`inverter_history_hour`(`PowerStationId`, `INVERTERID`, `TIMESTAMP`, Irradiance, `AC1V`, `AC1A`, `AC1W`, `AC1F`, `AC1WH`, `AC2V`, `AC2A`, `AC2W`, `AC2F`, `AC2WH`, -`AC3V`, `AC3A`, `AC3W`, `AC3F`, `AC3WH`, `DC1V`, `DC1A`, `DC1W`, `DC1WH`, `DC2V`, `DC2A`, `DC2W`, `DC2WH`, `DC3V`, `DC3A`, `DC3W`, -`DC3WH`, `DC4V`, `DC4A`, `DC4W`, `DC4WH`, `DC5V`, `DC5A`, `DC5W`, `DC5WH`, `PR`, `RA1`, `RA2`, `RA3`, `RA4`, `RA5`, `ACKW`, `KWH`, `TODAYKWH`, `TOTALKWH`, `KWHKWP`) -SELECT 1 powerStationID, a.INVERTERID, concat(a.reportDate,':00:00') reportDate, IFNULL(Irradiance, 0), a.AC1V, a.AC1A, a.AC1W, a.AC1F, a.AC1WH, a.AC2V, a.AC2A, a.AC2W, a.AC2F, a.AC2WH, - a.AC3V, a.AC3A, a.AC3W, a.AC3F, a.AC3WH, a.DC1V, a.DC1A, a.DC1W, a.DC1WH, a.DC2V, a.DC2A, a.DC2W, a.DC2WH, a.DC3V, a.DC3A, a.DC3W, - a.DC3WH, a.DC4V, a.DC4A, a.DC4W, a.DC4WH, a.DC5V, a.DC5A, a.DC5W, a.DC5WH, inv_pr.PR, a.RA1, a.RA2, a.RA3, a.RA4, a.RA5, a.KWH, a.TODAYKWH, a.TOTALKWH, - i.Capacity, (a.KWH / i.Capacity) AS KWHKWP -from -( -- 取得該時間區間的KWH - SELECT FROM_UNIXTIME(a.TIMESTAMP / 1000, '%Y-%m-%d %H') AS reportDate, a.INVERTERID, - AVG(a.AC1V) AS AC1V, AVG(a.AC1A) AS AC1A, SUM(a.AC1W) AS AC1W, - AVG(a.AC1F) AS AC1F, SUM(a.AC1WH) AS AC1WH, AVG(a.AC2V) AS AC2V, - AVG(a.AC2A) AS AC2A, SUM(a.AC2W) AS AC2W, AVG(a.AC2F) AS AC2F, - SUM(a.AC2WH) AS AC2WH,AVG(a.AC3V) AS AC3V, AVG(a.AC3A) AS AC3A, - SUM(a.AC3W) AS AC3W, AVG(a.AC3F) AS AC3F, SUM(a.AC3WH) AS AC3WH, - AVG(a.DC1V) AS DC1V, AVG(a.DC1A) AS DC1A, SUM(a.DC1W) AS DC1W, - SUM(a.DC1WH) AS DC1WH, AVG(a.DC2V) AS DC2V, AVG(a.DC2A) AS DC2A, - SUM(a.DC2W) AS DC2W, SUM(a.DC2WH) AS DC2WH, AVG(a.DC3V) AS DC3V, - AVG(a.DC3A) AS DC3A, AVG(a.DC3W) AS DC3W, AVG(a.DC3WH) AS DC3WH, - AVG(a.DC4V) AS DC4V, AVG(a.DC4A) AS DC4A, SUM(a.DC4W) AS DC4W, - SUM(a.DC4WH) AS DC4WH,AVG(a.DC5V) AS DC5V, AVG(a.DC5A) AS DC5A, - SUM(a.DC5W) AS DC5W, SUM(a.DC5WH) AS DC5WH, #inv_pr.PR AS PR, - AVG(a.RA1) AS RA1, AVG(a.RA2) AS RA2, AVG(a.RA3) AS RA3, - AVG(a.RA4) AS RA4, AVG(a.RA5) AS RA5, # a.KWH, - MAX(a.TODAYKWH) AS TODAYKWH, MAX(a.TOTALKWH) AS TOTALKWH, (SUM(a.WH)/1000) AS KWH - FROM s02202000101_inv a - WHERE left(FROM_UNIXTIME(a.TIMESTAMP / 1000, '%Y-%m-%d'), 10) = '2021-08-04' - GROUP BY left(FROM_UNIXTIME(a.TIMESTAMP/ 1000, '%Y-%m-%d %H'), 13), a.INVERTERID -) a -LEFT JOIN( -- 取得整點值PR - SELECT FROM_UNIXTIME(inv.TIMESTAMP / 1000, '%Y-%m-%d %H') AS reportDate, inv.INVERTERID, inv.PR - FROM s02202000101_inv inv - WHERE left(DATE_FORMAT(FROM_UNIXTIME(inv.TIMESTAMP / 1000), '%Y-%m-%d %H:%i'), 10) = '2021-08-04' - and right(DATE_FORMAT(FROM_UNIXTIME(inv.TIMESTAMP / 1000), '%Y-%m-%d %H:%i'), 2) = '55' - GROUP BY FROM_UNIXTIME(inv.TIMESTAMP/ 1000, '%Y-%m-%d %H'), inv.INVERTERID -) inv_pr -ON a.reportDate = inv_pr.reportDate AND a.INVERTERID = inv_pr.INVERTERID --- 取得逆變器容量 -LEFT JOIN solar_com0002.inverter i ON a.INVERTERID = i.InverterId --- 取日照度 ----------------------臨時做法 -left join ( - select FROM_UNIXTIME(a.TIMESTAMP / 1000, '%Y-%m-%d %H') AS reportDate, round(avg(SENSORAVG01),3) Irradiance - from s02202000101_sensoravg a - where left(FROM_UNIXTIME(a.TIMESTAMP / 1000, '%Y-%m-%d'), 10) = '2021-08-04' and SENSORAVG01 <> 0 - group by FROM_UNIXTIME(a.TIMESTAMP / 1000, '%Y-%m-%d %H') -)irr on a.reportDate = irr.reportDate ;"; - List ds = conn.Query(sql).AsList(); - conn.Close(); - return ds; + public string _siteID { get; set; } + public string _siteDB { get; set; } + public string _siteID01 { get; set; } + public string _date1 { get; set; } + public string _date2 { get; set; } + public string _powerStationID { get; set; } + + + public bool clearData() + { + bool result = false; + try + { + #region 取得 PowerStationID + using (MySqlConnection conn = new MySqlConnection(Connection1)) + { + conn.Open(); + string sql = @" select id as PowerStationID from solar_master.power_station where `code` = @siteID"; + var ds = conn.Query(sql, new { siteID = _siteID }).AsList(); + _powerStationID = (ds.Count > 0) ? ds[0][0].ToString() : "0"; + conn.Close(); + } + #endregion + + using (MySqlConnection conn = new MySqlConnection(Connection1)) + { + conn.Open(); + string sql = @" + delete from inverter_history_15min where powerstationID = @powerStationID and DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') = @date1; + delete from inverter_history_hour where powerstationID = @powerStationID and DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') = @date1; + delete from inverter_history_day where powerstationID = @powerStationID and DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') = @date1; + delete from inverter_history_month where powerstationID = @powerStationID and left(`TIMESTAMP`, 7) = left(@date1, 7) ;"; + var ds = conn.Execute(sql, new { date1 = _date1, PowerStationID = _powerStationID }); + conn.Close(); + } + result = true; } + catch (Exception ex) + { + throw ex; + } + return result; } - public List proc_Inv_day(string reportDate) + public bool archiveData() { - using (MySqlConnection conn = new MySqlConnection(Connection1)) + + bool result = false; + try { - conn.Open(); - string sql = @" -INSERT INTO `solar_master`.`inverter_history_day`(`PowerStationId`, `INVERTERID`, `TIMESTAMP`, Irradiance, `AC1V`, `AC1A`, `AC1W`, `AC1F`, `AC1WH`, `AC2V`, `AC2A`, `AC2W`, `AC2F`, `AC2WH`, -`AC3V`, `AC3A`, `AC3W`, `AC3F`, `AC3WH`, `DC1V`, `DC1A`, `DC1W`, `DC1WH`, `DC2V`, `DC2A`, `DC2W`, `DC2WH`, `DC3V`, `DC3A`, `DC3W`, -`DC3WH`, `DC4V`, `DC4A`, `DC4W`, `DC4WH`, `DC5V`, `DC5A`, `DC5W`, `DC5WH`, `PR`, `RA1`, `RA2`, `RA3`, `RA4`, `RA5`, `ACKW`, `KWH`, `TODAYKWH`, `TOTALKWH`, `KWHKWP`) - -SELECT 1 powerStationID, a.INVERTERID, concat(a.reportDate,':00:00') reportDate, IFNULL(Irradiance, 0), a.AC1V, a.AC1A, a.AC1W, a.AC1F, a.AC1WH, a.AC2V, a.AC2A, a.AC2W, a.AC2F, a.AC2WH, - a.AC3V, a.AC3A, a.AC3W, a.AC3F, a.AC3WH, a.DC1V, a.DC1A, a.DC1W, a.DC1WH, a.DC2V, a.DC2A, a.DC2W, a.DC2WH, a.DC3V, a.DC3A, a.DC3W, - a.DC3WH, a.DC4V, a.DC4A, a.DC4W, a.DC4WH, a.DC5V, a.DC5A, a.DC5W, a.DC5WH, a.PR, a.RA1, a.RA2, a.RA3, a.RA4, a.RA5, a.KWH, a.TODAYKWH, a.TOTALKWH, - i.Capacity, (a.KWH / i.Capacity) AS KWHKWP -from -( -- 取得該時間區間的KWH - SELECT FROM_UNIXTIME(a.TIMESTAMP / 1000, '%Y-%m-%d') AS reportDate, a.INVERTERID, - AVG(a.AC1V) AS AC1V, AVG(a.AC1A) AS AC1A, SUM(a.AC1W) AS AC1W, - AVG(a.AC1F) AS AC1F, SUM(a.AC1WH) AS AC1WH, AVG(a.AC2V) AS AC2V, - AVG(a.AC2A) AS AC2A, SUM(a.AC2W) AS AC2W, AVG(a.AC2F) AS AC2F, - SUM(a.AC2WH) AS AC2WH,AVG(a.AC3V) AS AC3V, AVG(a.AC3A) AS AC3A, - SUM(a.AC3W) AS AC3W, AVG(a.AC3F) AS AC3F, SUM(a.AC3WH) AS AC3WH, - AVG(a.DC1V) AS DC1V, AVG(a.DC1A) AS DC1A, SUM(a.DC1W) AS DC1W, - SUM(a.DC1WH) AS DC1WH, AVG(a.DC2V) AS DC2V, AVG(a.DC2A) AS DC2A, - SUM(a.DC2W) AS DC2W, SUM(a.DC2WH) AS DC2WH, AVG(a.DC3V) AS DC3V, - AVG(a.DC3A) AS DC3A, AVG(a.DC3W) AS DC3W, AVG(a.DC3WH) AS DC3WH, - AVG(a.DC4V) AS DC4V, AVG(a.DC4A) AS DC4A, SUM(a.DC4W) AS DC4W, - SUM(a.DC4WH) AS DC4WH,AVG(a.DC5V) AS DC5V, AVG(a.DC5A) AS DC5A, - SUM(a.DC5W) AS DC5W, SUM(a.DC5WH) AS DC5WH, #inv_pr.PR AS PR, - AVG(a.RA1) AS RA1, AVG(a.RA2) AS RA2, AVG(a.RA3) AS RA3, - AVG(a.RA4) AS RA4, AVG(a.RA5) AS RA5, # a.KWH, - AVG(a.PR) as PR, - MAX(a.TODAYKWH) AS TODAYKWH, MAX(a.TOTALKWH) AS TOTALKWH, (SUM(a.WH)/1000) AS KWH - FROM solar_com0002.s02202000101_inv a - WHERE left(FROM_UNIXTIME(a.TIMESTAMP / 1000, '%Y-%m-%d'), 10) = '2021-08-04' - GROUP BY left(FROM_UNIXTIME(a.TIMESTAMP/ 1000, '%Y-%m-%d'), 10), a.INVERTERID -) a --- LEFT JOIN( -- 取得整點值PR --- SELECT FROM_UNIXTIME(inv.TIMESTAMP / 1000, '%Y-%m-%d') AS reportDate, inv.INVERTERID, inv.PR --- FROM s02202000101_inv inv --- WHERE left(FROM_UNIXTIME(a.TIMESTAMP / 1000, '%Y-%m-%d'), 10) = '2021-08-04' --- and right(DATE_FORMAT(FROM_UNIXTIME(inv.TIMESTAMP / 1000), '%Y-%m-%d %H:%i'), 2) = '55' --- GROUP BY FROM_UNIXTIME(inv.TIMESTAMP/ 1000, '%Y-%m-%d %H'), inv.INVERTERID --- ) inv_pr --- ON a.reportDate = inv_pr.reportDate AND a.INVERTERID = inv_pr.INVERTERID --- 取得逆變器容量 -LEFT JOIN solar_com0002.inverter i ON a.INVERTERID = i.InverterId --- 取日照度 ----------------------臨時做法 -left join ( - select FROM_UNIXTIME(a.TIMESTAMP / 1000, '%Y-%m-%d') AS reportDate, round(avg(SENSORAVG01),3) Irradiance - from s02202000101_sensoravg a - where left(FROM_UNIXTIME(a.TIMESTAMP / 1000, '%Y-%m-%d'), 10) = '2021-08-04' and SENSORAVG01 <> 0 - group by FROM_UNIXTIME(a.TIMESTAMP / 1000, '%Y-%m-%d') -)irr on a.reportDate = irr.reportDate ;"; - List ds = conn.Query(sql).AsList(); - conn.Close(); - return ds; + clearData(); + insert_inv(); + result = true; } - } + catch (Exception ex) + { + throw ex; + } + return result; + } - public List proc_station_hour(string reportDate) + bool insert_inv() { - using (MySqlConnection conn = new MySqlConnection(Connection1)) + bool result = false; + try { - conn.Open(); - string sql = @" -INSERT INTO `solar_master`.`power_station_history_hour`(`PowerStationId`, `TIMESTAMP`, `SITEID`, `SITETYPE`, `KWH`, `TODAYKWH`, `TOTALKWH`, `KWHKWP`, `PR`, `MP`, `SOLARHOUR`, `MONEY`, `TODAYMONEY`, `TOTALMONEY`, `CARBON`, `TODAYCARBON`, `TOTALCARBON`) + using (MySqlConnection conn = new MySqlConnection(Connection1)) + { + conn.Open(); + string ss = @"SET SESSION sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY,',''));"; + conn.Execute(ss); -select 1 PowerStationId, concat(FROM_UNIXTIME(`TIMESTAMP`/1000, '%Y-%m-%d %H'), ':00:00') reportdate, siteid, 0 SITETYPE, round((sum(KWH)), 2) KWH, round((max(TODAYKWH)), 2) TODAYKWH, round((max(TOTALKWH)), 2) TOTALKWH, round((max(KWHKWP)), 2) KWHKWP, round((max(PR)), 2) PR, 0 as MP, round((max(SOLARHOUR)), 2) SOLARHOUR, - 0, 0 , 0, 0, 0 , 0 -from solar_com0002.s02202000101_station -where FROM_UNIXTIME(`TIMESTAMP`/1000, '%Y-%m-%d') = '2021-08-04' -group by siteid, FROM_UNIXTIME(`TIMESTAMP`/1000, '%Y-%m-%d %H')"; - List ds = conn.Query(sql).AsList(); - conn.Close(); - return ds; + #region 15 min + string sql = @" + INSERT INTO solar_master.inverter_history_15min( PowerStationId, TIMESTAMP, INVERTERID, KWH, TODAYKWH, KWHKWP) + select PowerStationId, reportdate, a.inverterid, KWH, TODAYKWH, round((kwh/capacity), 3) kwpkwp from + ( + select @PowerStationId PowerStationId, CONCAT(FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H'), ':00:00')reportdate, inverterid, + round((sum(WH)/1000), 3) KWH, round(max(TODAYKWH), 3) TODAYKWH + from " + _siteDB + ".s" + _siteID01 + @"_inv + where FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d') = @date1 and right(FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H:%i') , 2) between '00' and '10' + group by FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H') , inverterid + union + select @PowerStationId PowerStationId, CONCAT(FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H'), ':15:00')reportdate, inverterid, + round((sum(WH)/1000), 3) KWH, round(max(TODAYKWH), 3) TODAYKWH + from " + _siteDB + ".s" + _siteID01 + @"_inv + where FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d') = @date1 and right(FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H:%i') , 2) between '15' and '25' + group by FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H') , inverterid + union + select @PowerStationId PowerStationId, CONCAT(FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H'), ':30:00')reportdate, inverterid, + round((sum(WH)/1000), 3) KWH, round(max(TODAYKWH), 3) TODAYKWH + from " + _siteDB + ".s" + _siteID01 + @"_inv + where FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d') = @date1 and right(FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H:%i') , 2) between '30' and '40' + group by FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H') , inverterid + union + select @PowerStationId PowerStationId, CONCAT(FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H'), ':45:00')reportdate, inverterid, + round((sum(WH)/1000), 3) KWH, round(max(TODAYKWH), 3) TODAYKWH + from " + _siteDB + ".s" + _siteID01 + @"_inv + where FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d') = @date1 and right(FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H:%i') , 2) between '45' and '55' + group by FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H') , inverterid + ) a join " + _siteDB + @".inverter b on a.inverterid = b.inverterid"; + var ds = conn.Execute(sql, new { date1 = _date1, PowerStationID = _powerStationID }); + #endregion + + #region hour + sql = @" + insert into solar_master.inverter_history_hour (PowerStationId, INVERTERID, TIMESTAMP, Irradiance, AC1V, AC1A, AC1W, AC1F, AC1WH, AC2V, AC2A, AC2W, AC2F, AC2WH, + AC3V, AC3A, AC3W, AC3F, AC3WH, DC1V, DC1A, DC1W, DC1WH, DC2V, DC2A, DC2W, DC2WH, DC3V, DC3A, DC3W, DC3WH, DC4V, DC4A, DC4W, DC4WH, DC5V, DC5A, DC5W, DC5WH, + PR, RA1, RA2, RA3, RA4, RA5, DCKW, ACKW, KWH, TODAYKWH, TOTALKWH, KWHKWP) + + SELECT @powerstationID powerStationID, a.INVERTERID, a.reportDate, IFNULL(Irradiance, 0), a.AC1V, a.AC1A, a.AC1W, a.AC1F, a.AC1WH, a.AC2V, a.AC2A, a.AC2W, a.AC2F, a.AC2WH, + a.AC3V, a.AC3A, a.AC3W, a.AC3F, a.AC3WH, a.DC1V, a.DC1A, a.DC1W, a.DC1WH, a.DC2V, a.DC2A, a.DC2W, a.DC2WH, a.DC3V, a.DC3A, a.DC3W, + a.DC3WH, a.DC4V, a.DC4A, a.DC4W, a.DC4WH, a.DC5V, a.DC5A, a.DC5W, a.DC5WH, + inv_pr.PR, a.RA1, a.RA2, a.RA3, a.RA4, a.RA5, ((DC1W + DC2W + DC3W + DC4W+ DC5W) / 1000) DCKW, + ((AC1W + AC2W + AC3W) / 1000) ACKW, a.KWH, a.TODAYKWH, a.TOTALKWH, (a.KWH / i.Capacity) AS KWHKWP + from + ( -- 取得該時間區間的KWH + SELECT concat(FROM_UNIXTIME(a.TIMESTAMP/1000,'%Y-%m-%d %H'), ':00:00') reportDate, a.INVERTERID, + AVG(a.AC1V) AS AC1V, AVG(a.AC1A) AS AC1A, SUM(a.AC1W) AS AC1W, AVG(a.AC1F) AS AC1F, SUM(a.AC1WH) AS AC1WH, AVG(a.AC2V) AS AC2V, + AVG(a.AC2A) AS AC2A, SUM(a.AC2W) AS AC2W, AVG(a.AC2F) AS AC2F, SUM(a.AC2WH) AS AC2WH,AVG(a.AC3V) AS AC3V, AVG(a.AC3A) AS AC3A, + SUM(a.AC3W) AS AC3W, AVG(a.AC3F) AS AC3F, SUM(a.AC3WH) AS AC3WH, AVG(a.DC1V) AS DC1V, AVG(a.DC1A) AS DC1A, SUM(a.DC1W) AS DC1W, + SUM(a.DC1WH) AS DC1WH, AVG(a.DC2V) AS DC2V, AVG(a.DC2A) AS DC2A, SUM(a.DC2W) AS DC2W, SUM(a.DC2WH) AS DC2WH, AVG(a.DC3V) AS DC3V, + AVG(a.DC3A) AS DC3A, AVG(a.DC3W) AS DC3W, AVG(a.DC3WH) AS DC3WH, AVG(a.DC4V) AS DC4V, AVG(a.DC4A) AS DC4A, SUM(a.DC4W) AS DC4W, + SUM(a.DC4WH) AS DC4WH,AVG(a.DC5V) AS DC5V, AVG(a.DC5A) AS DC5A, SUM(a.DC5W) AS DC5W, SUM(a.DC5WH) AS DC5WH, + AVG(a.RA1) AS RA1, AVG(a.RA2) AS RA2, AVG(a.RA3) AS RA3, + AVG(a.RA4) AS RA4, AVG(a.RA5) AS RA5, MAX(a.TODAYKWH) AS TODAYKWH, MAX(a.TOTALKWH) AS TOTALKWH, (SUM(a.WH)/1000) AS KWH + FROM " + _siteDB + ".s" + _siteID01 + @"_inv a + WHERE left(FROM_UNIXTIME(a.TIMESTAMP / 1000, '%Y-%m-%d'), 10) = @date1 + GROUP BY left(FROM_UNIXTIME(a.TIMESTAMP/ 1000, '%Y-%m-%d %H'), 13), a.INVERTERID + ) a + LEFT JOIN( -- 取得整點值PR + SELECT concat(FROM_UNIXTIME(inv.TIMESTAMP/1000,'%Y-%m-%d %H'), ':00:00') AS reportDate, inv.INVERTERID, inv.PR + FROM " + _siteDB + ".s" + _siteID01 + @"_inv inv + WHERE left(DATE_FORMAT(FROM_UNIXTIME(inv.TIMESTAMP / 1000), '%Y-%m-%d %H:%i'), 10) = @date1 + and right(DATE_FORMAT(FROM_UNIXTIME(inv.TIMESTAMP / 1000), '%Y-%m-%d %H:%i'), 2) = '55' + GROUP BY FROM_UNIXTIME(inv.TIMESTAMP/ 1000, '%Y-%m-%d %H'), inv.INVERTERID + ) inv_pr + ON a.reportDate = inv_pr.reportDate AND a.INVERTERID = inv_pr.INVERTERID + -- 取得逆變器容量 + LEFT JOIN " + _siteDB + @".inverter i ON a.INVERTERID = i.InverterId + -- 取日照度 --------------------- + left join ( + select concat(DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d %H'), ':00:00') AS reportDate, Irradiance + from sensor_history_hour a + where powerstationID = @powerstationID and DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') = @date1 + )irr on a.reportDate = irr.reportDate + order by a.INVERTERID, a.reportDate;"; + int ct = conn.Execute(sql, new { date1 = _date1, PowerStationID = _powerStationID }); + + #endregion hour + + #region day + sql = @" + INSERT INTO solar_master.inverter_history_day(PowerStationId, INVERTERID, TIMESTAMP, Irradiance, AC1V, AC1A, AC1W, AC1F, AC1WH, AC2V, AC2A, AC2W, AC2F, AC2WH, + AC3V, AC3A, AC3W, AC3F, AC3WH, DC1V, DC1A, DC1W, DC1WH, DC2V, DC2A, DC2W, DC2WH, DC3V, DC3A, DC3W, + DC3WH, DC4V, DC4A, DC4W, DC4WH, DC5V, DC5A, DC5W, DC5WH, PR, RA1, RA2, RA3, RA4, RA5, `DCKW`, `ACKW`, KWH, TODAYKWH, TOTALKWH, KWHKWP) + + SELECT @powerStationID powerStationID, a.INVERTERID, concat(a.reportDate,' 00:00:00') reportDate, IFNULL(Irradiance, 0), a.AC1V, a.AC1A, a.AC1W, a.AC1F, a.AC1WH, a.AC2V, a.AC2A, a.AC2W, a.AC2F, a.AC2WH, + a.AC3V, a.AC3A, a.AC3W, a.AC3F, a.AC3WH, a.DC1V, a.DC1A, a.DC1W, a.DC1WH, a.DC2V, a.DC2A, a.DC2W, a.DC2WH, a.DC3V, a.DC3A, a.DC3W, + a.DC3WH, a.DC4V, a.DC4A, a.DC4W, a.DC4WH, a.DC5V, a.DC5A, a.DC5W, a.DC5WH, a.PR, a.RA1, a.RA2, a.RA3, a.RA4, a.RA5, + DCKW, ACKW, a.KWH, a.TODAYKWH, a.TOTALKWH, (a.KWH / i.Capacity) AS KWHKWP + from + ( -- 取得該時間區間的KWH + SELECT DATE_FORMAT(a.TIMESTAMP, '%Y-%m-%d') AS reportDate, a.INVERTERID, + AVG(a.AC1V) AS AC1V, AVG(a.AC1A) AS AC1A, SUM(a.AC1W) AS AC1W, AVG(a.AC1F) AS AC1F, SUM(a.AC1WH) AS AC1WH, AVG(a.AC2V) AS AC2V, + AVG(a.AC2A) AS AC2A, SUM(a.AC2W) AS AC2W, AVG(a.AC2F) AS AC2F, SUM(a.AC2WH) AS AC2WH,AVG(a.AC3V) AS AC3V, AVG(a.AC3A) AS AC3A, + SUM(a.AC3W) AS AC3W, AVG(a.AC3F) AS AC3F, SUM(a.AC3WH) AS AC3WH, AVG(a.DC1V) AS DC1V, AVG(a.DC1A) AS DC1A, SUM(a.DC1W) AS DC1W, + SUM(a.DC1WH) AS DC1WH, AVG(a.DC2V) AS DC2V, AVG(a.DC2A) AS DC2A, SUM(a.DC2W) AS DC2W, SUM(a.DC2WH) AS DC2WH, AVG(a.DC3V) AS DC3V, + AVG(a.DC3A) AS DC3A, AVG(a.DC3W) AS DC3W, AVG(a.DC3WH) AS DC3WH, AVG(a.DC4V) AS DC4V, AVG(a.DC4A) AS DC4A, SUM(a.DC4W) AS DC4W, + SUM(a.DC4WH) AS DC4WH,AVG(a.DC5V) AS DC5V, AVG(a.DC5A) AS DC5A, SUM(a.DC5W) AS DC5W, SUM(a.DC5WH) AS DC5WH, max(PR) AS PR, + AVG(a.RA1) AS RA1, AVG(a.RA2) AS RA2, AVG(a.RA3) AS RA3, AVG(a.RA4) AS RA4, AVG(a.RA5) AS RA5, avg(DCKW) as DCKW, avg(ACKW) as ACKW, + MAX(a.TODAYKWH) AS TODAYKWH, MAX(a.TOTALKWH) AS TOTALKWH, SUM(a.KWH) AS KWH + FROM solar_master.inverter_history_hour a + WHERE powerstationID = @powerstationID and DATE_FORMAT(a.TIMESTAMP, '%Y-%m-%d') = @date1 + GROUP BY DATE_FORMAT(a.TIMESTAMP, '%Y-%m-%d'), a.INVERTERID + ) a + LEFT JOIN " + _siteDB + @".inverter i ON a.INVERTERID = i.InverterId + -- 取日照度 ---------------------- + left join ( + select DATE_FORMAT(a.TIMESTAMP, '%Y-%m-%d') AS reportDate, Irradiance + from sensor_history_day a + where powerstationID = @powerstationID and DATE_FORMAT(a.TIMESTAMP, '%Y-%m-%d') = @date1 and Irradiance <> 0 limit 1 + )irr on a.reportDate = irr.reportDate ;"; + conn.Execute(sql, new { date1 = _date1, PowerStationID = _powerStationID }); + #endregion day + + #region month + sql = @" + delete from inverter_history_month where powerstationID = @powerStationID and left(`TIMESTAMP`, 7) = left(@date1, 7) ; + + INSERT INTO solar_master.inverter_history_month (PowerStationId, INVERTERID, TIMESTAMP, Irradiance, AC1V, AC1A, AC1W, AC1F, AC1WH, AC2V, AC2A, AC2W, AC2F, AC2WH, + AC3V, AC3A, AC3W, AC3F, AC3WH, DC1V, DC1A, DC1W, DC1WH, DC2V, DC2A, DC2W, DC2WH, DC3V, DC3A, DC3W, + DC3WH, DC4V, DC4A, DC4W, DC4WH, DC5V, DC5A, DC5W, DC5WH, PR, RA1, RA2, RA3, RA4, RA5, `DCKW`, `ACKW`, KWH, TODAYKWH, TOTALKWH, KWHKWP) + + SELECT @powerStationID powerStationID, a.INVERTERID, concat(a.reportDate,'-01 00:00:00') reportDate, IFNULL(Irradiance, 0), a.AC1V, a.AC1A, a.AC1W, a.AC1F, a.AC1WH, a.AC2V, a.AC2A, a.AC2W, a.AC2F, a.AC2WH, + a.AC3V, a.AC3A, a.AC3W, a.AC3F, a.AC3WH, a.DC1V, a.DC1A, a.DC1W, a.DC1WH, a.DC2V, a.DC2A, a.DC2W, a.DC2WH, a.DC3V, a.DC3A, a.DC3W, + a.DC3WH, a.DC4V, a.DC4A, a.DC4W, a.DC4WH, a.DC5V, a.DC5A, a.DC5W, a.DC5WH, a.PR, a.RA1, a.RA2, a.RA3, a.RA4, a.RA5, DCKW, ACKW, a.KWH, a.TODAYKWH, a.TOTALKWH, + (a.KWH / i.Capacity) AS KWHKWP + from + ( -- 取得該時間區間的KWH + SELECT DATE_FORMAT(a.TIMESTAMP, '%Y-%m') AS reportDate, a.INVERTERID, + AVG(a.AC1V) AS AC1V, AVG(a.AC1A) AS AC1A, SUM(a.AC1W) AS AC1W, AVG(a.AC1F) AS AC1F, SUM(a.AC1WH) AS AC1WH, AVG(a.AC2V) AS AC2V, + AVG(a.AC2A) AS AC2A, SUM(a.AC2W) AS AC2W, AVG(a.AC2F) AS AC2F, SUM(a.AC2WH) AS AC2WH,AVG(a.AC3V) AS AC3V, AVG(a.AC3A) AS AC3A, + SUM(a.AC3W) AS AC3W, AVG(a.AC3F) AS AC3F, SUM(a.AC3WH) AS AC3WH, AVG(a.DC1V) AS DC1V, AVG(a.DC1A) AS DC1A, SUM(a.DC1W) AS DC1W, + SUM(a.DC1WH) AS DC1WH, AVG(a.DC2V) AS DC2V, AVG(a.DC2A) AS DC2A, SUM(a.DC2W) AS DC2W, SUM(a.DC2WH) AS DC2WH, AVG(a.DC3V) AS DC3V, + AVG(a.DC3A) AS DC3A, AVG(a.DC3W) AS DC3W, AVG(a.DC3WH) AS DC3WH, AVG(a.DC4V) AS DC4V, AVG(a.DC4A) AS DC4A, SUM(a.DC4W) AS DC4W, + SUM(a.DC4WH) AS DC4WH,AVG(a.DC5V) AS DC5V, AVG(a.DC5A) AS DC5A, SUM(a.DC5W) AS DC5W, SUM(a.DC5WH) AS DC5WH, max(PR) AS PR, + AVG(a.RA1) AS RA1, AVG(a.RA2) AS RA2, AVG(a.RA3) AS RA3, AVG(a.RA4) AS RA4, AVG(a.RA5) AS RA5, avg(DCKW) as DCKW, avg(ACKW) as ACKW, + MAX(a.TODAYKWH) AS TODAYKWH, MAX(a.TOTALKWH) AS TOTALKWH, SUM(a.KWH) AS KWH + FROM solar_master.inverter_history_day a + WHERE powerstationID = @powerstationID and DATE_FORMAT(a.TIMESTAMP, '%Y-%m') = @date1 + GROUP BY DATE_FORMAT(a.TIMESTAMP, '%Y-%m'), a.INVERTERID + ) a + LEFT JOIN " + _siteDB + @".inverter i ON a.INVERTERID = i.InverterId + -- 取日照度 ---------------------- + left join ( + select DATE_FORMAT(a.TIMESTAMP, '%Y-%m') AS reportDate, Irradiance + from sensor_history_month a + where powerstationID = @powerstationID and DATE_FORMAT(a.TIMESTAMP, '%Y-%m') = @date1 and Irradiance <> 0 + )irr on a.reportDate = irr.reportDate ;"; + conn.Execute(sql, new { date1 = _date1.Substring(0, 7), PowerStationID = _powerStationID }); + #endregion month + + conn.Close(); + } + result = true; } - } - - public List proc_station_day(string reportDate) - { - using (MySqlConnection conn = new MySqlConnection(Connection1)) - { - conn.Open(); - string sql = @" -insert into solar_master.power_station_history_day(`PowerStationId`, `TIMESTAMP`, `SITEID`, `SITETYPE`, `TODAYKWH`, `TOTALKWH`, `KWHKWP`, `PR`, `MP`, `SOLARHOUR`, `MONEY`, `TOTALMONEY`, `CARBON`, `TOTALCARBON`) - -SELECT 1 as PowerStationId, FROM_UNIXTIME(s.TIMESTAMP/1000, '%Y-%m-%d %H') AS reportDate, s.SITEID, s.SITETYPE, - s.TodayKWh, s.TotalKWH, s.KWHKWP, s.PR, s.MP, s.SolarHour, 0, 0, 0, 0 - FROM solar_com0002.s02202000101_station s - WHERE DATE_FORMAT(FROM_UNIXTIME(s.TIMESTAMP/1000), '%Y-%m-%d') = '2021-08-04' - ORDER BY TIMESTAMP DESC - LIMIT 1 "; - List ds = conn.Query(sql).AsList(); - conn.Close(); - return ds; + catch (Exception ex) + { + throw ex; } - } - - public List proc_sensor_hour(string reportDate) - { - using (MySqlConnection conn = new MySqlConnection(Connection1)) - { - conn.Open(); - string sql = @" - -INSERT INTO `solar_master`.`sensor_history_hour`(`PowerStationId`, `TIMESTAMP`, `Irradiance`, `Temperature`) - -select a.PowerStationId, a.reportDate, ifnull(b.Irradiance, 0) Irradiance, a.temperature -from -(#溫度 - select 1 as PowerStationId, concat(FROM_UNIXTIME(`TIMESTAMP`/1000, '%Y-%m-%d %H'), ':00:00') AS reportDate, - 0 Irradiance, round(avg(SENSORAVG02),3) temperature - from s02202000101_sensoravg a - where left(FROM_UNIXTIME(a.TIMESTAMP / 1000, '%Y-%m-%d'), 10) = '2021-08-04' - group by FROM_UNIXTIME(a.TIMESTAMP / 1000, '%Y-%m-%d %H') -)a left join ( - #日照 - select 1 as PowerStationId, concat(FROM_UNIXTIME(`TIMESTAMP`/1000, '%Y-%m-%d %H'), ':00:00') AS reportDate, - round(avg(SENSORAVG01),3) Irradiance, round(avg(SENSORAVG02),3) temperature - from s02202000101_sensoravg a - where left(FROM_UNIXTIME(a.TIMESTAMP / 1000, '%Y-%m-%d'), 10) = '2021-08-04' and SENSORAVG01 <> 0 - group by FROM_UNIXTIME(a.TIMESTAMP / 1000, '%Y-%m-%d %H') -)b on a.reportDate = b.reportDate ;"; - List ds = conn.Query(sql).AsList(); - conn.Close(); - return ds; - } - } + return result; + } + } } diff --git a/solarApp/Service/procSensorSvc.cs b/solarApp/Service/procSensorSvc.cs new file mode 100644 index 0000000..b5e8bde --- /dev/null +++ b/solarApp/Service/procSensorSvc.cs @@ -0,0 +1,362 @@ +using System; +using System.Collections.Generic; +using System.Text; +using MySql.Data.MySqlClient; +using Dapper; +using solarApp.Model; +using System.Configuration; +using System.Threading.Tasks; + +namespace solarApp.Service +{ + public class procSensorSvc + { + string Connection1 = ConfigurationManager.ConnectionStrings["mySql"].ConnectionString; + + public string _siteID { get; set; } + public string _siteDB { get; set; } + public string _siteID01 { get; set; } + public string _date1 { get; set; } + public string _date2 { get; set; } + public string _powerStationID { get; set; } + + //public procSensorSvc(string siteDB, string siteID, string date1, string date2) + //{ + // if (!string.IsNullOrEmpty(siteDB)) _siteDB = siteDB; + // if (!string.IsNullOrEmpty(siteID)) _siteID = siteID.Substring(0, 9); + // if (!string.IsNullOrEmpty(siteID)) _siteID01 = siteID; + // if (!string.IsNullOrEmpty(_date1)) _date1 = date1; + // if (!string.IsNullOrEmpty(_date2)) _date2 = date2; + // #region 取得 PowerStationID + // using (MySqlConnection conn = new MySqlConnection(Connection1)) + // { + // conn.Open(); + // string sql = @" select id as PowerStationID from solar_master.power_station where `code` = @siteID"; + // var ds = conn.Query(sql, new { siteID = _siteID }).AsList(); + // _powerStationID = (ds.Count > 0) ? ds[0][0].ToString() : "0"; + // conn.Close(); + // } + // #endregion + + // archiveData(); + //} + + public bool archiveData() { + + bool result = false; + try + { + clearData(); + insert_sensor(); + result = true; + } + catch (Exception ex) + { + throw ex; + } + return result; + } + + /// + /// 清除指定期間、指定電站的 Sensor hour、day、month 資料 + /// + /// + private bool clearData() + { + bool result = false; + try + { + #region 取得 PowerStationID + using (MySqlConnection conn = new MySqlConnection(Connection1)) + { + conn.Open(); + string sql = @" select id as PowerStationID from solar_master.power_station where `code` = @siteID"; + var ds = conn.Query(sql, new { siteID = _siteID }).AsList(); + _powerStationID = (ds.Count > 0) ? ds[0][0].ToString() : "0"; + conn.Close(); + } + #endregion + + using (MySqlConnection conn = new MySqlConnection(Connection1)) + { + conn.Open(); + string sql = @"delete from sensor_history_hour where left(`TIMESTAMP`, 10) between @date1 and @date2 and PowerStationID = @powerStationID; + delete from sensor_history_day where left(`TIMESTAMP`, 10) between @date1 and @date2 and PowerStationID = @powerStationID; + delete from sensor_history_month where left(`TIMESTAMP`, 7) between left(@date1, 7) and left(@date2, 7) and PowerStationID = @powerStationID; + delete from sensoravg_history_hour where left(`TIMESTAMP`, 10) between @date1 and @date2 and PowerStationID = @powerStationID; + delete from sensoravg_history_day where left(`TIMESTAMP`, 10) between @date1 and @date2 and PowerStationID = @powerStationID; + delete from sensoravg_history_month where left(`TIMESTAMP`, 7) between left(@date1, 7) and left(@date2, 7) and PowerStationID = @powerStationID;"; + var ds = conn.Execute(sql, new { date1 = _date1, date2 = _date2, PowerStationID = _powerStationID }); + conn.Close(); + } + result = true; + } + catch (Exception ex) + { + throw ex; + } + return result; + } + + /// + /// Sensor 欄位需要平均時 欄位串接 (s1 + s2) / 2 + /// + /// + /// + string ConcatColumn(List lstData) + { + string ss = string.Empty; string result = string.Empty; + if (lstData.Count > 0) + { + foreach (var item in lstData) + { + ss += (ss == string.Empty) ? item.colname : "+" + item.colname; // 2 個以上欄位需要相加 + } + if (lstData.Count >= 2) result = "(" + ss + ") / " + lstData.Count.ToString(); // 2個以上 需要除以個數 (平均值) + else + result = ss; + } + else result = "0"; + return result; + } + bool insert_sensor() + { + bool result = false; + using (MySqlConnection conn = new MySqlConnection(Connection1)) + { + conn.Open(); + #region 取得 PowerStationID + string sql = @" select id as PowerStationID from solar_master.power_station where `code` = @siteID"; + var ds = conn.Query(sql, new { siteID = _siteID }).AsList(); + _powerStationID = (ds.Count > 0) ? ds[0][0].ToString() : "0"; + + #endregion + // init 關閉 group full column + string ss = @"SET SESSION sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY,',''));"; + conn.Execute(ss); + + #region 獲取 Sensor 類別 + //沒有被刪除、啟用中、狀態正常的 才可列入計算 + sql = @"select a.id, CONCAT( left(UID, 9) ,'01') SiteID, a.`name` sensorName, type, DBName, TableName, colname + from " + _siteDB + @".device a + where left(UID, 11) = @siteID and deleted = 0 and enabled = 1 and `status` = 1 "; + List ds_sensor = conn.Query(sql, new { siteID = _siteID01 }).AsList(); + + //欄位處理 + // { "Type":[ + // { "Name":"日照計","EName":"PYR"}, + // { "Name":"模組溫度計","EName":"MTR"}, + // { "Name":"環境溫度計","EName":"ETR"}, + // { "Name":"環境濕度計","EName":"EMM"}, + // { "Name":"風速計","EName":"VAN"}, + // { "Name":" 電表","EName":"PWR"}]} + var irrlst = ds_sensor.FindAll(x => x.type.Contains("PYR")); + var modelTemplst = ds_sensor.FindAll(x => x.type.Contains("MTR")); + var envTemplst = ds_sensor.FindAll(x => x.type.Contains("ETR")); + var humlst = ds_sensor.FindAll(x => x.type.Contains("EMM")); + var windlst = ds_sensor.FindAll(x => x.type.Contains("VAN")); + var dustlst = ds_sensor.FindAll(x => x.type.Contains("DST")); //需要新增於DB + //var meterlst = ds_sensor.FindAll(x => x.type.Contains("PWR")); 電錶暫不處理 + string irrCol = string.Empty; string modelTempCol = string.Empty; string evnTempCol = string.Empty; string humCol = string.Empty; string windCol = string.Empty; string meterCol = string.Empty; string dustCol = string.Empty; + irrCol = ConcatColumn(irrlst);//日照計 + modelTempCol = ConcatColumn(modelTemplst); + evnTempCol = ConcatColumn(envTemplst); + humCol = ConcatColumn(humlst); + windCol = ConcatColumn(windlst); + dustCol = ConcatColumn(dustlst); + + string irrNot0 = string.Empty; // and 日照1 <> 0 and 日照2 <> 0 + # region 日照計需要過濾 0 + if (irrlst.Count > 0) + { + foreach (var item in irrlst) + irrNot0 += " and " + item.colname + " <> 0 "; // and S1 <> 0 and S2 <> 0 + } + #endregion + + //電表 + + #endregion 獲取 Sensor 類別 + + try + { + + #region hour + sql = @"insert into sensor_history_hour( `PowerStationId`, `TIMESTAMP`, `Irradiance`, `Temperature`) + select a.powerstationID, a.reportdate, ifnull(b.irrAvg, 0) irrAvg, a.modelTempAvg from + ( + select @powerStationID powerstationID, FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H:%i') reportdate, round(avg(" + modelTempCol + @"), 2) modelTempAvg, + round(avg(" + evnTempCol + @"), 2) envTempAvg, round(avg(" + humCol + @"), 2) humidityAvg, round(avg(" + windCol + @"), 2) windAvg, + round(avg(" + dustCol + @"), 2) dustAvg + from " + _siteDB + ".s" + _siteID01 + @"_sensorAvg + where FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d') between @date1 and @date2 + group by FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H') + ) a left join + ( + select concat(FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H'), ':00') reportdate, round(avg(" + irrCol + @"), 2) irrAvg + from " + _siteDB + ".s" + _siteID01 + @"_sensorAvg + where FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d') between @date1 and @date2 " + irrNot0 + @" #需要過濾 0 的數值 + group by FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H') + )b on a.reportdate = b.reportdate"; + //MySqlCommand cmd = new MySqlCommand(sql, conn); + //cmd.Parameters() + //cmd.ExecuteNonQuery(); + conn.Execute(sql, new { powerStationID = _powerStationID, date1 = _date1, date2 = _date2 }); + #endregion hour + + #region day + sql = @"insert into sensor_history_day( `PowerStationId`, `TIMESTAMP`, `Irradiance`, `Temperature`) + select a.powerstationID, a.reportdate, ifnull(b.Irradiance, 0) irrAvg, a.modelTempAvg from + ( + select powerStationID , concat(DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d'), ' 00:00') reportdate, round(avg(Temperature), 2) modelTempAvg + from solar_master.sensor_history_hour + where powerstationID = @powerstationID and DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') between @date1 and @date2 + group by DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') + ) a left join + ( + select concat(DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d'), ' 00:00') reportdate, round(avg(Irradiance), 2) Irradiance + from solar_master.sensor_history_hour + where powerstationID = @powerstationID + and DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') between @date1 and @date2 and Irradiance <> 0 #需要過濾 0 的數值 + group by DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') + )b on a.reportdate = b.reportdate"; + conn.Execute(sql, new { powerStationID = _powerStationID, date1 = _date1, date2 = _date2 }); + #endregion day + + #region month 如果跨月 會有2筆資料 + + sql = @" + delete from sensor_history_month where left(`TIMESTAMP`, 7) between left(@date1, 7) and left(@date2, 7) and PowerStationID = @powerStationID; + + insert into sensor_history_month( `PowerStationId`, `TIMESTAMP`, `Irradiance`, `Temperature`) + select a.powerstationID, a.reportdate, ifnull(b.Irradiance, 0) irrAvg, a.modelTempAvg from + ( + select powerStationID , concat(DATE_FORMAT(`TIMESTAMP`,'%Y-%m'), '-01 00:00') reportdate, round(avg(Temperature), 2) modelTempAvg + from solar_master.sensor_history_day + where powerstationID = @powerstationID and DATE_FORMAT(`TIMESTAMP`,'%Y-%m') between @date1 and @date2 + group by DATE_FORMAT(`TIMESTAMP`,'%Y-%m') + ) a left join + ( + select concat(DATE_FORMAT(`TIMESTAMP`,'%Y-%m'), '-01 00:00') reportdate, round(avg(Irradiance), 2) Irradiance + from solar_master.sensor_history_day + where powerstationID = @powerstationID + and DATE_FORMAT(`TIMESTAMP`,'%Y-%m') between @date1 and @date2 and Irradiance <> 0 #需要過濾 0 的數值 + group by DATE_FORMAT(`TIMESTAMP`,'%Y-%m') + )b on a.reportdate = b.reportdate"; + conn.Execute(sql, new { powerStationID = _powerStationID, date1 = _date1.Substring(0,7), date2 = _date2.Substring(0, 7) }); + #endregion month + + #region Sensor 1~50 hour + sql = @" + INSERT INTO solar_master.sensoravg_history_hour(PowerStationId, `TIMESTAMP`, SENSORAVG01, SENSORAVG02, SENSORAVG03, SENSORAVG04, SENSORAVG05, SENSORAVG06, SENSORAVG07, SENSORAVG08, SENSORAVG09, SENSORAVG10, SENSORAVG11, SENSORAVG12, SENSORAVG13, SENSORAVG14, SENSORAVG15, SENSORAVG16, SENSORAVG17, SENSORAVG18, SENSORAVG19, SENSORAVG20, SENSORAVG21, SENSORAVG22, SENSORAVG23, SENSORAVG24, SENSORAVG25, SENSORAVG26, SENSORAVG27, SENSORAVG28, SENSORAVG29, SENSORAVG30, SENSORAVG31, SENSORAVG32, SENSORAVG33, SENSORAVG34, SENSORAVG35, SENSORAVG36, SENSORAVG37, SENSORAVG38, SENSORAVG39, SENSORAVG40, SENSORAVG41, SENSORAVG42, SENSORAVG43, SENSORAVG44, SENSORAVG45, SENSORAVG46, SENSORAVG47, SENSORAVG48, SENSORAVG49, SENSORAVG50) + select @powerStationID powerstationID, FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H:%i') reportdate, avg(sensorAvg01), avg(sensorAvg02), + avg(sensorAvg03), avg(sensorAvg04), avg(sensorAvg05), avg(sensorAvg06), avg(sensorAvg07), avg(sensorAvg08), avg(sensorAvg09), + avg(sensorAvg10), avg(sensorAvg11), avg(sensorAvg12), avg(sensorAvg13), avg(sensorAvg14), avg(sensorAvg15), avg(sensorAvg16), + avg(sensorAvg17), avg(sensorAvg18), avg(sensorAvg19), avg(sensorAvg20), avg(sensorAvg21), avg(sensorAvg22), avg(sensorAvg23), + avg(sensorAvg24), avg(sensorAvg25), avg(sensorAvg26), avg(sensorAvg27), avg(sensorAvg28), avg(sensorAvg29), avg(sensorAvg30), + avg(sensorAvg31), avg(sensorAvg32), avg(sensorAvg33), avg(sensorAvg34), avg(sensorAvg35), avg(sensorAvg36), avg(sensorAvg37), + avg(sensorAvg38), avg(sensorAvg39), avg(sensorAvg40), avg(sensorAvg41), avg(sensorAvg42), avg(sensorAvg43), avg(sensorAvg44), + avg(sensorAvg45), avg(sensorAvg46), avg(sensorAvg47), avg(sensorAvg48), avg(sensorAvg49), avg(sensorAvg50) + from " + _siteDB + ".s" + _siteID01 + @"_sensorAvg + where FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d') between @date1 and @date2 + group by FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H');"; + conn.Execute(sql, new { powerStationID = _powerStationID, date1 = _date1, date2 = _date2 }); + + //日照計 update + sql = string.Empty; + foreach (var item in irrlst) + { + sql += @" update sensoravg_history_hour a join + ( + select @powerstationID powerstationID, concat(FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H'), ':00') reportdate, round(avg(" + item.colname + @"), 2) irrAvg + from " + _siteDB + ".s" + _siteID01 + @"_sensorAvg + where FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d') between @date1 and @date2 and " + item.colname + @" <> 0 #需要過濾 0 的數值 + group by FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H') + )b on a.powerStationID = b.powerStationID and DATE_FORMAT(a.`TIMESTAMP`,'%Y-%m-%d %H:%i') = b.reportdate + set a." + item.colname + " = b.irrAvg;"; + } + if (!string.IsNullOrEmpty(sql)) conn.Execute(sql, new { powerStationID = _powerStationID, date1 = _date1, date2 = _date2 }); + + #endregion + + #region Sensor 1~50 day + sql = @" + INSERT INTO solar_master.sensoravg_history_day(PowerStationId, `TIMESTAMP`, SENSORAVG01, SENSORAVG02, SENSORAVG03, SENSORAVG04, SENSORAVG05, SENSORAVG06, SENSORAVG07, SENSORAVG08, SENSORAVG09, SENSORAVG10, SENSORAVG11, SENSORAVG12, SENSORAVG13, SENSORAVG14, SENSORAVG15, SENSORAVG16, SENSORAVG17, SENSORAVG18, SENSORAVG19, SENSORAVG20, SENSORAVG21, SENSORAVG22, SENSORAVG23, SENSORAVG24, SENSORAVG25, SENSORAVG26, SENSORAVG27, SENSORAVG28, SENSORAVG29, SENSORAVG30, SENSORAVG31, SENSORAVG32, SENSORAVG33, SENSORAVG34, SENSORAVG35, SENSORAVG36, SENSORAVG37, SENSORAVG38, SENSORAVG39, SENSORAVG40, SENSORAVG41, SENSORAVG42, SENSORAVG43, SENSORAVG44, SENSORAVG45, SENSORAVG46, SENSORAVG47, SENSORAVG48, SENSORAVG49, SENSORAVG50) + select @powerStationID powerstationID, DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') reportdate, avg(sensorAvg01), avg(sensorAvg02), + avg(sensorAvg03), avg(sensorAvg04), avg(sensorAvg05), avg(sensorAvg06), avg(sensorAvg07), avg(sensorAvg08), avg(sensorAvg09), + avg(sensorAvg10), avg(sensorAvg11), avg(sensorAvg12), avg(sensorAvg13), avg(sensorAvg14), avg(sensorAvg15), avg(sensorAvg16), + avg(sensorAvg17), avg(sensorAvg18), avg(sensorAvg19), avg(sensorAvg20), avg(sensorAvg21), avg(sensorAvg22), avg(sensorAvg23), + avg(sensorAvg24), avg(sensorAvg25), avg(sensorAvg26), avg(sensorAvg27), avg(sensorAvg28), avg(sensorAvg29), avg(sensorAvg30), + avg(sensorAvg31), avg(sensorAvg32), avg(sensorAvg33), avg(sensorAvg34), avg(sensorAvg35), avg(sensorAvg36), avg(sensorAvg37), + avg(sensorAvg38), avg(sensorAvg39), avg(sensorAvg40), avg(sensorAvg41), avg(sensorAvg42), avg(sensorAvg43), avg(sensorAvg44), + avg(sensorAvg45), avg(sensorAvg46), avg(sensorAvg47), avg(sensorAvg48), avg(sensorAvg49), avg(sensorAvg50) + from solar_master.sensoravg_history_hour + where powerStationID = @powerStationID and DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') between @date1 and @date2 + group by DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d');"; + conn.Execute(sql, new { powerStationID = _powerStationID, date1 = _date1, date2 = _date2 }); + + //日照計 update + sql = string.Empty; + foreach (var item in irrlst) + { + sql += @" update solar_master.sensoravg_history_day a join + ( + select @powerstationID powerstationID, concat(DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d'), ' 00:00') reportdate, round(avg(" + item.colname + @"), 2) irrAvg + from solar_master.sensoravg_history_hour + where powerStationID = @powerStationID and DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') between @date1 and @date2 and " + item.colname + @" <> 0 #需要過濾 0 的數值 + group by DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') + )b on a.powerStationID = b.powerStationID and DATE_FORMAT(a.`TIMESTAMP`,'%Y-%m-%d') = b.reportdate + set a." + item.colname + " = b.irrAvg;"; + } + if (!string.IsNullOrEmpty(sql)) conn.Execute(sql, new { powerStationID = _powerStationID, date1 = _date1, date2 = _date2 }); + + #endregion + + #region Sensor 1~50 month + sql = @" + delete from sensoravg_history_month where left(`TIMESTAMP`, 7) between left(@date1, 7) and left(@date2, 7) and PowerStationID = @powerStationID; + + INSERT INTO solar_master.sensoravg_history_month (PowerStationId, `TIMESTAMP`, SENSORAVG01, SENSORAVG02, SENSORAVG03, SENSORAVG04, SENSORAVG05, SENSORAVG06, SENSORAVG07, SENSORAVG08, SENSORAVG09, SENSORAVG10, SENSORAVG11, SENSORAVG12, SENSORAVG13, SENSORAVG14, SENSORAVG15, SENSORAVG16, SENSORAVG17, SENSORAVG18, SENSORAVG19, SENSORAVG20, SENSORAVG21, SENSORAVG22, SENSORAVG23, SENSORAVG24, SENSORAVG25, SENSORAVG26, SENSORAVG27, SENSORAVG28, SENSORAVG29, SENSORAVG30, SENSORAVG31, SENSORAVG32, SENSORAVG33, SENSORAVG34, SENSORAVG35, SENSORAVG36, SENSORAVG37, SENSORAVG38, SENSORAVG39, SENSORAVG40, SENSORAVG41, SENSORAVG42, SENSORAVG43, SENSORAVG44, SENSORAVG45, SENSORAVG46, SENSORAVG47, SENSORAVG48, SENSORAVG49, SENSORAVG50) + select @powerStationID powerstationID, concat(DATE_FORMAT(`TIMESTAMP`,'%Y-%m'), '-01 00:00') reportdate, avg(sensorAvg01), avg(sensorAvg02), + avg(sensorAvg03), avg(sensorAvg04), avg(sensorAvg05), avg(sensorAvg06), avg(sensorAvg07), avg(sensorAvg08), avg(sensorAvg09), + avg(sensorAvg10), avg(sensorAvg11), avg(sensorAvg12), avg(sensorAvg13), avg(sensorAvg14), avg(sensorAvg15), avg(sensorAvg16), + avg(sensorAvg17), avg(sensorAvg18), avg(sensorAvg19), avg(sensorAvg20), avg(sensorAvg21), avg(sensorAvg22), avg(sensorAvg23), + avg(sensorAvg24), avg(sensorAvg25), avg(sensorAvg26), avg(sensorAvg27), avg(sensorAvg28), avg(sensorAvg29), avg(sensorAvg30), + avg(sensorAvg31), avg(sensorAvg32), avg(sensorAvg33), avg(sensorAvg34), avg(sensorAvg35), avg(sensorAvg36), avg(sensorAvg37), + avg(sensorAvg38), avg(sensorAvg39), avg(sensorAvg40), avg(sensorAvg41), avg(sensorAvg42), avg(sensorAvg43), avg(sensorAvg44), + avg(sensorAvg45), avg(sensorAvg46), avg(sensorAvg47), avg(sensorAvg48), avg(sensorAvg49), avg(sensorAvg50) + from solar_master.sensoravg_history_day + where powerStationID = @powerStationID and DATE_FORMAT(`TIMESTAMP`,'%Y-%m') between @date1 and @date2 + group by DATE_FORMAT(`TIMESTAMP`,'%Y-%m');"; + conn.Execute(sql, new { powerStationID = _powerStationID, date1 = _date1.Substring(0, 7), date2 = _date2.Substring(0, 7) }); + + //日照計 update + sql = string.Empty; + foreach (var item in irrlst) + { + sql += @" update solar_master.sensoravg_history_month a join + ( + select @powerstationID powerstationID, concat( DATE_FORMAT(`TIMESTAMP`,'%Y-%m'), '-01 00:00') reportdate, round(avg(" + item.colname + @"), 2) irrAvg + from solar_master.sensoravg_history_day + where powerStationID = @powerStationID and DATE_FORMAT(`TIMESTAMP`,'%Y-%m') between @date1 and @date2 and " + item.colname + @" <> 0 #需要過濾 0 的數值 + group by DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') + )b on a.powerStationID = b.powerStationID and DATE_FORMAT(a.`TIMESTAMP`,'%Y-%m') = b.reportdate + set a." + item.colname + " = b.irrAvg;"; + } + if (!string.IsNullOrEmpty(sql)) conn.Execute(sql, new { powerStationID = _powerStationID, date1 = _date1.Substring(0, 7), date2 = _date2.Substring(0, 7) }); + + #endregion + + conn.Close(); + result = true; + } + catch (Exception ex) + { + throw ex; + } + } + return result; + } + + } +} diff --git a/solarApp/Service/procStationSvc.cs b/solarApp/Service/procStationSvc.cs new file mode 100644 index 0000000..5b60bd6 --- /dev/null +++ b/solarApp/Service/procStationSvc.cs @@ -0,0 +1,190 @@ +using System; +using System.Collections.Generic; +using System.Text; +using MySql.Data.MySqlClient; +using Dapper; +using solarApp.Model; +using System.Configuration; + +namespace solarApp.Service +{ + public class procStationSvc + { + string Connection1 = ConfigurationManager.ConnectionStrings["mySql"].ConnectionString; + + public string _siteID { get; set; } + public string _siteDB { get; set; } + public string _siteID01 { get; set; } + public string _date1 { get; set; } + public string _date2 { get; set; } + public string _powerStationID { get; set; } + + + public bool clearData() + { + bool result = false; + try + { + #region 取得 PowerStationID + using (MySqlConnection conn = new MySqlConnection(Connection1)) + { + conn.Open(); + string sql = @" select id as PowerStationID from solar_master.power_station where `code` = @siteID"; + var ds = conn.Query(sql, new { siteID = _siteID }).AsList(); + _powerStationID = (ds.Count > 0) ? ds[0][0].ToString() : "0"; + conn.Close(); + } + #endregion + + using (MySqlConnection conn = new MySqlConnection(Connection1)) + { + conn.Open(); + string sql = @" + delete from power_station_history_hour where powerstationID = @powerStationID and DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') = @date1; + delete from power_station_history_day where powerstationID = @powerStationID and DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') = @date1; + delete from power_station_history_month where powerstationID = @powerStationID and left(`TIMESTAMP`, 7) = left(@date1, 7) ;"; + var ds = conn.Execute(sql, new { date1 = _date1, powerStationID = _powerStationID }); + conn.Close(); + } + result = true; + } + catch (Exception ex) + { + throw ex; + } + return result; + } + + public bool archiveData() + { + bool result = false; + try + { + clearData(); + insert_station(); + result = true; + } + catch (Exception ex) + { + throw ex; + } + return result; + } + + bool insert_station() + { + bool result = false; + try + { + using (MySqlConnection conn = new MySqlConnection(Connection1)) + { + conn.Open(); + string ss = @"SET SESSION sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY,',''));"; + conn.Execute(ss); + + string sql = @" select id as PowerStationID from solar_master.power_station where `code` = @siteID"; + var ds = conn.Query(sql, new { siteID = _siteID }).AsList(); + _powerStationID = (ds.Count > 0) ? ds[0][0].ToString() : "0"; + + #region hour -- solar_type 計費類別:0 自建、1 租建、2自建 ------------ 1 租建計算方式 後再再補上 + + //select b.id PowerStationId, a.reportdate, b.siteID, a.siteType, a.KWH, a.TODAYKWH, a.TOTALKWH, a.KWHKWP, a.PR, a.MP, a.SOLARHOUR, + // (KWH * PowerRate) MONEY, (a.TODAYKWH * PowerRate) TODAYMONEY, (a.TOTALKWH * PowerRate) TOTALMONEY, + // (KWH * CarbonRate) CARBON, (a.TODAYKWH * CarbonRate) TODAYCARBON, round((a.TOTALKWH * CarbonRate), 4) TOTALCARBON + // from + // ( + // select siteid, siteType, concat(FROM_UNIXTIME(`TIMESTAMP`/ 1000, '%Y-%m-%d %H'), ':00:00') reportdate, + // round((sum(KWH)), 4) KWH, round((max(TODAYKWH)), 4) TODAYKWH, round((max(TOTALKWH)), 4) TOTALKWH, round((max(KWHKWP)), 4) KWHKWP, + // round((max(PR)), 4) PR, 0 as MP, round((max(SOLARHOUR)), 4) SOLARHOUR + + // from " + _siteDB + ".s" + _siteID01 + @"_station + + // where FROM_UNIXTIME(`TIMESTAMP`/ 1000, '%Y-%m-%d') = @date1 + + // group by siteid, FROM_UNIXTIME(`TIMESTAMP`/ 1000, '%Y-%m-%d %H') + // )a join + // ( + // select id, `code` siteID, PowerRate, (select `value` from solar_master.`variable` where `name` = 'CarbonRate') as CarbonRate, SolarType + // from solar_master.power_station + // where `code` = @siteID + // ) b on a.siteID = b.siteID + + sql = @"insert into solar_master.power_station_history_hour(PowerStationId, `TIMESTAMP`, SITEID, SITETYPE, KWH, TODAYKWH, TOTALKWH, KWHKWP, PR, MP, SOLARHOUR, MONEY, TODAYMONEY, TOTALMONEY, CARBON, TODAYCARBON, TOTALCARBON) + + select b.id PowerStationId, concat(FROM_UNIXTIME(`TIMESTAMP`/1000, '%Y-%m-%d %H'), ':00:00') reportdate, a.siteid, a.siteType, + a.KWH, TODAYKWH, TOTALKWH, KWHKWP , PR, MP, a.SOLARHOUR , + (a.KWH * PowerRate) MONEY, (a.TODAYKWH * PowerRate) TODAYMONEY, (a.TOTALKWH * PowerRate) TOTALMONEY, + (a.KWH * CarbonRate) CARBON, (a.TODAYKWH * CarbonRate) TODAYCARBON, round((a.TOTALKWH * CarbonRate), 3) TOTALCARBON + from " + _siteDB + ".s" + _siteID01 + @"_station a join + (select id, `code` siteID, PowerRate, (select `value` from solar_master.`variable` where `name` = 'CarbonRate') as CarbonRate, SolarType + from solar_master.power_station where `code` = @siteID + ) b on a.siteID = b.siteID + where FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d') = @date1 and a.SITEID = @siteID "; + int ct = conn.Execute(sql, new { date1 = _date1, siteID = _siteID }); + + #endregion hour + + #region day + sql = @" INSERT INTO solar_master.power_station_history_day( PowerStationId, `TIMESTAMP`, SITEID, SITETYPE, TODAYKWH, TOTALKWH, KWHKWP, + PR, MP, SOLARHOUR, MONEY, TOTALMONEY, CARBON, TOTALCARBON) + + select b.id PowerStationId, a.reportdate, b.siteID, a.siteType, a.TODAYKWH, a.TOTALKWH, a.KWHKWP, a.PR, a.MP, a.SOLARHOUR, + (KWH * PowerRate) MONEY, (a.TOTALKWH * PowerRate) TOTALMONEY, + (KWH * CarbonRate) CARBON, round((a.TOTALKWH * CarbonRate), 4) TOTALCARBON + from + ( + select concat(DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d'), ' 00:00:00') reportdate, siteid, siteType, round((sum(KWH)), 4) KWH, + round((max(TODAYKWH)), 4) TODAYKWH, round((max(TOTALKWH)), 4) TOTALKWH, round((max(KWHKWP)), 4) KWHKWP, + round((max(PR)), 4) PR, round((max(MP)), 4) as MP, round((max(SOLARHOUR)), 4) SOLARHOUR + from solar_master.power_station_history_hour a + where SITEID = @siteID and DATE_FORMAT(a.`TIMESTAMP`,'%Y-%m-%d') = @date1 + group by a.siteid, DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') + )a join + ( + select id, `code` siteID, PowerRate, (select `value` from solar_master.`variable` where `name` = 'CarbonRate') as CarbonRate, SolarType + from solar_master.power_station + where `code` = @siteID + ) b on a.siteID = b.siteID "; + conn.Execute(sql, new { date1 = _date1, siteID = _siteID }); + #endregion day + + #region month + sql = @" + delete from power_station_history_month where powerstationID = @powerStationID and left(`TIMESTAMP`, 7) = left(@date1, 7) ; + + INSERT INTO solar_master.power_station_history_month(PowerStationId, `TIMESTAMP`, SITEID, SITETYPE, TOTALKWH, KWHKWP, + PR, MP, SOLARHOUR, MONEY, TOTALMONEY, CARBON, TOTALCARBON) + + select b.id PowerStationId, a.reportdate, b.siteID, a.siteType, a.TOTALKWH, a.KWHKWP, a.PR, a.MP, a.SOLARHOUR, + (KWH * PowerRate) MONEY, (a.TOTALKWH * PowerRate) TOTALMONEY, + (KWH * CarbonRate) CARBON, round((a.TOTALKWH * CarbonRate), 4) TOTALCARBON + from + ( + select concat(DATE_FORMAT(`TIMESTAMP`,'%Y-%m'), '-01 00:00:00') reportdate, siteid, siteType, round((sum(KWH)), 4) KWH, + round((max(TODAYKWH)), 4) TODAYKWH, round((max(TOTALKWH)), 4) TOTALKWH, round((max(KWHKWP)), 4) KWHKWP, + round((max(PR)), 4) PR, round((max(MP)), 4) as MP, round((max(SOLARHOUR)), 4) SOLARHOUR + from solar_master.power_station_history_hour a + where SITEID = @siteID and DATE_FORMAT(a.`TIMESTAMP`, '%Y-%m') = @date1 + group by a.siteid, DATE_FORMAT(`TIMESTAMP`, '%Y-%m') + )a join + ( + select id, `code` siteID, PowerRate, (select `value` from solar_master.`variable` where `name` = 'CarbonRate') as CarbonRate, SolarType + from solar_master.power_station + where `code` = @siteID + ) b on a.siteID = b.siteID "; + conn.Execute(sql, new { date1 = _date1.Substring(0, 7), siteID = _siteID, powerStationID = _powerStationID }); + + #endregion month + + conn.Close(); + } + result = true; + } + catch (Exception ex) + { + throw ex; + } + return result; + } + } +} diff --git a/solarApp/fmArchive.Designer.cs b/solarApp/fmArchive.Designer.cs index 792af38..c914109 100644 --- a/solarApp/fmArchive.Designer.cs +++ b/solarApp/fmArchive.Designer.cs @@ -31,16 +31,29 @@ namespace solarApp { this.tabControl1 = new System.Windows.Forms.TabControl(); this.tabPage1 = new System.Windows.Forms.TabPage(); - this.tabPage2 = new System.Windows.Forms.TabPage(); this.splitContainer1 = new System.Windows.Forms.SplitContainer(); - this.panel1 = new System.Windows.Forms.Panel(); + this.bt_clear_station = new System.Windows.Forms.Button(); + this.bt_clear_inv = new System.Windows.Forms.Button(); + this.fp_site = new System.Windows.Forms.FlowLayoutPanel(); + this.dtSelect = new System.Windows.Forms.DateTimePicker(); + this.bt_site = new System.Windows.Forms.Button(); + this.bt_Inv = new System.Windows.Forms.Button(); + this.bt_Sensor = new System.Windows.Forms.Button(); this.dataGridView1 = new System.Windows.Forms.DataGridView(); + this.panel1 = new System.Windows.Forms.Panel(); + this.btVerifyData = new System.Windows.Forms.Button(); + this.lbSiteDB_sensor = new System.Windows.Forms.Label(); + this.lbSiteID_sensor = new System.Windows.Forms.Label(); + this.lbSiteName_sensor = new System.Windows.Forms.Label(); + this.tabPage2 = new System.Windows.Forms.TabPage(); this.tabControl1.SuspendLayout(); this.tabPage1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit(); + this.splitContainer1.Panel1.SuspendLayout(); this.splitContainer1.Panel2.SuspendLayout(); this.splitContainer1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit(); + this.panel1.SuspendLayout(); this.SuspendLayout(); // // tabControl1 @@ -48,6 +61,7 @@ namespace solarApp this.tabControl1.Appearance = System.Windows.Forms.TabAppearance.Buttons; this.tabControl1.Controls.Add(this.tabPage1); this.tabControl1.Controls.Add(this.tabPage2); + this.tabControl1.Dock = System.Windows.Forms.DockStyle.Fill; this.tabControl1.Location = new System.Drawing.Point(0, 0); this.tabControl1.Name = "tabControl1"; this.tabControl1.SelectedIndex = 0; @@ -65,16 +79,6 @@ namespace solarApp this.tabPage1.Text = "tabPage1"; this.tabPage1.UseVisualStyleBackColor = true; // - // tabPage2 - // - this.tabPage2.Location = new System.Drawing.Point(4, 31); - this.tabPage2.Name = "tabPage2"; - this.tabPage2.Padding = new System.Windows.Forms.Padding(3); - this.tabPage2.Size = new System.Drawing.Size(1774, 918); - this.tabPage2.TabIndex = 1; - this.tabPage2.Text = "tabPage2"; - this.tabPage2.UseVisualStyleBackColor = true; - // // splitContainer1 // this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill; @@ -84,6 +88,13 @@ namespace solarApp // splitContainer1.Panel1 // this.splitContainer1.Panel1.BackColor = System.Drawing.SystemColors.ActiveCaption; + this.splitContainer1.Panel1.Controls.Add(this.bt_clear_station); + this.splitContainer1.Panel1.Controls.Add(this.bt_clear_inv); + this.splitContainer1.Panel1.Controls.Add(this.fp_site); + this.splitContainer1.Panel1.Controls.Add(this.dtSelect); + this.splitContainer1.Panel1.Controls.Add(this.bt_site); + this.splitContainer1.Panel1.Controls.Add(this.bt_Inv); + this.splitContainer1.Panel1.Controls.Add(this.bt_Sensor); // // splitContainer1.Panel2 // @@ -94,13 +105,78 @@ namespace solarApp this.splitContainer1.SplitterWidth = 10; this.splitContainer1.TabIndex = 0; // - // panel1 + // bt_clear_station // - this.panel1.Dock = System.Windows.Forms.DockStyle.Top; - this.panel1.Location = new System.Drawing.Point(0, 0); - this.panel1.Name = "panel1"; - this.panel1.Size = new System.Drawing.Size(1458, 125); - this.panel1.TabIndex = 0; + this.bt_clear_station.Enabled = false; + this.bt_clear_station.Font = new System.Drawing.Font("Microsoft JhengHei UI", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); + this.bt_clear_station.Location = new System.Drawing.Point(3, 418); + this.bt_clear_station.Name = "bt_clear_station"; + this.bt_clear_station.Size = new System.Drawing.Size(135, 44); + this.bt_clear_station.TabIndex = 6; + this.bt_clear_station.Text = "clear data"; + this.bt_clear_station.UseVisualStyleBackColor = true; + this.bt_clear_station.Click += new System.EventHandler(this.bt_clear_station_Click); + // + // bt_clear_inv + // + this.bt_clear_inv.Enabled = false; + this.bt_clear_inv.Font = new System.Drawing.Font("Microsoft JhengHei UI", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); + this.bt_clear_inv.Location = new System.Drawing.Point(5, 335); + this.bt_clear_inv.Name = "bt_clear_inv"; + this.bt_clear_inv.Size = new System.Drawing.Size(135, 44); + this.bt_clear_inv.TabIndex = 5; + this.bt_clear_inv.Text = "clear data"; + this.bt_clear_inv.UseVisualStyleBackColor = true; + this.bt_clear_inv.Click += new System.EventHandler(this.bt_clear_inv_Click); + // + // fp_site + // + this.fp_site.Dock = System.Windows.Forms.DockStyle.Top; + this.fp_site.Location = new System.Drawing.Point(0, 0); + this.fp_site.Name = "fp_site"; + this.fp_site.Size = new System.Drawing.Size(300, 150); + this.fp_site.TabIndex = 4; + // + // dtSelect + // + this.dtSelect.Location = new System.Drawing.Point(37, 156); + this.dtSelect.Name = "dtSelect"; + this.dtSelect.Size = new System.Drawing.Size(204, 27); + this.dtSelect.TabIndex = 3; + this.dtSelect.ValueChanged += new System.EventHandler(this.dtSelect_ValueChanged); + // + // bt_site + // + this.bt_site.Font = new System.Drawing.Font("Microsoft JhengHei UI", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); + this.bt_site.Location = new System.Drawing.Point(152, 418); + this.bt_site.Name = "bt_site"; + this.bt_site.Size = new System.Drawing.Size(135, 44); + this.bt_site.TabIndex = 2; + this.bt_site.Text = "Site 歸檔"; + this.bt_site.UseVisualStyleBackColor = true; + this.bt_site.Click += new System.EventHandler(this.bt_site_Click); + // + // bt_Inv + // + this.bt_Inv.Font = new System.Drawing.Font("Microsoft JhengHei UI", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); + this.bt_Inv.Location = new System.Drawing.Point(152, 335); + this.bt_Inv.Name = "bt_Inv"; + this.bt_Inv.Size = new System.Drawing.Size(135, 44); + this.bt_Inv.TabIndex = 1; + this.bt_Inv.Text = "Inv 歸檔"; + this.bt_Inv.UseVisualStyleBackColor = true; + this.bt_Inv.Click += new System.EventHandler(this.bt_Inv_Click); + // + // bt_Sensor + // + this.bt_Sensor.Font = new System.Drawing.Font("Microsoft JhengHei UI", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); + this.bt_Sensor.Location = new System.Drawing.Point(152, 256); + this.bt_Sensor.Name = "bt_Sensor"; + this.bt_Sensor.Size = new System.Drawing.Size(135, 44); + this.bt_Sensor.TabIndex = 0; + this.bt_Sensor.Text = "Sensor 歸檔"; + this.bt_Sensor.UseVisualStyleBackColor = true; + this.bt_Sensor.Click += new System.EventHandler(this.bt_Sensor_Click); // // dataGridView1 // @@ -113,6 +189,73 @@ namespace solarApp this.dataGridView1.Size = new System.Drawing.Size(1458, 787); this.dataGridView1.TabIndex = 1; // + // panel1 + // + this.panel1.BackColor = System.Drawing.SystemColors.GradientActiveCaption; + this.panel1.Controls.Add(this.btVerifyData); + this.panel1.Controls.Add(this.lbSiteDB_sensor); + this.panel1.Controls.Add(this.lbSiteID_sensor); + this.panel1.Controls.Add(this.lbSiteName_sensor); + this.panel1.Dock = System.Windows.Forms.DockStyle.Top; + this.panel1.Location = new System.Drawing.Point(0, 0); + this.panel1.Name = "panel1"; + this.panel1.Size = new System.Drawing.Size(1458, 125); + this.panel1.TabIndex = 0; + // + // btVerifyData + // + this.btVerifyData.Font = new System.Drawing.Font("Microsoft JhengHei UI", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); + this.btVerifyData.Location = new System.Drawing.Point(635, 7); + this.btVerifyData.Name = "btVerifyData"; + this.btVerifyData.Size = new System.Drawing.Size(158, 44); + this.btVerifyData.TabIndex = 12; + this.btVerifyData.Text = "檢核結果"; + this.btVerifyData.UseVisualStyleBackColor = true; + this.btVerifyData.Click += new System.EventHandler(this.btVerifyData_Click); + // + // lbSiteDB_sensor + // + this.lbSiteDB_sensor.AutoSize = true; + this.lbSiteDB_sensor.Font = new System.Drawing.Font("Microsoft JhengHei UI", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); + this.lbSiteDB_sensor.Location = new System.Drawing.Point(41, 19); + this.lbSiteDB_sensor.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0); + this.lbSiteDB_sensor.Name = "lbSiteDB_sensor"; + this.lbSiteDB_sensor.Size = new System.Drawing.Size(79, 24); + this.lbSiteDB_sensor.TabIndex = 11; + this.lbSiteDB_sensor.Text = "Site_DB"; + // + // lbSiteID_sensor + // + this.lbSiteID_sensor.AutoSize = true; + this.lbSiteID_sensor.Font = new System.Drawing.Font("Microsoft JhengHei UI", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); + this.lbSiteID_sensor.Location = new System.Drawing.Point(200, 19); + this.lbSiteID_sensor.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0); + this.lbSiteID_sensor.Name = "lbSiteID_sensor"; + this.lbSiteID_sensor.Size = new System.Drawing.Size(72, 24); + this.lbSiteID_sensor.TabIndex = 10; + this.lbSiteID_sensor.Text = "Site_ID"; + // + // lbSiteName_sensor + // + this.lbSiteName_sensor.AutoSize = true; + this.lbSiteName_sensor.Font = new System.Drawing.Font("Microsoft JhengHei UI", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); + this.lbSiteName_sensor.Location = new System.Drawing.Point(360, 19); + this.lbSiteName_sensor.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0); + this.lbSiteName_sensor.Name = "lbSiteName_sensor"; + this.lbSiteName_sensor.Size = new System.Drawing.Size(107, 24); + this.lbSiteName_sensor.TabIndex = 9; + this.lbSiteName_sensor.Text = "Site_Name"; + // + // tabPage2 + // + this.tabPage2.Location = new System.Drawing.Point(4, 31); + this.tabPage2.Name = "tabPage2"; + this.tabPage2.Padding = new System.Windows.Forms.Padding(3); + this.tabPage2.Size = new System.Drawing.Size(1774, 918); + this.tabPage2.TabIndex = 1; + this.tabPage2.Text = "tabPage2"; + this.tabPage2.UseVisualStyleBackColor = true; + // // fmArchive // this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 19F); @@ -121,12 +264,16 @@ namespace solarApp this.Controls.Add(this.tabControl1); this.Name = "fmArchive"; this.Text = "fmArchive"; + this.Load += new System.EventHandler(this.fmArchive_Load); this.tabControl1.ResumeLayout(false); this.tabPage1.ResumeLayout(false); + this.splitContainer1.Panel1.ResumeLayout(false); this.splitContainer1.Panel2.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit(); this.splitContainer1.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit(); + this.panel1.ResumeLayout(false); + this.panel1.PerformLayout(); this.ResumeLayout(false); } @@ -139,5 +286,17 @@ namespace solarApp private System.Windows.Forms.DataGridView dataGridView1; private System.Windows.Forms.Panel panel1; private System.Windows.Forms.TabPage tabPage2; + private System.Windows.Forms.DateTimePicker dtSelect; + private System.Windows.Forms.Button bt_site; + private System.Windows.Forms.Button bt_Inv; + private System.Windows.Forms.Button bt_Sensor; + private System.Windows.Forms.FlowLayoutPanel flowLayoutPanel1; + private System.Windows.Forms.Label lbSiteDB_sensor; + private System.Windows.Forms.Label lbSiteID_sensor; + private System.Windows.Forms.Label lbSiteName_sensor; + private System.Windows.Forms.FlowLayoutPanel fp_site; + private System.Windows.Forms.Button btVerifyData; + private System.Windows.Forms.Button bt_clear_inv; + private System.Windows.Forms.Button bt_clear_station; } } \ No newline at end of file diff --git a/solarApp/fmArchive.cs b/solarApp/fmArchive.cs index 7c2ab47..2c5ee0d 100644 --- a/solarApp/fmArchive.cs +++ b/solarApp/fmArchive.cs @@ -5,11 +5,13 @@ using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; +using solarApp.Service; namespace solarApp { public partial class fmArchive : Form { + getStationSvc stationSvc = new getStationSvc(); public fmArchive() { InitializeComponent(); @@ -19,5 +21,133 @@ namespace solarApp { } + + private void bt_Sensor_Click(object sender, EventArgs e) + { + string date1 = dtSelect.Value.ToString("yyyy-MM-dd"); + //procSensorSvc sensorSvc = new procSensorSvc(lbSiteDB_sensor.Text, lbSiteID_sensor.Text, date1, date1); + procSensorSvc sensorSvc = new procSensorSvc(); + sensorSvc._siteDB = lbSiteDB_sensor.Text; + sensorSvc._siteID = lbSiteID_sensor.Text.Substring(0, 9); + sensorSvc._siteID01 = lbSiteID_sensor.Text; + sensorSvc._date1 = date1; + sensorSvc._date2 = date1; + sensorSvc.archiveData(); + //sensorSvc.archiveData(); + MessageBox.Show("OK"); + } + + private void fmArchive_Load(object sender, EventArgs e) + { + #region 電站清單 + int i = 0; + var site_list = stationSvc.get_station_list(); + foreach (var item in site_list) + { + RadioButton rb = new RadioButton(); + rb.Name = item.SiteID; + rb.Text = item.SiteName; + rb.Tag = item.SiteDB; + rb.Font = new Font(Font.FontFamily, 22); + rb.AutoSize = true; + rb.CheckedChanged += new EventHandler(rb_site_CheckedChanged); + fp_site.Controls.Add(rb); + if (i == 0) rb.Checked = true; + i++; + } + #endregion + } + + private void rb_site_CheckedChanged(object sender, EventArgs e) + { + RadioButton rb = sender as RadioButton; + if (rb.Checked) + { + lbSiteName_sensor.Text = rb.Text; + lbSiteDB_sensor.Text = rb.Tag.ToString(); + lbSiteID_sensor.Text = rb.Name; + + //顯示 table name on label + //lbSiteRaw.Text = lbSiteName_inv.Text + " " + lbSiteDB_inv.Text + ".s" + lbSiteID_inv.Text + "_Station"; + //lbInvRaw.Text = lbSiteName_inv.Text + " " + lbSiteDB_inv.Text + ".s" + lbSiteID_inv.Text + "_Inv"; + //lbSensorRaw.Text = lbSiteName_inv.Text + " " + lbSiteDB_inv.Text + ".s" + lbSiteID_inv.Text + "_SensorAVG"; + + //add_inv_list(lbSiteDB_inv.Text, lbSiteID_inv.Text.Substring(0, 9)); + } + } + + private void btVerifyData_Click(object sender, EventArgs e) + { + fmMain fm = new fmMain(); + fm.Show(); + } + + private void bt_Inv_Click(object sender, EventArgs e) + { + string date1 = dtSelect.Value.ToString("yyyy-MM-dd"); + //procSensorSvc sensorSvc = new procSensorSvc(lbSiteDB_sensor.Text, lbSiteID_sensor.Text, date1, date1); + procInvSvc invSvc = new procInvSvc(); + invSvc._siteDB = lbSiteDB_sensor.Text; + invSvc._siteID = lbSiteID_sensor.Text.Substring(0, 9); + invSvc._siteID01 = lbSiteID_sensor.Text; + invSvc._date1 = date1; + invSvc._date2 = date1; + invSvc.archiveData(); + //sensorSvc.archiveData(); + MessageBox.Show("OK"); + } + + private void dtSelect_ValueChanged(object sender, EventArgs e) + { + bt_clear_inv.Enabled = true; + bt_clear_station.Enabled = true; + bt_clear_inv.Text = "clear " + dtSelect.Value.ToString("MM-dd"); + bt_clear_station.Text = "clear " + dtSelect.Value.ToString("MM-dd"); + } + + private void bt_clear_inv_Click(object sender, EventArgs e) + { + string date1 = dtSelect.Value.ToString("yyyy-MM-dd"); + //procSensorSvc sensorSvc = new procSensorSvc(lbSiteDB_sensor.Text, lbSiteID_sensor.Text, date1, date1); + procInvSvc invSvc = new procInvSvc(); + invSvc._siteDB = lbSiteDB_sensor.Text; + invSvc._siteID = lbSiteID_sensor.Text.Substring(0, 9); + invSvc._siteID01 = lbSiteID_sensor.Text; + invSvc._date1 = date1; + invSvc._date2 = date1; + invSvc.clearData(); + //sensorSvc.archiveData(); + MessageBox.Show("OK"); + } + + private void bt_clear_station_Click(object sender, EventArgs e) + { + string date1 = dtSelect.Value.ToString("yyyy-MM-dd"); + //procSensorSvc sensorSvc = new procSensorSvc(lbSiteDB_sensor.Text, lbSiteID_sensor.Text, date1, date1); + procStationSvc siteSvc = new procStationSvc(); + siteSvc._siteDB = lbSiteDB_sensor.Text; + siteSvc._siteID = lbSiteID_sensor.Text.Substring(0, 9); + siteSvc._siteID01 = lbSiteID_sensor.Text; + siteSvc._date1 = date1; + siteSvc._date2 = date1; + siteSvc.clearData(); + //sensorSvc.archiveData(); + MessageBox.Show("OK"); + } + + private void bt_site_Click(object sender, EventArgs e) + { + string date1 = dtSelect.Value.ToString("yyyy-MM-dd"); + //procSensorSvc sensorSvc = new procSensorSvc(lbSiteDB_sensor.Text, lbSiteID_sensor.Text, date1, date1); + procStationSvc siteSvc = new procStationSvc(); + siteSvc._siteDB = lbSiteDB_sensor.Text; + siteSvc._siteID = lbSiteID_sensor.Text.Substring(0, 9); + siteSvc._siteID01 = lbSiteID_sensor.Text; + siteSvc._date1 = date1; + siteSvc._date2 = date1; + siteSvc.archiveData(); + //sensorSvc.archiveData(); + MessageBox.Show("OK"); + } } } diff --git a/solarApp/fmMain.Designer.cs b/solarApp/fmMain.Designer.cs index 9b88f2b..8569d89 100644 --- a/solarApp/fmMain.Designer.cs +++ b/solarApp/fmMain.Designer.cs @@ -97,6 +97,7 @@ namespace solarApp this.label15 = new System.Windows.Forms.Label(); this.gv_web_inv_hour = new System.Windows.Forms.DataGridView(); this.panel1 = new System.Windows.Forms.Panel(); + this.btInv_15min = new System.Windows.Forms.Button(); this.label3 = new System.Windows.Forms.Label(); this.lb_inv_hour = new System.Windows.Forms.Label(); this.gv_fic_inv_hour = new System.Windows.Forms.DataGridView(); @@ -155,6 +156,7 @@ namespace solarApp this.label29 = new System.Windows.Forms.Label(); this.gv_web_sensor_hour = new System.Windows.Forms.DataGridView(); this.panel13 = new System.Windows.Forms.Panel(); + this.btSensor_switch = new System.Windows.Forms.Button(); this.label30 = new System.Windows.Forms.Label(); this.label31 = new System.Windows.Forms.Label(); this.gv_fic_sensor_hour = new System.Windows.Forms.DataGridView(); @@ -566,6 +568,7 @@ namespace solarApp // // panel1 // + this.panel1.Controls.Add(this.btInv_15min); this.panel1.Controls.Add(this.label3); this.panel1.Controls.Add(this.lb_inv_hour); this.panel1.Dock = System.Windows.Forms.DockStyle.Top; @@ -575,6 +578,16 @@ namespace solarApp this.panel1.Size = new System.Drawing.Size(666, 38); this.panel1.TabIndex = 0; // + // btInv_15min + // + this.btInv_15min.Location = new System.Drawing.Point(559, 4); + this.btInv_15min.Name = "btInv_15min"; + this.btInv_15min.Size = new System.Drawing.Size(94, 29); + this.btInv_15min.TabIndex = 2; + this.btInv_15min.Text = " 15 min"; + this.btInv_15min.UseVisualStyleBackColor = true; + this.btInv_15min.Click += new System.EventHandler(this.btInv_15min_Click); + // // label3 // this.label3.AutoSize = true; @@ -1476,6 +1489,7 @@ namespace solarApp // // panel13 // + this.panel13.Controls.Add(this.btSensor_switch); this.panel13.Controls.Add(this.label30); this.panel13.Controls.Add(this.label31); this.panel13.Dock = System.Windows.Forms.DockStyle.Top; @@ -1485,6 +1499,16 @@ namespace solarApp this.panel13.Size = new System.Drawing.Size(666, 38); this.panel13.TabIndex = 0; // + // btSensor_switch + // + this.btSensor_switch.Location = new System.Drawing.Point(563, 6); + this.btSensor_switch.Name = "btSensor_switch"; + this.btSensor_switch.Size = new System.Drawing.Size(94, 29); + this.btSensor_switch.TabIndex = 10; + this.btSensor_switch.Text = "sensor50"; + this.btSensor_switch.UseVisualStyleBackColor = true; + this.btSensor_switch.Click += new System.EventHandler(this.btSensor_switch_Click); + // // label30 // this.label30.AutoSize = true; @@ -1821,5 +1845,7 @@ namespace solarApp private System.Windows.Forms.DataGridView gv_web_sensor_hour; private System.Windows.Forms.Label lbSiteDB_sensor; private System.Windows.Forms.Button btSearch_sensor; + private System.Windows.Forms.Button btSensor_switch; + private System.Windows.Forms.Button btInv_15min; } } \ No newline at end of file diff --git a/solarApp/fmMain.cs b/solarApp/fmMain.cs index 1b62c66..45884e9 100644 --- a/solarApp/fmMain.cs +++ b/solarApp/fmMain.cs @@ -41,10 +41,21 @@ namespace solarApp lbMsg_inv.Text = " done " + System.DateTime.Now.ToShortTimeString(); } - + + private void btInv_15min_Click(object sender, EventArgs e) + { + lbMsg_inv.Text = "loading ... "; + string date2 = dtselect_inv.Value.ToString("yyyy-MM-dd"); + //string date2 = dtSelect_sensor2.Value.ToString("yyyy-MM-dd"); + string date1 = dtselect_inv.Value.AddDays(-7).ToString("yyyy-MM-dd"); + + gv_web_inv_hour.DataSource = inv_svc.get_web_Inv_15min(date2, lbInverterID.Text , lbSiteID_inv.Text.Substring(0, 9)); + } private void fmMain_Load(object sender, EventArgs e) { + + dtselect_station1.Value = DateTime.Today.AddDays(-1); dtselect_inv.Value = DateTime.Today.AddDays(-1); @@ -174,7 +185,7 @@ namespace solarApp gv_fic_station_day.DataSource = stationSvc.get_station_rawAvg(date1, date2, lbSiteDB_inv.Text, lbSiteID_inv.Text); - gv_web_station_hour.DataSource = stationSvc.get_web_station_hour(date2); + gv_web_station_hour.DataSource = stationSvc.get_web_station_hour(date2, lbSiteID_inv.Text); gv_web_station_day.DataSource = stationSvc.get_web_station_day(date1, date2, lbSiteID_inv.Text.Substring(0, 9)); @@ -199,5 +210,18 @@ namespace solarApp gv_web_sensor_month.DataSource = sensorSvc.get_web_sensor_month(date1.Substring(0, 7), date2.Substring(0, 7), lbSiteID_inv.Text.Substring(0, 09)); } + + private void btSensor_switch_Click(object sender, EventArgs e) + { + string date2 = dtSelect_sensor1.Value.ToString("yyyy-MM-dd"); + //string date2 = dtSelect_sensor2.Value.ToString("yyyy-MM-dd"); + string date1 = dtSelect_sensor1.Value.AddDays(-7).ToString("yyyy-MM-dd"); + + gv_web_sensor_hour.DataSource = sensorSvc.get_web_sensor50_hour(date2, lbSiteID_inv.Text.Substring(0, 09)); + + gv_web_sensor_day.DataSource = sensorSvc.get_web_sensor50_day(date1, date2, lbSiteID_inv.Text.Substring(0, 09)); + + gv_web_sensor_month.DataSource = sensorSvc.get_web_sensor50_month(date1.Substring(0, 7), date2.Substring(0, 7), lbSiteID_inv.Text.Substring(0, 09)); + } } }