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 getSensorSvc { string Connection1 = ConfigurationManager.ConnectionStrings["mySql"].ConnectionString; /// /// 電站 Raw Data /// /// /// /// /// public List get_sensor_raw(string reportDate, string siteDB, string siteID) { using (MySqlConnection conn = new MySqlConnection(Connection1)) { conn.Open(); string sql = @"select siteid , FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H:%i') reportdate, round(sensorAvg01, 3) sensorAvg01, round(sensorAvg02, 3) sensorAvg02, round(sensorAvg03, 3) sensorAvg03, round(sensorAvg04, 3) sensorAvg04, round(sensorAvg05, 3) sensorAvg05, round(sensorAvg06, 3) sensorAvg06 from " + siteDB + ".s" + siteID + @"_sensorAvg where left(FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H:%i'), 10) = @reportDate"; List ds = conn.Query(sql, new { reportDate = reportDate }).AsList(); conn.Close(); return ds; } } /// /// 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; } public List get_sensor_raw_hour(string reportDate, string siteDB, string siteID) { using (MySqlConnection conn = new MySqlConnection(Connection1)) { conn.Open(); #region 獲取 Sensor 類別 string 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"; List ds_sensor = conn.Query(sql, new { siteID = siteID}).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 類別 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 from " + siteDB + ".s" + siteID + @"_sensorAvg where FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d') = @reportDate group by FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H') ) a left join ( select FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H:%i') reportdate, round(avg("+ irrCol + @"), 2) 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') )b on a.reportdate = b.reportdate"; List ds = conn.Query(sql, new { siteID = siteID, reportDate = reportDate }).AsList(); conn.Close(); return ds; } } /// /// web 呈現值 station - hour /// /// /// public List get_web_sensor_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, round(Irradiance, 2) irrAvg, round(Temperature, 2) modelTempAvg from sensor_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_sensor_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, round(Irradiance, 2) irrAvg, round(Temperature, 2) modelTempAvg from sensor_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 }).AsList(); conn.Close(); return ds; } } public List get_web_sensor_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, 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(); conn.Close(); return ds; } } } }